blob: d9c7e368c62f7a7e03b3ea50625cad195e26da20 [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
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500786LinkResult ProgramD3D::load(const ContextImpl *contextImpl,
787 gl::InfoLog &infoLog,
788 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700789{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500790 // TODO(jmadill): Use Renderer from contextImpl.
791
Jamie Madill62d31cb2015-09-11 13:25:51 -0400792 reset();
793
Jamie Madill334d6152015-10-22 14:00:28 -0400794 DeviceIdentifier binaryDeviceIdentifier = {0};
795 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
796 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700797
798 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
799 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
800 {
801 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500802 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700803 }
804
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500805 int compileFlags = stream->readInt<int>();
806 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
807 {
Jamie Madillf6113162015-05-07 11:49:21 -0400808 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500809 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500810 }
811
Jamie Madill8047c0d2016-03-07 13:02:12 -0500812 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400813 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500814 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400815 }
816
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700817 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
818 for (unsigned int i = 0; i < psSamplerCount; ++i)
819 {
820 Sampler sampler;
821 stream->readBool(&sampler.active);
822 stream->readInt(&sampler.logicalTextureUnit);
823 stream->readInt(&sampler.textureType);
824 mSamplersPS.push_back(sampler);
825 }
826 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
827 for (unsigned int i = 0; i < vsSamplerCount; ++i)
828 {
829 Sampler sampler;
830 stream->readBool(&sampler.active);
831 stream->readInt(&sampler.logicalTextureUnit);
832 stream->readInt(&sampler.textureType);
833 mSamplersVS.push_back(sampler);
834 }
835
836 stream->readInt(&mUsedVertexSamplerRange);
837 stream->readInt(&mUsedPixelSamplerRange);
838
839 const unsigned int uniformCount = stream->readInt<unsigned int>();
840 if (stream->error())
841 {
Jamie Madillf6113162015-05-07 11:49:21 -0400842 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500843 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700844 }
845
Jamie Madill48ef11b2016-04-27 15:21:52 -0400846 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400847 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700848 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
849 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400850 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700851
Jamie Madill62d31cb2015-09-11 13:25:51 -0400852 D3DUniform *d3dUniform =
853 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
854 linkedUniform.isInDefaultBlock());
855 stream->readInt(&d3dUniform->psRegisterIndex);
856 stream->readInt(&d3dUniform->vsRegisterIndex);
857 stream->readInt(&d3dUniform->registerCount);
858 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700859
Jamie Madill62d31cb2015-09-11 13:25:51 -0400860 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700861 }
862
Jamie Madill4a3c2342015-10-08 12:58:45 -0400863 const unsigned int blockCount = stream->readInt<unsigned int>();
864 if (stream->error())
865 {
866 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500867 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400868 }
869
870 ASSERT(mD3DUniformBlocks.empty());
871 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
872 {
873 D3DUniformBlock uniformBlock;
874 stream->readInt(&uniformBlock.psRegisterIndex);
875 stream->readInt(&uniformBlock.vsRegisterIndex);
876 mD3DUniformBlocks.push_back(uniformBlock);
877 }
878
Jamie Madill9fc36822015-11-18 13:08:07 -0500879 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
880 mStreamOutVaryings.resize(streamOutVaryingCount);
881 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700882 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500883 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700884
Jamie Madill28afae52015-11-09 15:07:57 -0500885 stream->readString(&varying->semanticName);
886 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500887 stream->readInt(&varying->componentCount);
888 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700889 }
890
Brandon Jones22502d52014-08-29 16:58:36 -0700891 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400892 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
893 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700894 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400895 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
896 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700897 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700898 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400899 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700900
901 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
902 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400903 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
904 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700905 {
906 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
907 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
908 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
909 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
910 }
911
Jamie Madill4e31ad52015-10-29 10:32:57 -0400912 stream->readString(&mGeometryShaderPreamble);
913
Jamie Madill334d6152015-10-22 14:00:28 -0400914 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700915
Jamie Madillb0a838b2016-11-13 20:02:12 -0500916 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
917
Brandon Joneseb994362014-09-24 10:27:28 -0700918 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400919 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
920 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700921 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400922 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400923 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700924
Jamie Madilld3dfda22015-07-06 08:28:49 -0400925 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700926 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400927 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700928 }
929
Jamie Madill334d6152015-10-22 14:00:28 -0400930 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700931 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400932
Jamie Madillada9ecc2015-08-17 12:53:37 -0400933 ShaderExecutableD3D *shaderExecutable = nullptr;
934
Jamie Madillb0a838b2016-11-13 20:02:12 -0500935 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
936 mStreamOutVaryings, separateAttribs,
937 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400938
Brandon Joneseb994362014-09-24 10:27:28 -0700939 if (!shaderExecutable)
940 {
Jamie Madillf6113162015-05-07 11:49:21 -0400941 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500942 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700943 }
944
945 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400946 VertexExecutable::Signature signature;
947 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700948
949 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400950 mVertexExecutables.push_back(
951 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700952
953 stream->skip(vertexShaderSize);
954 }
955
956 const size_t pixelShaderCount = stream->readInt<unsigned int>();
957 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
958 {
959 const size_t outputCount = stream->readInt<unsigned int>();
960 std::vector<GLenum> outputs(outputCount);
961 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
962 {
963 stream->readInt(&outputs[outputIndex]);
964 }
965
Jamie Madill334d6152015-10-22 14:00:28 -0400966 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700967 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400968 ShaderExecutableD3D *shaderExecutable = nullptr;
969
Jamie Madillb0a838b2016-11-13 20:02:12 -0500970 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
971 mStreamOutVaryings, separateAttribs,
972 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700973
974 if (!shaderExecutable)
975 {
Jamie Madillf6113162015-05-07 11:49:21 -0400976 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500977 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700978 }
979
980 // add new binary
981 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
982
983 stream->skip(pixelShaderSize);
984 }
985
Jamie Madill4e31ad52015-10-29 10:32:57 -0400986 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
987 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700988 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400989 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
990 if (geometryShaderSize == 0)
991 {
992 mGeometryExecutables[geometryExeIndex] = nullptr;
993 continue;
994 }
995
Brandon Joneseb994362014-09-24 10:27:28 -0700996 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400997
Jamie Madillb0a838b2016-11-13 20:02:12 -0500998 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
999 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
1000 &mGeometryExecutables[geometryExeIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001001
Jamie Madill4e31ad52015-10-29 10:32:57 -04001002 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -07001003 {
Jamie Madillf6113162015-05-07 11:49:21 -04001004 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001005 return false;
Brandon Joneseb994362014-09-24 10:27:28 -07001006 }
1007 stream->skip(geometryShaderSize);
1008 }
1009
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001010 initializeUniformStorage();
1011
Jamie Madillb0a838b2016-11-13 20:02:12 -05001012 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001013}
1014
Geoff Langb543aff2014-09-30 14:52:54 -04001015gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -07001016{
Austin Kinross137b1512015-06-17 16:14:53 -07001017 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -04001018 // When we load the binary again later, we can validate the device identifier before trying to
1019 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -07001020 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -04001021 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
1022 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -07001023
Jamie Madill2db1fbb2014-12-03 10:58:55 -05001024 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1025
Jamie Madill8047c0d2016-03-07 13:02:12 -05001026 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -04001027 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05001028 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -04001029 }
1030
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001031 stream->writeInt(mSamplersPS.size());
1032 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
1033 {
1034 stream->writeInt(mSamplersPS[i].active);
1035 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
1036 stream->writeInt(mSamplersPS[i].textureType);
1037 }
1038
1039 stream->writeInt(mSamplersVS.size());
1040 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
1041 {
1042 stream->writeInt(mSamplersVS[i].active);
1043 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
1044 stream->writeInt(mSamplersVS[i].textureType);
1045 }
1046
1047 stream->writeInt(mUsedVertexSamplerRange);
1048 stream->writeInt(mUsedPixelSamplerRange);
1049
Jamie Madill62d31cb2015-09-11 13:25:51 -04001050 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001051 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001052 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001053 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001054 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1055 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001056 stream->writeInt(uniform->registerCount);
1057 stream->writeInt(uniform->registerElement);
1058 }
1059
1060 stream->writeInt(mD3DUniformBlocks.size());
1061 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1062 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001063 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1064 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001065 }
1066
Jamie Madill9fc36822015-11-18 13:08:07 -05001067 stream->writeInt(mStreamOutVaryings.size());
1068 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001069 {
Brandon Joneseb994362014-09-24 10:27:28 -07001070 stream->writeString(varying.semanticName);
1071 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001072 stream->writeInt(varying.componentCount);
1073 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001074 }
1075
Brandon Jones22502d52014-08-29 16:58:36 -07001076 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001077 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1078 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001079 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001080 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1081 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001082 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001083 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001084 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001085
Brandon Joneseb994362014-09-24 10:27:28 -07001086 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001087 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001088 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1089 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001090 {
Brandon Joneseb994362014-09-24 10:27:28 -07001091 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001092 stream->writeInt(variable.type);
1093 stream->writeString(variable.name);
1094 stream->writeString(variable.source);
1095 stream->writeInt(variable.outputIndex);
1096 }
1097
Jamie Madill4e31ad52015-10-29 10:32:57 -04001098 stream->writeString(mGeometryShaderPreamble);
1099
Brandon Joneseb994362014-09-24 10:27:28 -07001100 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001101 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1102 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001103 {
1104 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1105
Jamie Madilld3dfda22015-07-06 08:28:49 -04001106 const auto &inputLayout = vertexExecutable->inputs();
1107 stream->writeInt(inputLayout.size());
1108
1109 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001110 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001111 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001112 }
1113
1114 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1115 stream->writeInt(vertexShaderSize);
1116
1117 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1118 stream->writeBytes(vertexBlob, vertexShaderSize);
1119 }
1120
1121 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001122 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1123 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001124 {
1125 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1126
1127 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1128 stream->writeInt(outputs.size());
1129 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1130 {
1131 stream->writeInt(outputs[outputIndex]);
1132 }
1133
1134 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1135 stream->writeInt(pixelShaderSize);
1136
1137 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1138 stream->writeBytes(pixelBlob, pixelShaderSize);
1139 }
1140
Jamie Madill4e31ad52015-10-29 10:32:57 -04001141 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001142 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001143 if (geometryExe == nullptr)
1144 {
1145 stream->writeInt(0);
1146 continue;
1147 }
1148
1149 size_t geometryShaderSize = geometryExe->getLength();
1150 stream->writeInt(geometryShaderSize);
1151 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001152 }
1153
Geoff Langb543aff2014-09-30 14:52:54 -04001154 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001155}
1156
Geoff Langc5629752015-12-07 16:29:04 -05001157void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1158{
1159}
1160
Jamie Madill334d6152015-10-22 14:00:28 -04001161gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1162 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001163{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001164 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001165
Jamie Madill85a18042015-03-05 15:41:41 -05001166 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001167 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001168
1169 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1170 {
1171 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1172
1173 if (colorbuffer)
1174 {
Jamie Madill334d6152015-10-22 14:00:28 -04001175 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1176 ? GL_COLOR_ATTACHMENT0
1177 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001178 }
1179 else
1180 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001181 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001182 }
1183 }
1184
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001185 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001186}
1187
Jamie Madill97399232014-12-23 12:31:15 -05001188gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001189 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001190 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001191{
1192 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1193 {
1194 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1195 {
Geoff Langb543aff2014-09-30 14:52:54 -04001196 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1197 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001198 }
1199 }
1200
Jamie Madill334d6152015-10-22 14:00:28 -04001201 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1202 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001203
1204 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001205 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001206
1207 gl::InfoLog tempInfoLog;
1208 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1209
Jamie Madill01074252016-11-28 15:55:51 -05001210 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001211 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001212 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001213 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001214
Jamie Madill97399232014-12-23 12:31:15 -05001215 if (pixelExecutable)
1216 {
1217 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1218 }
1219 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001220 {
1221 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001222 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001223 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1224 }
Brandon Jones22502d52014-08-29 16:58:36 -07001225
Geoff Langb543aff2014-09-30 14:52:54 -04001226 *outExectuable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001227 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001228}
1229
Jamie Madilld3dfda22015-07-06 08:28:49 -04001230gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001231 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001232 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001233{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001234 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001235
1236 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1237 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001238 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001239 {
Geoff Langb543aff2014-09-30 14:52:54 -04001240 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1241 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001242 }
1243 }
1244
Brandon Jones22502d52014-08-29 16:58:36 -07001245 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001246 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001247 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001248
1249 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001250 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001251
1252 gl::InfoLog tempInfoLog;
1253 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1254
Jamie Madill01074252016-11-28 15:55:51 -05001255 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001256 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001257 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001258 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001259
Jamie Madill97399232014-12-23 12:31:15 -05001260 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001261 {
Jamie Madill334d6152015-10-22 14:00:28 -04001262 mVertexExecutables.push_back(
1263 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001264 }
Jamie Madill97399232014-12-23 12:31:15 -05001265 else if (!infoLog)
1266 {
1267 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001268 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001269 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1270 }
Brandon Jones22502d52014-08-29 16:58:36 -07001271
Geoff Langb543aff2014-09-30 14:52:54 -04001272 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001273 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001274}
1275
Jamie Madill9082b982016-04-27 15:21:51 -04001276gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001277 GLenum drawMode,
1278 ShaderExecutableD3D **outExecutable,
1279 gl::InfoLog *infoLog)
1280{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001281 if (outExecutable)
1282 {
1283 *outExecutable = nullptr;
1284 }
1285
Austin Kinross88829e82016-01-12 13:04:41 -08001286 // Return a null shader if the current rendering doesn't use a geometry shader
1287 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001288 {
1289 return gl::Error(GL_NO_ERROR);
1290 }
1291
Jamie Madill4e31ad52015-10-29 10:32:57 -04001292 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1293
1294 if (mGeometryExecutables[geometryShaderType] != nullptr)
1295 {
1296 if (outExecutable)
1297 {
1298 *outExecutable = mGeometryExecutables[geometryShaderType];
1299 }
1300 return gl::Error(GL_NO_ERROR);
1301 }
1302
1303 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001304 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001305 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001306
1307 gl::InfoLog tempInfoLog;
1308 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1309
1310 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001311 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001312 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
Jamie Madill4e31ad52015-10-29 10:32:57 -04001313 &mGeometryExecutables[geometryShaderType]);
1314
1315 if (!infoLog && error.isError())
1316 {
1317 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1318 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1319 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1320 }
1321
1322 if (outExecutable)
1323 {
1324 *outExecutable = mGeometryExecutables[geometryShaderType];
1325 }
1326 return error;
1327}
1328
Jamie Madill01074252016-11-28 15:55:51 -05001329class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001330{
Jamie Madill01074252016-11-28 15:55:51 -05001331 public:
1332 GetExecutableTask(ProgramD3D *program)
1333 : mProgram(program), mError(GL_NO_ERROR), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001334 {
Brandon Joneseb994362014-09-24 10:27:28 -07001335 }
1336
Jamie Madill01074252016-11-28 15:55:51 -05001337 virtual gl::Error run() = 0;
1338
1339 void operator()() override { mError = run(); }
1340
1341 const gl::Error &getError() const { return mError; }
1342 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1343 ShaderExecutableD3D *getResult() { return mResult; }
1344
1345 protected:
1346 ProgramD3D *mProgram;
1347 gl::Error mError;
1348 gl::InfoLog mInfoLog;
1349 ShaderExecutableD3D *mResult;
1350};
1351
1352class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1353{
1354 public:
1355 GetVertexExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1356 gl::Error run() override
1357 {
1358 const auto &defaultInputLayout =
1359 GetDefaultInputLayoutFromShader(mProgram->mState.getAttachedVertexShader());
1360
1361 ANGLE_TRY(
1362 mProgram->getVertexExecutableForInputLayout(defaultInputLayout, &mResult, &mInfoLog));
1363
1364 return gl::NoError();
1365 }
1366};
1367
1368class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1369{
1370 public:
1371 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1372 gl::Error run() override
1373 {
1374 const auto &defaultPixelOutput =
1375 GetDefaultOutputLayoutFromShader(mProgram->getPixelShaderKey());
1376
1377 ANGLE_TRY(
1378 mProgram->getPixelExecutableForOutputLayout(defaultPixelOutput, &mResult, &mInfoLog));
1379
1380 return gl::NoError();
1381 }
1382};
1383
1384class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1385{
1386 public:
1387 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1388 : GetExecutableTask(program), mContextState(contextState)
1389 {
1390 }
1391
1392 gl::Error run() override
1393 {
1394 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1395 // D3D11.
1396 if (mProgram->usesGeometryShader(GL_POINTS))
1397 {
1398 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1399 &mResult, &mInfoLog));
1400 }
1401
1402 return gl::NoError();
1403 }
1404
1405 private:
1406 const gl::ContextState &mContextState;
1407};
1408
1409LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &contextState,
1410 gl::InfoLog &infoLog)
1411{
1412 // Ensure the compiler is initialized to avoid race conditions.
1413 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1414
1415 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1416
1417 GetVertexExecutableTask vertexTask(this);
1418 GetPixelExecutableTask pixelTask(this);
1419 GetGeometryExecutableTask geometryTask(this, contextState);
1420
1421 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1422 workerPool->postWorkerTask(&pixelTask),
1423 workerPool->postWorkerTask(&geometryTask)}};
1424
1425 WaitableEvent::WaitMany(&waitEvents);
1426
1427 infoLog << vertexTask.getInfoLog().str();
1428 infoLog << pixelTask.getInfoLog().str();
1429 infoLog << geometryTask.getInfoLog().str();
1430
1431 ANGLE_TRY(vertexTask.getError());
1432 ANGLE_TRY(pixelTask.getError());
1433 ANGLE_TRY(geometryTask.getError());
1434
1435 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1436 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1437 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1438
Jamie Madill48ef11b2016-04-27 15:21:52 -04001439 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001440
1441 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001442 {
Jamie Madill334d6152015-10-22 14:00:28 -04001443 // Geometry shaders are currently only used internally, so there is no corresponding shader
1444 // object at the interface level. For now the geometry shader debug info is prepended to
1445 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001446 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001447 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001448 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1449 }
1450
1451 if (defaultVertexExecutable)
1452 {
1453 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1454 }
1455
1456 if (defaultPixelExecutable)
1457 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001458 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001459 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001460 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1461 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001462
Jamie Madillb0a838b2016-11-13 20:02:12 -05001463 return (defaultVertexExecutable && defaultPixelExecutable &&
1464 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001465}
1466
Jamie Madill9082b982016-04-27 15:21:51 -04001467LinkResult ProgramD3D::link(const gl::ContextState &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001468{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001469 reset();
1470
Jamie Madill48ef11b2016-04-27 15:21:52 -04001471 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1472 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001473
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001474 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1475 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1476
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001477 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1478 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001479
Arun Patole44efa0b2015-03-04 17:11:05 +05301480 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001481 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1482
Jamie Madill53ea9cc2016-05-17 10:12:52 -04001483 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
Austin Kinross02df7962015-07-01 10:03:42 -07001484 {
1485 if (fragmentShaderD3D->usesFrontFacing())
1486 {
1487 infoLog << "The current renderer doesn't support gl_FrontFacing";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001488 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001489 }
1490 }
1491
Jamie Madillca03b352015-09-02 12:38:13 -04001492 std::vector<PackedVarying> packedVaryings =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001493 MergeVaryings(*vertexShader, *fragmentShader, mState.getTransformFeedbackVaryingNames());
Jamie Madillca03b352015-09-02 12:38:13 -04001494
Brandon Jones22502d52014-08-29 16:58:36 -07001495 // Map the varyings to the register file
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001496 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors);
Jamie Madill2ad1c492016-12-07 14:46:18 -05001497 if (!varyingPacking.packUserVaryings(infoLog, packedVaryings,
1498 mState.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001499 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001500 return false;
Brandon Jones22502d52014-08-29 16:58:36 -07001501 }
1502
Jamie Madillc9bde922016-07-24 17:58:50 -04001503 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madille39a3f02015-11-17 20:42:15 -05001504
Jamie Madill120040e2016-12-07 14:46:16 -05001505 metadata.updatePackingBuiltins(SHADER_VERTEX, &varyingPacking);
1506 metadata.updatePackingBuiltins(SHADER_PIXEL, &varyingPacking);
Jamie Madill9fc36822015-11-18 13:08:07 -05001507
Jamie Madill2ad1c492016-12-07 14:46:18 -05001508 if (!varyingPacking.validateBuiltins())
Jamie Madill9fc36822015-11-18 13:08:07 -05001509 {
1510 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001511 return false;
Jamie Madill9fc36822015-11-18 13:08:07 -05001512 }
1513
1514 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1515 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1516 // intelligently, but D3D9 assumes one semantic per register.
1517 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001518 varyingPacking.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
Jamie Madill9fc36822015-11-18 13:08:07 -05001519 {
1520 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001521 return false;
Jamie Madill9fc36822015-11-18 13:08:07 -05001522 }
1523
Jamie Madill48ef11b2016-04-27 15:21:52 -04001524 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, varyingPacking, &mPixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -05001525 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001526 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001527 return false;
Brandon Jones22502d52014-08-29 16:58:36 -07001528 }
1529
Brandon Jones44151a92014-09-10 11:32:25 -07001530 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001531 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1532 mUsesFragDepth = metadata.usesFragDepth();
Brandon Jones44151a92014-09-10 11:32:25 -07001533
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001534 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001535 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001536 for (const auto &varying : packedVaryings)
1537 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001538 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001539 {
1540 mUsesFlatInterpolation = true;
1541 break;
1542 }
1543 }
1544
Jamie Madill4e31ad52015-10-29 10:32:57 -04001545 if (mRenderer->getMajorShaderModel() >= 4)
1546 {
Jamie Madill120040e2016-12-07 14:46:16 -05001547 metadata.updatePackingBuiltins(SHADER_GEOMETRY, &varyingPacking);
Jamie Madill9fc36822015-11-18 13:08:07 -05001548 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001549 }
1550
Jamie Madill8047c0d2016-03-07 13:02:12 -05001551 initAttribLocationsToD3DSemantic();
Jamie Madill437d2662014-12-05 14:23:35 -05001552
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001553 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001554
Jamie Madill9fc36822015-11-18 13:08:07 -05001555 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001556
Jamie Madill4e31ad52015-10-29 10:32:57 -04001557 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001558 if (result.isError())
Geoff Lang65d17e52016-09-08 09:52:58 -04001559 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001560 infoLog << result.getError().getMessage();
Geoff Lang65d17e52016-09-08 09:52:58 -04001561 return result;
1562 }
Jamie Madillb0a838b2016-11-13 20:02:12 -05001563 else if (!result.getResult())
Jamie Madill31c8c562015-08-19 14:08:03 -04001564 {
1565 infoLog << "Failed to create D3D shaders.";
1566 return result;
1567 }
1568
Jamie Madill4a3c2342015-10-08 12:58:45 -04001569 initUniformBlockInfo();
1570
Jamie Madillb0a838b2016-11-13 20:02:12 -05001571 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001572}
1573
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001574GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001575{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001576 // TODO(jmadill): Do something useful here?
1577 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001578}
1579
Jamie Madill4a3c2342015-10-08 12:58:45 -04001580void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001581{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001582 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001583
Jamie Madill62d31cb2015-09-11 13:25:51 -04001584 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1585 {
1586 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1587 continue;
1588
Jamie Madill4a3c2342015-10-08 12:58:45 -04001589 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001590 continue;
1591
Jamie Madill4a3c2342015-10-08 12:58:45 -04001592 size_t dataSize = getUniformBlockInfo(vertexBlock);
1593 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001594 }
1595
Jamie Madill48ef11b2016-04-27 15:21:52 -04001596 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001597
1598 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1599 {
1600 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1601 continue;
1602
Jamie Madill4a3c2342015-10-08 12:58:45 -04001603 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001604 continue;
1605
Jamie Madill4a3c2342015-10-08 12:58:45 -04001606 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1607 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001608 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001609}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001610
Jamie Madill4a3c2342015-10-08 12:58:45 -04001611void ProgramD3D::assignUniformBlockRegisters()
1612{
1613 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001614
1615 // Assign registers and update sizes.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001616 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1617 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001618
Jamie Madill48ef11b2016-04-27 15:21:52 -04001619 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001620 {
1621 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1622
Jamie Madill4a3c2342015-10-08 12:58:45 -04001623 D3DUniformBlock d3dUniformBlock;
1624
Jamie Madill62d31cb2015-09-11 13:25:51 -04001625 if (uniformBlock.vertexStaticUse)
1626 {
1627 unsigned int baseRegister =
1628 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001629 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001630 }
1631
1632 if (uniformBlock.fragmentStaticUse)
1633 {
1634 unsigned int baseRegister =
1635 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001636 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001637 }
1638
Jamie Madill4a3c2342015-10-08 12:58:45 -04001639 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001640 }
1641}
1642
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001643void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001644{
1645 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001646 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001647 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001648 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001649 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001650 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001651 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001652 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001653 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001654 vertexRegisters = std::max(vertexRegisters,
1655 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001656 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001657 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001658 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001659 fragmentRegisters = std::max(
1660 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001661 }
1662 }
1663 }
1664
Jamie Madill334d6152015-10-22 14:00:28 -04001665 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001666 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1667}
1668
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001669gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001670{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001671 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001672
Jamie Madill01074252016-11-28 15:55:51 -05001673 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001674
Jamie Madill62d31cb2015-09-11 13:25:51 -04001675 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001676 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001677 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001678 }
1679
1680 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001681}
1682
Jamie Madill9082b982016-04-27 15:21:51 -04001683gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001684{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001685 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001686 {
1687 return gl::Error(GL_NO_ERROR);
1688 }
1689
1690 // Lazy init.
1691 if (mD3DUniformBlocks.empty())
1692 {
1693 assignUniformBlockRegisters();
1694 }
1695
Jamie Madill03260fa2015-06-22 13:57:22 -04001696 mVertexUBOCache.clear();
1697 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001698
1699 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1700 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1701
Jamie Madill4a3c2342015-10-08 12:58:45 -04001702 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001703 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001704 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001705 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001706 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001707
Brandon Jones18bd4102014-09-22 14:21:44 -07001708 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001709 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001710 {
1711 continue;
1712 }
1713
Jamie Madill4a3c2342015-10-08 12:58:45 -04001714 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001715 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001716 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001717 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001718
Jamie Madill969194d2015-07-20 14:36:56 -04001719 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001720 {
1721 mVertexUBOCache.resize(registerIndex + 1, -1);
1722 }
1723
1724 ASSERT(mVertexUBOCache[registerIndex] == -1);
1725 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001726 }
1727
Jamie Madill4a3c2342015-10-08 12:58:45 -04001728 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001729 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001730 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001731 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001732
1733 if (mFragmentUBOCache.size() <= registerIndex)
1734 {
1735 mFragmentUBOCache.resize(registerIndex + 1, -1);
1736 }
1737
1738 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1739 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001740 }
1741 }
1742
Jamie Madill03260fa2015-06-22 13:57:22 -04001743 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001744}
1745
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001746void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001747{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001748 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001749 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001750 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001751 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001752}
1753
Jamie Madill334d6152015-10-22 14:00:28 -04001754void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001755{
1756 setUniform(location, count, v, GL_FLOAT);
1757}
1758
1759void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1760{
1761 setUniform(location, count, v, GL_FLOAT_VEC2);
1762}
1763
1764void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1765{
1766 setUniform(location, count, v, GL_FLOAT_VEC3);
1767}
1768
1769void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1770{
1771 setUniform(location, count, v, GL_FLOAT_VEC4);
1772}
1773
Jamie Madill334d6152015-10-22 14:00:28 -04001774void ProgramD3D::setUniformMatrix2fv(GLint location,
1775 GLsizei count,
1776 GLboolean transpose,
1777 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001778{
1779 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1780}
1781
Jamie Madill334d6152015-10-22 14:00:28 -04001782void ProgramD3D::setUniformMatrix3fv(GLint location,
1783 GLsizei count,
1784 GLboolean transpose,
1785 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001786{
1787 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1788}
1789
Jamie Madill334d6152015-10-22 14:00:28 -04001790void ProgramD3D::setUniformMatrix4fv(GLint location,
1791 GLsizei count,
1792 GLboolean transpose,
1793 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001794{
1795 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1796}
1797
Jamie Madill334d6152015-10-22 14:00:28 -04001798void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1799 GLsizei count,
1800 GLboolean transpose,
1801 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001802{
1803 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1804}
1805
Jamie Madill334d6152015-10-22 14:00:28 -04001806void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1807 GLsizei count,
1808 GLboolean transpose,
1809 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001810{
1811 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1812}
1813
Jamie Madill334d6152015-10-22 14:00:28 -04001814void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1815 GLsizei count,
1816 GLboolean transpose,
1817 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001818{
1819 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1820}
1821
Jamie Madill334d6152015-10-22 14:00:28 -04001822void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1823 GLsizei count,
1824 GLboolean transpose,
1825 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001826{
1827 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1828}
1829
Jamie Madill334d6152015-10-22 14:00:28 -04001830void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1831 GLsizei count,
1832 GLboolean transpose,
1833 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001834{
1835 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1836}
1837
Jamie Madill334d6152015-10-22 14:00:28 -04001838void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1839 GLsizei count,
1840 GLboolean transpose,
1841 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001842{
1843 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1844}
1845
1846void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1847{
1848 setUniform(location, count, v, GL_INT);
1849}
1850
1851void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1852{
1853 setUniform(location, count, v, GL_INT_VEC2);
1854}
1855
1856void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1857{
1858 setUniform(location, count, v, GL_INT_VEC3);
1859}
1860
1861void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1862{
1863 setUniform(location, count, v, GL_INT_VEC4);
1864}
1865
1866void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1867{
1868 setUniform(location, count, v, GL_UNSIGNED_INT);
1869}
1870
1871void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1872{
1873 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1874}
1875
1876void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1877{
1878 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1879}
1880
1881void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1882{
1883 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1884}
1885
Jamie Madill4a3c2342015-10-08 12:58:45 -04001886void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1887 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001888{
1889}
1890
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001891void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001892{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001893 D3DUniformMap uniformMap;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001894 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001895 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001896
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001897 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001898 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001899 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001900 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001901 }
1902 }
1903
Jamie Madill48ef11b2016-04-27 15:21:52 -04001904 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001905 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001906 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001907 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001908 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001909 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001910 }
1911 }
1912
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001913 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001914 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001915 {
1916 if (!glUniform.isInDefaultBlock())
1917 continue;
1918
1919 auto mapEntry = uniformMap.find(glUniform.name);
1920 ASSERT(mapEntry != uniformMap.end());
1921 mD3DUniforms.push_back(mapEntry->second);
1922 }
1923
Jamie Madill62d31cb2015-09-11 13:25:51 -04001924 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001925 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001926}
1927
Jamie Madill91445bc2015-09-23 16:47:53 -04001928void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001929 const sh::Uniform &uniform,
1930 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001931{
Olli Etuaho96963162016-03-21 11:54:33 +02001932 // Samplers get their registers assigned in assignAllSamplerRegisters.
1933 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001934 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001935 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001936 return;
1937 }
1938
Jamie Madill91445bc2015-09-23 16:47:53 -04001939 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1940
1941 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1942 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001943 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001944 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001945
Jamie Madill91445bc2015-09-23 16:47:53 -04001946 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001947}
1948
Jamie Madill62d31cb2015-09-11 13:25:51 -04001949D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1950{
1951 for (D3DUniform *d3dUniform : mD3DUniforms)
1952 {
1953 if (d3dUniform->name == name)
1954 {
1955 return d3dUniform;
1956 }
1957 }
1958
1959 return nullptr;
1960}
1961
Jamie Madill91445bc2015-09-23 16:47:53 -04001962void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001963 const sh::ShaderVariable &uniform,
1964 const std::string &fullName,
1965 sh::HLSLBlockEncoder *encoder,
1966 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001967{
1968 if (uniform.isStruct())
1969 {
1970 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1971 {
1972 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1973
Jamie Madill55def582015-05-04 11:24:57 -04001974 if (encoder)
1975 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001976
1977 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1978 {
Jamie Madill334d6152015-10-22 14:00:28 -04001979 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001980 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1981
Olli Etuaho96963162016-03-21 11:54:33 +02001982 // Samplers get their registers assigned in assignAllSamplerRegisters.
1983 // Also they couldn't use the same encoder as the rest of the struct, since they are
1984 // extracted out of the struct by the shader translator.
1985 if (gl::IsSamplerType(field.type))
1986 {
1987 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
1988 }
1989 else
1990 {
1991 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
1992 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001993 }
1994
Jamie Madill55def582015-05-04 11:24:57 -04001995 if (encoder)
1996 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001997 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001998 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001999 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002000
2001 // Not a struct. Arrays are treated as aggregate types.
2002 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002003 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002004 encoder->enterAggregateType();
2005 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002006
Jamie Madill62d31cb2015-09-11 13:25:51 -04002007 // Advance the uniform offset, to track registers allocation for structs
2008 sh::BlockMemberInfo blockInfo =
2009 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
2010 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002011
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002012 auto uniformMapEntry = uniformMap->find(fullName);
2013 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002014
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002015 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002016 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002017 d3dUniform = uniformMapEntry->second;
2018 }
2019 else
2020 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002021 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002022 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002023 }
2024
2025 if (encoder)
2026 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002027 d3dUniform->registerElement =
2028 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002029 unsigned int reg =
2030 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002031 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002032 {
2033 d3dUniform->psRegisterIndex = reg;
2034 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002035 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002036 {
Jamie Madill91445bc2015-09-23 16:47:53 -04002037 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002038 d3dUniform->vsRegisterIndex = reg;
2039 }
Jamie Madillfb536032015-09-11 13:19:49 -04002040
2041 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002042 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002043 {
2044 encoder->exitAggregateType();
2045 }
2046 }
2047}
2048
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002049template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002050void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002051{
Jamie Madill334d6152015-10-22 14:00:28 -04002052 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002053 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
2054
Jamie Madill62d31cb2015-09-11 13:25:51 -04002055 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002056
Jamie Madill62d31cb2015-09-11 13:25:51 -04002057 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002058 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002059 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002060
2061 if (targetUniform->type == targetUniformType)
2062 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002063 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002064
Jamie Madill62d31cb2015-09-11 13:25:51 -04002065 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002066 {
Jamie Madill334d6152015-10-22 14:00:28 -04002067 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002068 const T *source = v + (i * components);
2069
2070 for (int c = 0; c < components; c++)
2071 {
2072 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
2073 }
2074 for (int c = components; c < 4; c++)
2075 {
2076 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
2077 }
2078 }
2079 }
2080 else if (targetUniform->type == targetBoolType)
2081 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002082 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002083
Jamie Madill62d31cb2015-09-11 13:25:51 -04002084 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002085 {
Jamie Madill334d6152015-10-22 14:00:28 -04002086 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002087 const T *source = v + (i * components);
2088
2089 for (int c = 0; c < components; c++)
2090 {
Jamie Madill334d6152015-10-22 14:00:28 -04002091 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
2092 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002093 }
2094 for (int c = components; c < 4; c++)
2095 {
2096 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2097 }
2098 }
2099 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002100 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002101 {
2102 ASSERT(targetUniformType == GL_INT);
2103
Jamie Madill62d31cb2015-09-11 13:25:51 -04002104 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002105
2106 bool wasDirty = targetUniform->dirty;
2107
Jamie Madill62d31cb2015-09-11 13:25:51 -04002108 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002109 {
Jamie Madill334d6152015-10-22 14:00:28 -04002110 GLint *dest = target + (i * 4);
2111 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002112
2113 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2114 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2115 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2116 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2117 }
2118
2119 if (!wasDirty && targetUniform->dirty)
2120 {
2121 mDirtySamplerMapping = true;
2122 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002123 }
Jamie Madill334d6152015-10-22 14:00:28 -04002124 else
2125 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002126}
2127
2128template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002129void ProgramD3D::setUniformMatrixfv(GLint location,
2130 GLsizei countIn,
2131 GLboolean transpose,
2132 const GLfloat *value,
2133 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002134{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002135 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002136
Jamie Madill62d31cb2015-09-11 13:25:51 -04002137 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002138 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002139 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002140
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002141 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002142 GLfloat *target =
2143 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002144
Jamie Madill62d31cb2015-09-11 13:25:51 -04002145 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002146 {
2147 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2148 if (transpose == GL_FALSE)
2149 {
Jamie Madill334d6152015-10-22 14:00:28 -04002150 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2151 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002152 }
2153 else
2154 {
Jamie Madill334d6152015-10-22 14:00:28 -04002155 targetUniform->dirty =
2156 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002157 }
2158 target += targetMatrixStride;
2159 value += cols * rows;
2160 }
2161}
2162
Jamie Madill4a3c2342015-10-08 12:58:45 -04002163size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002164{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002165 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002166
Jamie Madill62d31cb2015-09-11 13:25:51 -04002167 // define member uniforms
2168 sh::Std140BlockEncoder std140Encoder;
2169 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2170 sh::BlockLayoutEncoder *encoder = nullptr;
2171
2172 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002173 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002174 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002175 }
2176 else
2177 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002178 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002179 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002180
Jamie Madill39046162016-02-08 15:05:17 -05002181 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2182 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002183
2184 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002185}
2186
Jamie Madill62d31cb2015-09-11 13:25:51 -04002187void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002188{
Olli Etuaho96963162016-03-21 11:54:33 +02002189 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002190 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002191 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002192 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002193 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002194 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002195 }
2196}
2197
Olli Etuaho96963162016-03-21 11:54:33 +02002198void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002199{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002200 ASSERT(d3dUniform->isSampler());
Jamie Madill48ef11b2016-04-27 15:21:52 -04002201 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2202 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Olli Etuaho96963162016-03-21 11:54:33 +02002203 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) || fragmentShaderD3D->hasUniform(d3dUniform));
2204 if (vertexShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002205 {
Olli Etuaho96963162016-03-21 11:54:33 +02002206 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2207 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002208 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2209 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002210 }
Olli Etuaho96963162016-03-21 11:54:33 +02002211 if (fragmentShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002212 {
Olli Etuaho96963162016-03-21 11:54:33 +02002213 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2214 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002215 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2216 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002217 }
2218}
2219
Jamie Madill62d31cb2015-09-11 13:25:51 -04002220// static
2221void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002222 GLenum samplerType,
2223 unsigned int samplerCount,
2224 std::vector<Sampler> &outSamplers,
2225 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002226{
2227 unsigned int samplerIndex = startSamplerIndex;
2228
2229 do
2230 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002231 ASSERT(samplerIndex < outSamplers.size());
2232 Sampler *sampler = &outSamplers[samplerIndex];
2233 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002234 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002235 sampler->logicalTextureUnit = 0;
2236 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002237 samplerIndex++;
2238 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002239}
2240
Brandon Jonesc9610c52014-08-25 17:02:59 -07002241void ProgramD3D::reset()
2242{
Brandon Joneseb994362014-09-24 10:27:28 -07002243 SafeDeleteContainer(mVertexExecutables);
2244 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002245
2246 for (auto &element : mGeometryExecutables)
2247 {
2248 SafeDelete(element);
2249 }
Brandon Joneseb994362014-09-24 10:27:28 -07002250
Brandon Jones22502d52014-08-29 16:58:36 -07002251 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002252 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002253
2254 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002255 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002256 mUsesFragDepth = false;
2257 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002258 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002259 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002260
Jamie Madill62d31cb2015-09-11 13:25:51 -04002261 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002262 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002263
Brandon Jonesc9610c52014-08-25 17:02:59 -07002264 SafeDelete(mVertexUniformStorage);
2265 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002266
2267 mSamplersPS.clear();
2268 mSamplersVS.clear();
2269
2270 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002271 mUsedPixelSamplerRange = 0;
2272 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002273
Jamie Madill8047c0d2016-03-07 13:02:12 -05002274 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002275
Jamie Madill9fc36822015-11-18 13:08:07 -05002276 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002277
2278 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002279}
2280
Geoff Lang7dd2e102014-11-10 15:19:26 -05002281unsigned int ProgramD3D::getSerial() const
2282{
2283 return mSerial;
2284}
2285
2286unsigned int ProgramD3D::issueSerial()
2287{
2288 return mCurrentSerial++;
2289}
2290
Jamie Madill8047c0d2016-03-07 13:02:12 -05002291void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002292{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002293 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002294 ASSERT(vertexShader != nullptr);
2295
2296 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002297 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002298 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002299 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2300 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002301
Jamie Madill8047c0d2016-03-07 13:02:12 -05002302 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002303 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002304 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002305 }
2306 }
Jamie Madill437d2662014-12-05 14:23:35 -05002307}
2308
Jamie Madill63805b42015-08-25 13:17:39 -04002309void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002310{
Jamie Madillbd136f92015-08-10 14:51:37 -04002311 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002312 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002313
Jamie Madill01074252016-11-28 15:55:51 -05002314 for (unsigned int locationIndex : IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002315 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002316 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002317
Jamie Madill8047c0d2016-03-07 13:02:12 -05002318 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002319 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002320 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002321 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002322 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002323 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002324 mCachedInputLayout[d3dSemantic] =
2325 GetVertexFormatType(vertexAttributes[locationIndex],
2326 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002327 }
2328 }
2329}
2330
Jamie Madill9fc36822015-11-18 13:08:07 -05002331void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002332{
Jamie Madill9fc36822015-11-18 13:08:07 -05002333 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2334
2335 const std::string &varyingSemantic =
2336 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2337
Jamie Madillccdf74b2015-08-18 10:46:12 -04002338 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002339 mStreamOutVaryings.clear();
2340
Jamie Madill48ef11b2016-04-27 15:21:52 -04002341 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002342 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2343 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002344 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002345 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2346 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002347 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002348 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002349 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002350 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2351 builtins.glPosition.index, 4, outputSlot));
2352 }
2353 }
2354 else if (tfVaryingName == "gl_FragCoord")
2355 {
2356 if (builtins.glFragCoord.enabled)
2357 {
2358 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2359 builtins.glFragCoord.index, 4, outputSlot));
2360 }
2361 }
2362 else if (tfVaryingName == "gl_PointSize")
2363 {
2364 if (builtins.glPointSize.enabled)
2365 {
2366 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2367 }
2368 }
2369 else
2370 {
2371 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2372 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002373 const auto &varying = *registerInfo.packedVarying->varying;
2374 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002375 int componentCount = gl::VariableColumnCount(transposedType);
2376 ASSERT(!varying.isBuiltIn());
2377
Jamie Madill55c25d02015-11-18 13:08:08 -05002378 // Transform feedback for varying structs is underspecified.
2379 // See Khronos bug 9856.
2380 // TODO(jmadill): Figure out how to be spec-compliant here.
2381 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2382 continue;
2383
Jamie Madill9fc36822015-11-18 13:08:07 -05002384 // There can be more than one register assigned to a particular varying, and each
2385 // register needs its own stream out entry.
2386 if (tfVaryingName == varying.name)
2387 {
2388 mStreamOutVaryings.push_back(D3DVarying(
2389 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2390 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002391 }
2392 }
2393 }
2394}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002395
2396D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2397{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002398 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002399}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002400
2401bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2402{
2403 std::string baseName = blockName;
2404 gl::ParseAndStripArrayIndex(&baseName);
2405
2406 auto sizeIter = mBlockDataSizes.find(baseName);
2407 if (sizeIter == mBlockDataSizes.end())
2408 {
2409 *sizeOut = 0;
2410 return false;
2411 }
2412
2413 *sizeOut = sizeIter->second;
2414 return true;
2415}
2416
2417bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2418 sh::BlockMemberInfo *memberInfoOut) const
2419{
2420 auto infoIter = mBlockInfo.find(memberUniformName);
2421 if (infoIter == mBlockInfo.end())
2422 {
2423 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2424 return false;
2425 }
2426
2427 *memberInfoOut = infoIter->second;
2428 return true;
2429}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002430
2431void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2432 GLenum genMode,
2433 GLint components,
2434 const GLfloat *coeffs)
2435{
2436 UNREACHABLE();
2437}
2438
Jamie Madill8047c0d2016-03-07 13:02:12 -05002439} // namespace rx