blob: b9f6c0a52bad90846f9ea4eff24b7d4853b2d3ec [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
Dian Xianga4928832015-09-15 10:11:17 -070011#include "common/BitSetIterator.h"
Jamie Madill437d2662014-12-05 14:23:35 -050012#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Framebuffer.h"
14#include "libANGLE/FramebufferAttachment.h"
15#include "libANGLE/Program.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040016#include "libANGLE/Uniform.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040017#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000018#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050019#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050020#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050021#include "libANGLE/renderer/d3d/RendererD3D.h"
22#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050023#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050024#include "libANGLE/renderer/d3d/VertexDataManager.h"
Jamie Madill120040e2016-12-07 14:46:16 -050025#include "libANGLE/renderer/d3d/hlsl/VaryingPacking.h"
Geoff Lang22072132014-11-20 15:15:01 -050026
Jamie Madill01074252016-11-28 15:55:51 -050027using namespace angle;
28
Brandon Jonesc9610c52014-08-25 17:02:59 -070029namespace rx
30{
31
Brandon Joneseb994362014-09-24 10:27:28 -070032namespace
33{
34
Jamie Madillf8dd7b12015-08-05 13:50:08 -040035gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070036{
Jamie Madillbd136f92015-08-10 14:51:37 -040037 gl::InputLayout defaultLayout;
38 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070039 {
Brandon Joneseb994362014-09-24 10:27:28 -070040 if (shaderAttr.type != GL_NONE)
41 {
42 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
43
Jamie Madilld3dfda22015-07-06 08:28:49 -040044 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040045 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070046 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040047 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040048 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040049 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040050 gl::VertexFormatType defaultType =
51 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070052
Jamie Madillbd136f92015-08-10 14:51:37 -040053 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070054 }
55 }
56 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040057
58 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070059}
60
Jamie Madill334d6152015-10-22 14:00:28 -040061std::vector<GLenum> GetDefaultOutputLayoutFromShader(
62 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070063{
Jamie Madillb4463142014-12-19 14:56:54 -050064 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070065
Jamie Madillb4463142014-12-19 14:56:54 -050066 if (!shaderOutputVars.empty())
67 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070068 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
69 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050070 }
Brandon Joneseb994362014-09-24 10:27:28 -070071
72 return defaultPixelOutput;
73}
74
Brandon Jones1a8a7e32014-10-01 12:49:30 -070075bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
76{
77 return var.isRowMajorLayout;
78}
79
80bool IsRowMajorLayout(const sh::ShaderVariable &var)
81{
82 return false;
83}
84
Jamie Madill9fc36822015-11-18 13:08:07 -050085// true if varying x has a higher priority in packing than y
86bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
87{
Jamie Madill55c25d02015-11-18 13:08:08 -050088 return gl::CompareShaderVar(*x.varying, *y.varying);
Jamie Madill9fc36822015-11-18 13:08:07 -050089}
90
Jamie Madillca03b352015-09-02 12:38:13 -040091std::vector<PackedVarying> MergeVaryings(const gl::Shader &vertexShader,
92 const gl::Shader &fragmentShader,
93 const std::vector<std::string> &tfVaryings)
Jamie Madillada9ecc2015-08-17 12:53:37 -040094{
Jamie Madillca03b352015-09-02 12:38:13 -040095 std::vector<PackedVarying> packedVaryings;
96
97 for (const sh::Varying &output : vertexShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -040098 {
Jamie Madillca03b352015-09-02 12:38:13 -040099 bool packed = false;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400100
101 // Built-in varyings obey special rules
Jamie Madillca03b352015-09-02 12:38:13 -0400102 if (output.isBuiltIn())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400103 {
104 continue;
105 }
106
Jamie Madillca03b352015-09-02 12:38:13 -0400107 for (const sh::Varying &input : fragmentShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400108 {
Jamie Madillca03b352015-09-02 12:38:13 -0400109 if (output.name == input.name)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400110 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500111 if (output.isStruct())
112 {
113 ASSERT(!output.isArray());
114 for (const auto &field : output.fields)
115 {
116 ASSERT(!field.isStruct() && !field.isArray());
117 packedVaryings.push_back(
118 PackedVarying(field, input.interpolation, input.name));
119 }
120 }
121 else
122 {
123 packedVaryings.push_back(PackedVarying(input, input.interpolation));
124 }
Jamie Madillca03b352015-09-02 12:38:13 -0400125 packed = true;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400126 break;
127 }
128 }
129
Jamie Madillca03b352015-09-02 12:38:13 -0400130 // Keep Transform FB varyings in the merged list always.
131 if (!packed)
132 {
133 for (const std::string &tfVarying : tfVaryings)
134 {
135 if (tfVarying == output.name)
136 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500137 // Transform feedback for varying structs is underspecified.
138 // See Khronos bug 9856.
139 // TODO(jmadill): Figure out how to be spec-compliant here.
140 if (!output.isStruct())
141 {
142 packedVaryings.push_back(PackedVarying(output, output.interpolation));
143 packedVaryings.back().vertexOnly = true;
144 }
Jamie Madillca03b352015-09-02 12:38:13 -0400145 break;
146 }
147 }
148 }
Jamie Madillada9ecc2015-08-17 12:53:37 -0400149 }
150
Jamie Madill9fc36822015-11-18 13:08:07 -0500151 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
152
Jamie Madillca03b352015-09-02 12:38:13 -0400153 return packedVaryings;
Brandon Joneseb994362014-09-24 10:27:28 -0700154}
155
Jamie Madill62d31cb2015-09-11 13:25:51 -0400156template <typename VarT>
157void GetUniformBlockInfo(const std::vector<VarT> &fields,
158 const std::string &prefix,
159 sh::BlockLayoutEncoder *encoder,
160 bool inRowMajorLayout,
161 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
162{
163 for (const VarT &field : fields)
164 {
165 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
166
167 if (field.isStruct())
168 {
169 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
170
171 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
172 {
173 encoder->enterAggregateType();
174
175 const std::string uniformElementName =
176 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
177 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
178 blockInfoOut);
179
180 encoder->exitAggregateType();
181 }
182 }
183 else
184 {
185 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
186 (*blockInfoOut)[fieldName] =
187 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
188 }
189 }
190}
191
Jamie Madill334d6152015-10-22 14:00:28 -0400192template <typename T>
Jacek Caban2302e692015-12-01 12:44:43 +0100193static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
194{
195 ASSERT(dest != NULL);
196 ASSERT(dirtyFlag != NULL);
197
198 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
199 *dest = source;
200}
201
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800202template <typename T, int cols, int rows>
203bool TransposeExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400204{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800205 constexpr int targetWidth = 4;
206 constexpr int targetHeight = rows;
207 constexpr int srcWidth = rows;
208 constexpr int srcHeight = cols;
209
210 constexpr int copyWidth = std::min(targetHeight, srcWidth);
211 constexpr int copyHeight = std::min(targetWidth, srcHeight);
212
213 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400214
215 for (int x = 0; x < copyWidth; x++)
216 {
217 for (int y = 0; y < copyHeight; y++)
218 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800219 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400220 }
221 }
222
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800223 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
224 {
225 return false;
226 }
227
228 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
229 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400230}
231
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800232template <typename T, int cols, int rows>
233bool ExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400234{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800235 constexpr int targetWidth = 4;
236 constexpr int targetHeight = rows;
237 constexpr int srcWidth = cols;
238 constexpr int srcHeight = rows;
239
240 constexpr int copyWidth = std::min(targetWidth, srcWidth);
241 constexpr int copyHeight = std::min(targetHeight, srcHeight);
242
243 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400244
245 for (int y = 0; y < copyHeight; y++)
246 {
247 for (int x = 0; x < copyWidth; x++)
248 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800249 staging[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400250 }
251 }
252
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800253 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
254 {
255 return false;
256 }
257
258 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
259 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400260}
261
Jamie Madill4e31ad52015-10-29 10:32:57 -0400262gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
263{
264 switch (drawMode)
265 {
266 // Uses the point sprite geometry shader.
267 case GL_POINTS:
268 return gl::PRIMITIVE_POINTS;
269
270 // All line drawing uses the same geometry shader.
271 case GL_LINES:
272 case GL_LINE_STRIP:
273 case GL_LINE_LOOP:
274 return gl::PRIMITIVE_LINES;
275
276 // The triangle fan primitive is emulated with strips in D3D11.
277 case GL_TRIANGLES:
278 case GL_TRIANGLE_FAN:
279 return gl::PRIMITIVE_TRIANGLES;
280
281 // Special case for triangle strips.
282 case GL_TRIANGLE_STRIP:
283 return gl::PRIMITIVE_TRIANGLE_STRIP;
284
285 default:
286 UNREACHABLE();
287 return gl::PRIMITIVE_TYPE_MAX;
288 }
289}
290
Jamie Madillada9ecc2015-08-17 12:53:37 -0400291} // anonymous namespace
292
Jamie Madill28afae52015-11-09 15:07:57 -0500293// D3DUniform Implementation
294
Jamie Madill62d31cb2015-09-11 13:25:51 -0400295D3DUniform::D3DUniform(GLenum typeIn,
296 const std::string &nameIn,
297 unsigned int arraySizeIn,
298 bool defaultBlock)
299 : type(typeIn),
300 name(nameIn),
301 arraySize(arraySizeIn),
302 data(nullptr),
303 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400304 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400305 psRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400306 registerCount(0),
307 registerElement(0)
308{
309 // We use data storage for default block uniforms to cache values that are sent to D3D during
310 // rendering
311 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
312 if (defaultBlock)
313 {
314 size_t bytes = gl::VariableInternalSize(type) * elementCount();
315 data = new uint8_t[bytes];
316 memset(data, 0, bytes);
317
318 // TODO(jmadill): is this correct with non-square matrices?
319 registerCount = gl::VariableRowCount(type) * elementCount();
320 }
321}
322
323D3DUniform::~D3DUniform()
324{
325 SafeDeleteArray(data);
326}
327
328bool D3DUniform::isSampler() const
329{
330 return gl::IsSamplerType(type);
331}
332
333bool D3DUniform::isReferencedByVertexShader() const
334{
335 return vsRegisterIndex != GL_INVALID_INDEX;
336}
337
338bool D3DUniform::isReferencedByFragmentShader() const
339{
340 return psRegisterIndex != GL_INVALID_INDEX;
341}
342
Jamie Madill28afae52015-11-09 15:07:57 -0500343// D3DVarying Implementation
344
Jamie Madill9fc36822015-11-18 13:08:07 -0500345D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500346{
347}
348
Jamie Madill9fc36822015-11-18 13:08:07 -0500349D3DVarying::D3DVarying(const std::string &semanticNameIn,
350 unsigned int semanticIndexIn,
351 unsigned int componentCountIn,
352 unsigned int outputSlotIn)
353 : semanticName(semanticNameIn),
354 semanticIndex(semanticIndexIn),
355 componentCount(componentCountIn),
356 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500357{
358}
359
Jamie Madille39a3f02015-11-17 20:42:15 -0500360// ProgramD3DMetadata Implementation
361
Jamie Madillc9bde922016-07-24 17:58:50 -0400362ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500363 const ShaderD3D *vertexShader,
364 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400365 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
366 mShaderModelSuffix(renderer->getShaderModelSuffix()),
367 mUsesInstancedPointSpriteEmulation(
368 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
369 mUsesViewScale(renderer->presentPathFastEnabled()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500370 mVertexShader(vertexShader),
371 mFragmentShader(fragmentShader)
372{
373}
374
375int ProgramD3DMetadata::getRendererMajorShaderModel() const
376{
377 return mRendererMajorShaderModel;
378}
379
Jamie Madill9082b982016-04-27 15:21:51 -0400380bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500381{
Martin Radev1be913c2016-07-11 17:59:16 +0300382 return (mFragmentShader->usesFragColor() && data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500383}
384
Jamie Madill48ef11b2016-04-27 15:21:52 -0400385bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500386{
Jamie Madill63286672015-11-24 13:00:08 -0500387 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500388}
389
390bool ProgramD3DMetadata::usesPointCoord() const
391{
392 return mFragmentShader->usesPointCoord();
393}
394
395bool ProgramD3DMetadata::usesFragCoord() const
396{
397 return mFragmentShader->usesFragCoord();
398}
399
400bool ProgramD3DMetadata::usesPointSize() const
401{
402 return mVertexShader->usesPointSize();
403}
404
405bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
406{
Jamie Madillc9bde922016-07-24 17:58:50 -0400407 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
408 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500409}
410
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800411bool ProgramD3DMetadata::usesViewScale() const
412{
413 return mUsesViewScale;
414}
415
Jamie Madille39a3f02015-11-17 20:42:15 -0500416bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
417{
Jamie Madillc9bde922016-07-24 17:58:50 -0400418 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500419 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400420 // Even with a geometry shader, the app can render triangles or lines and reference
421 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
422 // simplicity, we always add this to the vertex shader when the fragment shader
423 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500424 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
425 usesInsertedPointCoordValue();
426}
427
428bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
429{
430 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
431 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
432 // the vertex shader in this case. This saves us 1 output vector.
433 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
434}
435
436bool ProgramD3DMetadata::usesSystemValuePointSize() const
437{
438 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
439}
440
441bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
442{
443 return mFragmentShader->usesMultipleRenderTargets();
444}
445
446GLint ProgramD3DMetadata::getMajorShaderVersion() const
447{
448 return mVertexShader->getData().getShaderVersion();
449}
450
451const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
452{
453 return mFragmentShader;
454}
455
Jamie Madill120040e2016-12-07 14:46:16 -0500456void ProgramD3DMetadata::updatePackingBuiltins(ShaderType shaderType, VaryingPacking *packing)
457{
458 const std::string &userSemantic =
459 GetVaryingSemantic(mRendererMajorShaderModel, usesSystemValuePointSize());
460
461 unsigned int reservedSemanticIndex = packing->getMaxSemanticIndex();
462
463 VaryingPacking::BuiltinInfo *builtins = &packing->builtins(shaderType);
464
465 if (mRendererMajorShaderModel >= 4)
466 {
467 builtins->dxPosition.enableSystem("SV_Position");
468 }
469 else if (shaderType == SHADER_PIXEL)
470 {
471 builtins->dxPosition.enableSystem("VPOS");
472 }
473 else
474 {
475 builtins->dxPosition.enableSystem("POSITION");
476 }
477
478 if (usesTransformFeedbackGLPosition())
479 {
480 builtins->glPosition.enable(userSemantic, reservedSemanticIndex++);
481 }
482
483 if (usesFragCoord())
484 {
485 builtins->glFragCoord.enable(userSemantic, reservedSemanticIndex++);
486 }
487
488 if (shaderType == SHADER_VERTEX ? addsPointCoordToVertexShader() : usesPointCoord())
489 {
490 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
491 // In D3D11 we manually compute gl_PointCoord in the GS.
492 if (mRendererMajorShaderModel >= 4)
493 {
494 builtins->glPointCoord.enable(userSemantic, reservedSemanticIndex++);
495 }
496 else
497 {
498 builtins->glPointCoord.enable("TEXCOORD", 0);
499 }
500 }
501
502 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
503 if (usesSystemValuePointSize() &&
504 (shaderType != SHADER_PIXEL || mRendererMajorShaderModel >= 4))
505 {
506 builtins->glPointSize.enableSystem("PSIZE");
507 }
508}
509
Jamie Madill28afae52015-11-09 15:07:57 -0500510// ProgramD3D Implementation
511
Jamie Madilld3dfda22015-07-06 08:28:49 -0400512ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
513 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500514 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400515 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700516{
Brandon Joneseb994362014-09-24 10:27:28 -0700517}
518
519ProgramD3D::VertexExecutable::~VertexExecutable()
520{
521 SafeDelete(mShaderExecutable);
522}
523
Jamie Madilld3dfda22015-07-06 08:28:49 -0400524// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500525ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
526 GLenum type)
527{
528 switch (type)
529 {
530 case GL_INT:
531 return HLSLAttribType::SIGNED_INT;
532 case GL_UNSIGNED_INT:
533 return HLSLAttribType::UNSIGNED_INT;
534 case GL_SIGNED_NORMALIZED:
535 case GL_UNSIGNED_NORMALIZED:
536 case GL_FLOAT:
537 return HLSLAttribType::FLOAT;
538 default:
539 UNREACHABLE();
540 return HLSLAttribType::FLOAT;
541 }
542}
543
544// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400545void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
546 const gl::InputLayout &inputLayout,
547 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700548{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500549 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400550
551 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700552 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400553 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500554 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
555 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400556
Jamie Madillbdec2f42016-03-02 16:35:32 -0500557 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
558 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
559 continue;
560
561 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
562 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700563 }
Brandon Joneseb994362014-09-24 10:27:28 -0700564}
565
Jamie Madilld3dfda22015-07-06 08:28:49 -0400566bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
567{
Jamie Madillbd136f92015-08-10 14:51:37 -0400568 size_t limit = std::max(mSignature.size(), signature.size());
569 for (size_t index = 0; index < limit; ++index)
570 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500571 // treat undefined indexes as FLOAT
572 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
573 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400574 if (a != b)
575 return false;
576 }
577
578 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400579}
580
581ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
582 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400583 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700584{
585}
586
587ProgramD3D::PixelExecutable::~PixelExecutable()
588{
589 SafeDelete(mShaderExecutable);
590}
591
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700592ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
593{
594}
595
Geoff Lang7dd2e102014-11-10 15:19:26 -0500596unsigned int ProgramD3D::mCurrentSerial = 1;
597
Jamie Madill48ef11b2016-04-27 15:21:52 -0400598ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
599 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700600 mRenderer(renderer),
601 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400602 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700603 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400604 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700605 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700606 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700607 mUsedVertexSamplerRange(0),
608 mUsedPixelSamplerRange(0),
609 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500610 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700611{
Brandon Joneseb994362014-09-24 10:27:28 -0700612 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700613}
614
615ProgramD3D::~ProgramD3D()
616{
617 reset();
618 SafeDelete(mDynamicHLSL);
619}
620
Brandon Jones44151a92014-09-10 11:32:25 -0700621bool ProgramD3D::usesPointSpriteEmulation() const
622{
623 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
624}
625
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400626bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700627{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400628 if (drawMode != GL_POINTS)
629 {
630 return mUsesFlatInterpolation;
631 }
632
Cooper Partine6664f02015-01-09 16:22:24 -0800633 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
634}
635
636bool ProgramD3D::usesInstancedPointSpriteEmulation() const
637{
638 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700639}
640
Jamie Madill334d6152015-10-22 14:00:28 -0400641GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
642 unsigned int samplerIndex,
643 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700644{
645 GLint logicalTextureUnit = -1;
646
647 switch (type)
648 {
Jamie Madill334d6152015-10-22 14:00:28 -0400649 case gl::SAMPLER_PIXEL:
650 ASSERT(samplerIndex < caps.maxTextureImageUnits);
651 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
652 {
653 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
654 }
655 break;
656 case gl::SAMPLER_VERTEX:
657 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
658 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
659 {
660 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
661 }
662 break;
663 default:
664 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700665 }
666
Jamie Madill334d6152015-10-22 14:00:28 -0400667 if (logicalTextureUnit >= 0 &&
668 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700669 {
670 return logicalTextureUnit;
671 }
672
673 return -1;
674}
675
676// Returns the texture type for a given Direct3D 9 sampler type and
677// index (0-15 for the pixel shader and 0-3 for the vertex shader).
678GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
679{
680 switch (type)
681 {
Jamie Madill334d6152015-10-22 14:00:28 -0400682 case gl::SAMPLER_PIXEL:
683 ASSERT(samplerIndex < mSamplersPS.size());
684 ASSERT(mSamplersPS[samplerIndex].active);
685 return mSamplersPS[samplerIndex].textureType;
686 case gl::SAMPLER_VERTEX:
687 ASSERT(samplerIndex < mSamplersVS.size());
688 ASSERT(mSamplersVS[samplerIndex].active);
689 return mSamplersVS[samplerIndex].textureType;
690 default:
691 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700692 }
693
694 return GL_TEXTURE_2D;
695}
696
Olli Etuaho618bebc2016-01-15 16:40:00 +0200697GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700698{
699 switch (type)
700 {
Jamie Madill334d6152015-10-22 14:00:28 -0400701 case gl::SAMPLER_PIXEL:
702 return mUsedPixelSamplerRange;
703 case gl::SAMPLER_VERTEX:
704 return mUsedVertexSamplerRange;
705 default:
706 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200707 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700708 }
709}
710
711void ProgramD3D::updateSamplerMapping()
712{
713 if (!mDirtySamplerMapping)
714 {
715 return;
716 }
717
718 mDirtySamplerMapping = false;
719
720 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400721 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700722 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400723 if (!d3dUniform->dirty)
724 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700725
Jamie Madill62d31cb2015-09-11 13:25:51 -0400726 if (!d3dUniform->isSampler())
727 continue;
728
729 int count = d3dUniform->elementCount();
730 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
731
732 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700733 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400734 unsigned int firstIndex = d3dUniform->psRegisterIndex;
735
736 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700737 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400738 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700739
Jamie Madill62d31cb2015-09-11 13:25:51 -0400740 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700741 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400742 ASSERT(mSamplersPS[samplerIndex].active);
743 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700744 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400745 }
746 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700747
Jamie Madill62d31cb2015-09-11 13:25:51 -0400748 if (d3dUniform->isReferencedByVertexShader())
749 {
750 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
751
752 for (int i = 0; i < count; i++)
753 {
754 unsigned int samplerIndex = firstIndex + i;
755
756 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700757 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400758 ASSERT(mSamplersVS[samplerIndex].active);
759 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700760 }
761 }
762 }
763 }
764}
765
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500766LinkResult ProgramD3D::load(const ContextImpl *contextImpl,
767 gl::InfoLog &infoLog,
768 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700769{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500770 // TODO(jmadill): Use Renderer from contextImpl.
771
Jamie Madill62d31cb2015-09-11 13:25:51 -0400772 reset();
773
Jamie Madill334d6152015-10-22 14:00:28 -0400774 DeviceIdentifier binaryDeviceIdentifier = {0};
775 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
776 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700777
778 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
779 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
780 {
781 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500782 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700783 }
784
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500785 int compileFlags = stream->readInt<int>();
786 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
787 {
Jamie Madillf6113162015-05-07 11:49:21 -0400788 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500789 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500790 }
791
Jamie Madill8047c0d2016-03-07 13:02:12 -0500792 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400793 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500794 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400795 }
796
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700797 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
798 for (unsigned int i = 0; i < psSamplerCount; ++i)
799 {
800 Sampler sampler;
801 stream->readBool(&sampler.active);
802 stream->readInt(&sampler.logicalTextureUnit);
803 stream->readInt(&sampler.textureType);
804 mSamplersPS.push_back(sampler);
805 }
806 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
807 for (unsigned int i = 0; i < vsSamplerCount; ++i)
808 {
809 Sampler sampler;
810 stream->readBool(&sampler.active);
811 stream->readInt(&sampler.logicalTextureUnit);
812 stream->readInt(&sampler.textureType);
813 mSamplersVS.push_back(sampler);
814 }
815
816 stream->readInt(&mUsedVertexSamplerRange);
817 stream->readInt(&mUsedPixelSamplerRange);
818
819 const unsigned int uniformCount = stream->readInt<unsigned int>();
820 if (stream->error())
821 {
Jamie Madillf6113162015-05-07 11:49:21 -0400822 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500823 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700824 }
825
Jamie Madill48ef11b2016-04-27 15:21:52 -0400826 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400827 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700828 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
829 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400830 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700831
Jamie Madill62d31cb2015-09-11 13:25:51 -0400832 D3DUniform *d3dUniform =
833 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
834 linkedUniform.isInDefaultBlock());
835 stream->readInt(&d3dUniform->psRegisterIndex);
836 stream->readInt(&d3dUniform->vsRegisterIndex);
837 stream->readInt(&d3dUniform->registerCount);
838 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700839
Jamie Madill62d31cb2015-09-11 13:25:51 -0400840 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700841 }
842
Jamie Madill4a3c2342015-10-08 12:58:45 -0400843 const unsigned int blockCount = stream->readInt<unsigned int>();
844 if (stream->error())
845 {
846 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500847 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400848 }
849
850 ASSERT(mD3DUniformBlocks.empty());
851 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
852 {
853 D3DUniformBlock uniformBlock;
854 stream->readInt(&uniformBlock.psRegisterIndex);
855 stream->readInt(&uniformBlock.vsRegisterIndex);
856 mD3DUniformBlocks.push_back(uniformBlock);
857 }
858
Jamie Madill9fc36822015-11-18 13:08:07 -0500859 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
860 mStreamOutVaryings.resize(streamOutVaryingCount);
861 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700862 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500863 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700864
Jamie Madill28afae52015-11-09 15:07:57 -0500865 stream->readString(&varying->semanticName);
866 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500867 stream->readInt(&varying->componentCount);
868 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700869 }
870
Brandon Jones22502d52014-08-29 16:58:36 -0700871 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400872 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500873 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700874 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400875 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500876 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700877 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700878 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400879 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700880
881 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
882 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400883 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
884 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700885 {
886 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
887 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
888 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
889 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
890 }
891
Jamie Madill4e31ad52015-10-29 10:32:57 -0400892 stream->readString(&mGeometryShaderPreamble);
893
Jamie Madill334d6152015-10-22 14:00:28 -0400894 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700895
Jamie Madillb0a838b2016-11-13 20:02:12 -0500896 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
897
Brandon Joneseb994362014-09-24 10:27:28 -0700898 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400899 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
900 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700901 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400902 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400903 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700904
Jamie Madilld3dfda22015-07-06 08:28:49 -0400905 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700906 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400907 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700908 }
909
Jamie Madill334d6152015-10-22 14:00:28 -0400910 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700911 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400912
Jamie Madillada9ecc2015-08-17 12:53:37 -0400913 ShaderExecutableD3D *shaderExecutable = nullptr;
914
Jamie Madillb0a838b2016-11-13 20:02:12 -0500915 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
916 mStreamOutVaryings, separateAttribs,
917 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400918
Brandon Joneseb994362014-09-24 10:27:28 -0700919 if (!shaderExecutable)
920 {
Jamie Madillf6113162015-05-07 11:49:21 -0400921 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500922 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700923 }
924
925 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400926 VertexExecutable::Signature signature;
927 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700928
929 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400930 mVertexExecutables.push_back(
931 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700932
933 stream->skip(vertexShaderSize);
934 }
935
936 const size_t pixelShaderCount = stream->readInt<unsigned int>();
937 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
938 {
939 const size_t outputCount = stream->readInt<unsigned int>();
940 std::vector<GLenum> outputs(outputCount);
941 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
942 {
943 stream->readInt(&outputs[outputIndex]);
944 }
945
Jamie Madill334d6152015-10-22 14:00:28 -0400946 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700947 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400948 ShaderExecutableD3D *shaderExecutable = nullptr;
949
Jamie Madillb0a838b2016-11-13 20:02:12 -0500950 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
951 mStreamOutVaryings, separateAttribs,
952 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700953
954 if (!shaderExecutable)
955 {
Jamie Madillf6113162015-05-07 11:49:21 -0400956 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500957 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700958 }
959
960 // add new binary
961 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
962
963 stream->skip(pixelShaderSize);
964 }
965
Jamie Madill4e31ad52015-10-29 10:32:57 -0400966 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
967 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700968 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400969 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
970 if (geometryShaderSize == 0)
971 {
972 mGeometryExecutables[geometryExeIndex] = nullptr;
973 continue;
974 }
975
Brandon Joneseb994362014-09-24 10:27:28 -0700976 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400977
Jamie Madillb0a838b2016-11-13 20:02:12 -0500978 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
979 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
980 &mGeometryExecutables[geometryExeIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -0700981
Jamie Madill4e31ad52015-10-29 10:32:57 -0400982 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700983 {
Jamie Madillf6113162015-05-07 11:49:21 -0400984 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500985 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700986 }
987 stream->skip(geometryShaderSize);
988 }
989
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700990 initializeUniformStorage();
991
Jamie Madillb0a838b2016-11-13 20:02:12 -0500992 return true;
Brandon Jones22502d52014-08-29 16:58:36 -0700993}
994
Geoff Langb543aff2014-09-30 14:52:54 -0400995gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700996{
Austin Kinross137b1512015-06-17 16:14:53 -0700997 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400998 // When we load the binary again later, we can validate the device identifier before trying to
999 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -07001000 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -04001001 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
1002 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -07001003
Jamie Madill2db1fbb2014-12-03 10:58:55 -05001004 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1005
Jamie Madill8047c0d2016-03-07 13:02:12 -05001006 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -04001007 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05001008 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -04001009 }
1010
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001011 stream->writeInt(mSamplersPS.size());
1012 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
1013 {
1014 stream->writeInt(mSamplersPS[i].active);
1015 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
1016 stream->writeInt(mSamplersPS[i].textureType);
1017 }
1018
1019 stream->writeInt(mSamplersVS.size());
1020 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
1021 {
1022 stream->writeInt(mSamplersVS[i].active);
1023 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
1024 stream->writeInt(mSamplersVS[i].textureType);
1025 }
1026
1027 stream->writeInt(mUsedVertexSamplerRange);
1028 stream->writeInt(mUsedPixelSamplerRange);
1029
Jamie Madill62d31cb2015-09-11 13:25:51 -04001030 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001031 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001032 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001033 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001034 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1035 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001036 stream->writeInt(uniform->registerCount);
1037 stream->writeInt(uniform->registerElement);
1038 }
1039
1040 stream->writeInt(mD3DUniformBlocks.size());
1041 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1042 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001043 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1044 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001045 }
1046
Jamie Madill9fc36822015-11-18 13:08:07 -05001047 stream->writeInt(mStreamOutVaryings.size());
1048 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001049 {
Brandon Joneseb994362014-09-24 10:27:28 -07001050 stream->writeString(varying.semanticName);
1051 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001052 stream->writeInt(varying.componentCount);
1053 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001054 }
1055
Brandon Jones22502d52014-08-29 16:58:36 -07001056 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001057 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001058 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001059 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001060 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001061 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001062 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001063 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001064 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001065
Brandon Joneseb994362014-09-24 10:27:28 -07001066 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001067 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001068 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1069 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001070 {
Brandon Joneseb994362014-09-24 10:27:28 -07001071 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001072 stream->writeInt(variable.type);
1073 stream->writeString(variable.name);
1074 stream->writeString(variable.source);
1075 stream->writeInt(variable.outputIndex);
1076 }
1077
Jamie Madill4e31ad52015-10-29 10:32:57 -04001078 stream->writeString(mGeometryShaderPreamble);
1079
Brandon Joneseb994362014-09-24 10:27:28 -07001080 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001081 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1082 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001083 {
1084 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1085
Jamie Madilld3dfda22015-07-06 08:28:49 -04001086 const auto &inputLayout = vertexExecutable->inputs();
1087 stream->writeInt(inputLayout.size());
1088
1089 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001090 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001091 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001092 }
1093
1094 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1095 stream->writeInt(vertexShaderSize);
1096
1097 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1098 stream->writeBytes(vertexBlob, vertexShaderSize);
1099 }
1100
1101 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001102 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1103 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001104 {
1105 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1106
1107 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1108 stream->writeInt(outputs.size());
1109 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1110 {
1111 stream->writeInt(outputs[outputIndex]);
1112 }
1113
1114 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1115 stream->writeInt(pixelShaderSize);
1116
1117 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1118 stream->writeBytes(pixelBlob, pixelShaderSize);
1119 }
1120
Jamie Madill4e31ad52015-10-29 10:32:57 -04001121 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001122 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001123 if (geometryExe == nullptr)
1124 {
1125 stream->writeInt(0);
1126 continue;
1127 }
1128
1129 size_t geometryShaderSize = geometryExe->getLength();
1130 stream->writeInt(geometryShaderSize);
1131 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001132 }
1133
Geoff Langb543aff2014-09-30 14:52:54 -04001134 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001135}
1136
Geoff Langc5629752015-12-07 16:29:04 -05001137void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1138{
1139}
1140
Jamie Madill334d6152015-10-22 14:00:28 -04001141gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1142 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001143{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001144 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001145
Jamie Madill85a18042015-03-05 15:41:41 -05001146 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001147 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001148
1149 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1150 {
1151 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1152
1153 if (colorbuffer)
1154 {
Jamie Madill334d6152015-10-22 14:00:28 -04001155 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1156 ? GL_COLOR_ATTACHMENT0
1157 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001158 }
1159 else
1160 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001161 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001162 }
1163 }
1164
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001165 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001166}
1167
Jamie Madill97399232014-12-23 12:31:15 -05001168gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001169 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001170 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001171{
1172 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1173 {
1174 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1175 {
Geoff Langb543aff2014-09-30 14:52:54 -04001176 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1177 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001178 }
1179 }
1180
Jamie Madill334d6152015-10-22 14:00:28 -04001181 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1182 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001183
1184 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001185 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001186
1187 gl::InfoLog tempInfoLog;
1188 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1189
Jamie Madill01074252016-11-28 15:55:51 -05001190 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001191 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001192 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001193 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001194
Jamie Madill97399232014-12-23 12:31:15 -05001195 if (pixelExecutable)
1196 {
1197 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1198 }
1199 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001200 {
1201 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001202 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001203 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1204 }
Brandon Jones22502d52014-08-29 16:58:36 -07001205
Geoff Langb543aff2014-09-30 14:52:54 -04001206 *outExectuable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001207 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001208}
1209
Jamie Madilld3dfda22015-07-06 08:28:49 -04001210gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001211 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001212 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001213{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001214 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001215
1216 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1217 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001218 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001219 {
Geoff Langb543aff2014-09-30 14:52:54 -04001220 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1221 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001222 }
1223 }
1224
Brandon Jones22502d52014-08-29 16:58:36 -07001225 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001226 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001227 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001228
1229 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001230 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001231
1232 gl::InfoLog tempInfoLog;
1233 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1234
Jamie Madill01074252016-11-28 15:55:51 -05001235 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001236 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001237 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001238 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001239
Jamie Madill97399232014-12-23 12:31:15 -05001240 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001241 {
Jamie Madill334d6152015-10-22 14:00:28 -04001242 mVertexExecutables.push_back(
1243 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001244 }
Jamie Madill97399232014-12-23 12:31:15 -05001245 else if (!infoLog)
1246 {
1247 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001248 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001249 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1250 }
Brandon Jones22502d52014-08-29 16:58:36 -07001251
Geoff Langb543aff2014-09-30 14:52:54 -04001252 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001253 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001254}
1255
Jamie Madill9082b982016-04-27 15:21:51 -04001256gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001257 GLenum drawMode,
1258 ShaderExecutableD3D **outExecutable,
1259 gl::InfoLog *infoLog)
1260{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001261 if (outExecutable)
1262 {
1263 *outExecutable = nullptr;
1264 }
1265
Austin Kinross88829e82016-01-12 13:04:41 -08001266 // Return a null shader if the current rendering doesn't use a geometry shader
1267 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001268 {
1269 return gl::Error(GL_NO_ERROR);
1270 }
1271
Jamie Madill4e31ad52015-10-29 10:32:57 -04001272 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1273
1274 if (mGeometryExecutables[geometryShaderType] != nullptr)
1275 {
1276 if (outExecutable)
1277 {
1278 *outExecutable = mGeometryExecutables[geometryShaderType];
1279 }
1280 return gl::Error(GL_NO_ERROR);
1281 }
1282
1283 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001284 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001285 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001286
1287 gl::InfoLog tempInfoLog;
1288 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1289
1290 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001291 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001292 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
1293 angle::CompilerWorkaroundsD3D(), &mGeometryExecutables[geometryShaderType]);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001294
1295 if (!infoLog && error.isError())
1296 {
1297 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1298 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1299 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1300 }
1301
1302 if (outExecutable)
1303 {
1304 *outExecutable = mGeometryExecutables[geometryShaderType];
1305 }
1306 return error;
1307}
1308
Jamie Madill01074252016-11-28 15:55:51 -05001309class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001310{
Jamie Madill01074252016-11-28 15:55:51 -05001311 public:
1312 GetExecutableTask(ProgramD3D *program)
1313 : mProgram(program), mError(GL_NO_ERROR), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001314 {
Brandon Joneseb994362014-09-24 10:27:28 -07001315 }
1316
Jamie Madill01074252016-11-28 15:55:51 -05001317 virtual gl::Error run() = 0;
1318
1319 void operator()() override { mError = run(); }
1320
1321 const gl::Error &getError() const { return mError; }
1322 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1323 ShaderExecutableD3D *getResult() { return mResult; }
1324
1325 protected:
1326 ProgramD3D *mProgram;
1327 gl::Error mError;
1328 gl::InfoLog mInfoLog;
1329 ShaderExecutableD3D *mResult;
1330};
1331
1332class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1333{
1334 public:
1335 GetVertexExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1336 gl::Error run() override
1337 {
1338 const auto &defaultInputLayout =
1339 GetDefaultInputLayoutFromShader(mProgram->mState.getAttachedVertexShader());
1340
1341 ANGLE_TRY(
1342 mProgram->getVertexExecutableForInputLayout(defaultInputLayout, &mResult, &mInfoLog));
1343
1344 return gl::NoError();
1345 }
1346};
1347
1348class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1349{
1350 public:
1351 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1352 gl::Error run() override
1353 {
1354 const auto &defaultPixelOutput =
1355 GetDefaultOutputLayoutFromShader(mProgram->getPixelShaderKey());
1356
1357 ANGLE_TRY(
1358 mProgram->getPixelExecutableForOutputLayout(defaultPixelOutput, &mResult, &mInfoLog));
1359
1360 return gl::NoError();
1361 }
1362};
1363
1364class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1365{
1366 public:
1367 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1368 : GetExecutableTask(program), mContextState(contextState)
1369 {
1370 }
1371
1372 gl::Error run() override
1373 {
1374 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1375 // D3D11.
1376 if (mProgram->usesGeometryShader(GL_POINTS))
1377 {
1378 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1379 &mResult, &mInfoLog));
1380 }
1381
1382 return gl::NoError();
1383 }
1384
1385 private:
1386 const gl::ContextState &mContextState;
1387};
1388
1389LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &contextState,
1390 gl::InfoLog &infoLog)
1391{
1392 // Ensure the compiler is initialized to avoid race conditions.
1393 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1394
1395 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1396
1397 GetVertexExecutableTask vertexTask(this);
1398 GetPixelExecutableTask pixelTask(this);
1399 GetGeometryExecutableTask geometryTask(this, contextState);
1400
1401 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1402 workerPool->postWorkerTask(&pixelTask),
1403 workerPool->postWorkerTask(&geometryTask)}};
1404
1405 WaitableEvent::WaitMany(&waitEvents);
1406
1407 infoLog << vertexTask.getInfoLog().str();
1408 infoLog << pixelTask.getInfoLog().str();
1409 infoLog << geometryTask.getInfoLog().str();
1410
1411 ANGLE_TRY(vertexTask.getError());
1412 ANGLE_TRY(pixelTask.getError());
1413 ANGLE_TRY(geometryTask.getError());
1414
1415 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1416 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1417 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1418
Jamie Madill48ef11b2016-04-27 15:21:52 -04001419 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001420
1421 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001422 {
Jamie Madill334d6152015-10-22 14:00:28 -04001423 // Geometry shaders are currently only used internally, so there is no corresponding shader
1424 // object at the interface level. For now the geometry shader debug info is prepended to
1425 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001426 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001427 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001428 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1429 }
1430
1431 if (defaultVertexExecutable)
1432 {
1433 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1434 }
1435
1436 if (defaultPixelExecutable)
1437 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001438 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001439 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001440 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1441 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001442
Jamie Madillb0a838b2016-11-13 20:02:12 -05001443 return (defaultVertexExecutable && defaultPixelExecutable &&
1444 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001445}
1446
Jamie Madill9082b982016-04-27 15:21:51 -04001447LinkResult ProgramD3D::link(const gl::ContextState &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001448{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001449 reset();
1450
Jamie Madill48ef11b2016-04-27 15:21:52 -04001451 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1452 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001453
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001454 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1455 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1456
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001457 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1458 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001459
Arun Patole44efa0b2015-03-04 17:11:05 +05301460 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001461 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1462
Jamie Madill53ea9cc2016-05-17 10:12:52 -04001463 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
Austin Kinross02df7962015-07-01 10:03:42 -07001464 {
1465 if (fragmentShaderD3D->usesFrontFacing())
1466 {
1467 infoLog << "The current renderer doesn't support gl_FrontFacing";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001468 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001469 }
1470 }
1471
Jamie Madillca03b352015-09-02 12:38:13 -04001472 std::vector<PackedVarying> packedVaryings =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001473 MergeVaryings(*vertexShader, *fragmentShader, mState.getTransformFeedbackVaryingNames());
Jamie Madillca03b352015-09-02 12:38:13 -04001474
Brandon Jones22502d52014-08-29 16:58:36 -07001475 // Map the varyings to the register file
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001476 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors);
Jamie Madill2ad1c492016-12-07 14:46:18 -05001477 if (!varyingPacking.packUserVaryings(infoLog, packedVaryings,
1478 mState.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001479 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001480 return false;
Brandon Jones22502d52014-08-29 16:58:36 -07001481 }
1482
Jamie Madillc9bde922016-07-24 17:58:50 -04001483 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madille39a3f02015-11-17 20:42:15 -05001484
Jamie Madill120040e2016-12-07 14:46:16 -05001485 metadata.updatePackingBuiltins(SHADER_VERTEX, &varyingPacking);
1486 metadata.updatePackingBuiltins(SHADER_PIXEL, &varyingPacking);
Jamie Madill9fc36822015-11-18 13:08:07 -05001487
Jamie Madill2ad1c492016-12-07 14:46:18 -05001488 if (!varyingPacking.validateBuiltins())
Jamie Madill9fc36822015-11-18 13:08:07 -05001489 {
1490 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001491 return false;
Jamie Madill9fc36822015-11-18 13:08:07 -05001492 }
1493
1494 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1495 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1496 // intelligently, but D3D9 assumes one semantic per register.
1497 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001498 varyingPacking.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
Jamie Madill9fc36822015-11-18 13:08:07 -05001499 {
1500 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001501 return false;
Jamie Madill9fc36822015-11-18 13:08:07 -05001502 }
1503
Jamie Madill48ef11b2016-04-27 15:21:52 -04001504 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, varyingPacking, &mPixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -05001505 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001506 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001507 return false;
Brandon Jones22502d52014-08-29 16:58:36 -07001508 }
1509
Brandon Jones44151a92014-09-10 11:32:25 -07001510 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001511 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1512 mUsesFragDepth = metadata.usesFragDepth();
Brandon Jones44151a92014-09-10 11:32:25 -07001513
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001514 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001515 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001516 for (const auto &varying : packedVaryings)
1517 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001518 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001519 {
1520 mUsesFlatInterpolation = true;
1521 break;
1522 }
1523 }
1524
Jamie Madill4e31ad52015-10-29 10:32:57 -04001525 if (mRenderer->getMajorShaderModel() >= 4)
1526 {
Jamie Madill120040e2016-12-07 14:46:16 -05001527 metadata.updatePackingBuiltins(SHADER_GEOMETRY, &varyingPacking);
Jamie Madill9fc36822015-11-18 13:08:07 -05001528 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001529 }
1530
Jamie Madill8047c0d2016-03-07 13:02:12 -05001531 initAttribLocationsToD3DSemantic();
Jamie Madill437d2662014-12-05 14:23:35 -05001532
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001533 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001534
Jamie Madill9fc36822015-11-18 13:08:07 -05001535 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001536
Jamie Madill4e31ad52015-10-29 10:32:57 -04001537 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001538 if (result.isError())
Geoff Lang65d17e52016-09-08 09:52:58 -04001539 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001540 infoLog << result.getError().getMessage();
Geoff Lang65d17e52016-09-08 09:52:58 -04001541 return result;
1542 }
Jamie Madillb0a838b2016-11-13 20:02:12 -05001543 else if (!result.getResult())
Jamie Madill31c8c562015-08-19 14:08:03 -04001544 {
1545 infoLog << "Failed to create D3D shaders.";
1546 return result;
1547 }
1548
Jamie Madill4a3c2342015-10-08 12:58:45 -04001549 initUniformBlockInfo();
1550
Jamie Madillb0a838b2016-11-13 20:02:12 -05001551 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001552}
1553
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001554GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001555{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001556 // TODO(jmadill): Do something useful here?
1557 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001558}
1559
Jamie Madill4a3c2342015-10-08 12:58:45 -04001560void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001561{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001562 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001563
Jamie Madill62d31cb2015-09-11 13:25:51 -04001564 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1565 {
1566 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1567 continue;
1568
Jamie Madill4a3c2342015-10-08 12:58:45 -04001569 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001570 continue;
1571
Jamie Madill4a3c2342015-10-08 12:58:45 -04001572 size_t dataSize = getUniformBlockInfo(vertexBlock);
1573 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001574 }
1575
Jamie Madill48ef11b2016-04-27 15:21:52 -04001576 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001577
1578 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1579 {
1580 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1581 continue;
1582
Jamie Madill4a3c2342015-10-08 12:58:45 -04001583 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001584 continue;
1585
Jamie Madill4a3c2342015-10-08 12:58:45 -04001586 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1587 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001588 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001589}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001590
Jamie Madill4a3c2342015-10-08 12:58:45 -04001591void ProgramD3D::assignUniformBlockRegisters()
1592{
1593 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001594
1595 // Assign registers and update sizes.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001596 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1597 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001598
Jamie Madill48ef11b2016-04-27 15:21:52 -04001599 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001600 {
1601 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1602
Jamie Madill4a3c2342015-10-08 12:58:45 -04001603 D3DUniformBlock d3dUniformBlock;
1604
Jamie Madill62d31cb2015-09-11 13:25:51 -04001605 if (uniformBlock.vertexStaticUse)
1606 {
1607 unsigned int baseRegister =
1608 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001609 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001610 }
1611
1612 if (uniformBlock.fragmentStaticUse)
1613 {
1614 unsigned int baseRegister =
1615 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001616 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001617 }
1618
Jamie Madill4a3c2342015-10-08 12:58:45 -04001619 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001620 }
1621}
1622
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001623void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001624{
1625 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001626 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001627 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001628 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001629 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001630 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001631 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001632 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001633 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001634 vertexRegisters = std::max(vertexRegisters,
1635 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001636 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001637 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001638 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001639 fragmentRegisters = std::max(
1640 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001641 }
1642 }
1643 }
1644
Jamie Madill334d6152015-10-22 14:00:28 -04001645 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001646 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1647}
1648
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001649gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001650{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001651 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001652
Jamie Madill01074252016-11-28 15:55:51 -05001653 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001654
Jamie Madill62d31cb2015-09-11 13:25:51 -04001655 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001656 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001657 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001658 }
1659
1660 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001661}
1662
Jamie Madill9082b982016-04-27 15:21:51 -04001663gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001664{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001665 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001666 {
1667 return gl::Error(GL_NO_ERROR);
1668 }
1669
1670 // Lazy init.
1671 if (mD3DUniformBlocks.empty())
1672 {
1673 assignUniformBlockRegisters();
1674 }
1675
Jamie Madill03260fa2015-06-22 13:57:22 -04001676 mVertexUBOCache.clear();
1677 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001678
1679 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1680 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1681
Jamie Madill4a3c2342015-10-08 12:58:45 -04001682 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001683 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001684 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001685 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001686 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001687
Brandon Jones18bd4102014-09-22 14:21:44 -07001688 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001689 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001690 {
1691 continue;
1692 }
1693
Jamie Madill4a3c2342015-10-08 12:58:45 -04001694 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001695 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001696 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001697 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001698
Jamie Madill969194d2015-07-20 14:36:56 -04001699 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001700 {
1701 mVertexUBOCache.resize(registerIndex + 1, -1);
1702 }
1703
1704 ASSERT(mVertexUBOCache[registerIndex] == -1);
1705 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001706 }
1707
Jamie Madill4a3c2342015-10-08 12:58:45 -04001708 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001709 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001710 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001711 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001712
1713 if (mFragmentUBOCache.size() <= registerIndex)
1714 {
1715 mFragmentUBOCache.resize(registerIndex + 1, -1);
1716 }
1717
1718 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1719 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001720 }
1721 }
1722
Jamie Madill03260fa2015-06-22 13:57:22 -04001723 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001724}
1725
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001726void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001727{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001728 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001729 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001730 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001731 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001732}
1733
Jamie Madill334d6152015-10-22 14:00:28 -04001734void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001735{
1736 setUniform(location, count, v, GL_FLOAT);
1737}
1738
1739void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1740{
1741 setUniform(location, count, v, GL_FLOAT_VEC2);
1742}
1743
1744void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1745{
1746 setUniform(location, count, v, GL_FLOAT_VEC3);
1747}
1748
1749void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1750{
1751 setUniform(location, count, v, GL_FLOAT_VEC4);
1752}
1753
Jamie Madill334d6152015-10-22 14:00:28 -04001754void ProgramD3D::setUniformMatrix2fv(GLint location,
1755 GLsizei count,
1756 GLboolean transpose,
1757 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001758{
1759 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1760}
1761
Jamie Madill334d6152015-10-22 14:00:28 -04001762void ProgramD3D::setUniformMatrix3fv(GLint location,
1763 GLsizei count,
1764 GLboolean transpose,
1765 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001766{
1767 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1768}
1769
Jamie Madill334d6152015-10-22 14:00:28 -04001770void ProgramD3D::setUniformMatrix4fv(GLint location,
1771 GLsizei count,
1772 GLboolean transpose,
1773 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001774{
1775 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1776}
1777
Jamie Madill334d6152015-10-22 14:00:28 -04001778void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1779 GLsizei count,
1780 GLboolean transpose,
1781 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001782{
1783 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1784}
1785
Jamie Madill334d6152015-10-22 14:00:28 -04001786void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1787 GLsizei count,
1788 GLboolean transpose,
1789 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001790{
1791 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1792}
1793
Jamie Madill334d6152015-10-22 14:00:28 -04001794void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1795 GLsizei count,
1796 GLboolean transpose,
1797 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001798{
1799 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1800}
1801
Jamie Madill334d6152015-10-22 14:00:28 -04001802void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1803 GLsizei count,
1804 GLboolean transpose,
1805 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001806{
1807 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1808}
1809
Jamie Madill334d6152015-10-22 14:00:28 -04001810void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1811 GLsizei count,
1812 GLboolean transpose,
1813 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001814{
1815 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1816}
1817
Jamie Madill334d6152015-10-22 14:00:28 -04001818void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1819 GLsizei count,
1820 GLboolean transpose,
1821 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001822{
1823 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1824}
1825
1826void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1827{
1828 setUniform(location, count, v, GL_INT);
1829}
1830
1831void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1832{
1833 setUniform(location, count, v, GL_INT_VEC2);
1834}
1835
1836void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1837{
1838 setUniform(location, count, v, GL_INT_VEC3);
1839}
1840
1841void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1842{
1843 setUniform(location, count, v, GL_INT_VEC4);
1844}
1845
1846void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1847{
1848 setUniform(location, count, v, GL_UNSIGNED_INT);
1849}
1850
1851void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1852{
1853 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1854}
1855
1856void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1857{
1858 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1859}
1860
1861void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1862{
1863 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1864}
1865
Jamie Madill4a3c2342015-10-08 12:58:45 -04001866void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1867 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001868{
1869}
1870
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001871void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001872{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001873 D3DUniformMap uniformMap;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001874 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001875 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001876
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001877 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001878 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001879 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001880 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001881 }
1882 }
1883
Jamie Madill48ef11b2016-04-27 15:21:52 -04001884 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001885 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001886 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001887 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001888 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001889 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001890 }
1891 }
1892
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001893 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001894 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001895 {
1896 if (!glUniform.isInDefaultBlock())
1897 continue;
1898
1899 auto mapEntry = uniformMap.find(glUniform.name);
1900 ASSERT(mapEntry != uniformMap.end());
1901 mD3DUniforms.push_back(mapEntry->second);
1902 }
1903
Jamie Madill62d31cb2015-09-11 13:25:51 -04001904 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001905 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001906}
1907
Jamie Madill91445bc2015-09-23 16:47:53 -04001908void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001909 const sh::Uniform &uniform,
1910 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001911{
Olli Etuaho96963162016-03-21 11:54:33 +02001912 // Samplers get their registers assigned in assignAllSamplerRegisters.
1913 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001914 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001915 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001916 return;
1917 }
1918
Jamie Madill91445bc2015-09-23 16:47:53 -04001919 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1920
1921 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1922 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001923 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001924 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001925
Jamie Madill91445bc2015-09-23 16:47:53 -04001926 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001927}
1928
Jamie Madill62d31cb2015-09-11 13:25:51 -04001929D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1930{
1931 for (D3DUniform *d3dUniform : mD3DUniforms)
1932 {
1933 if (d3dUniform->name == name)
1934 {
1935 return d3dUniform;
1936 }
1937 }
1938
1939 return nullptr;
1940}
1941
Jamie Madill91445bc2015-09-23 16:47:53 -04001942void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001943 const sh::ShaderVariable &uniform,
1944 const std::string &fullName,
1945 sh::HLSLBlockEncoder *encoder,
1946 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001947{
1948 if (uniform.isStruct())
1949 {
1950 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1951 {
1952 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1953
Jamie Madill55def582015-05-04 11:24:57 -04001954 if (encoder)
1955 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001956
1957 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1958 {
Jamie Madill334d6152015-10-22 14:00:28 -04001959 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001960 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1961
Olli Etuaho96963162016-03-21 11:54:33 +02001962 // Samplers get their registers assigned in assignAllSamplerRegisters.
1963 // Also they couldn't use the same encoder as the rest of the struct, since they are
1964 // extracted out of the struct by the shader translator.
1965 if (gl::IsSamplerType(field.type))
1966 {
1967 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
1968 }
1969 else
1970 {
1971 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
1972 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001973 }
1974
Jamie Madill55def582015-05-04 11:24:57 -04001975 if (encoder)
1976 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001977 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001978 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001979 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001980
1981 // Not a struct. Arrays are treated as aggregate types.
1982 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001983 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001984 encoder->enterAggregateType();
1985 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986
Jamie Madill62d31cb2015-09-11 13:25:51 -04001987 // Advance the uniform offset, to track registers allocation for structs
1988 sh::BlockMemberInfo blockInfo =
1989 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1990 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001991
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001992 auto uniformMapEntry = uniformMap->find(fullName);
1993 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001994
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001995 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001996 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001997 d3dUniform = uniformMapEntry->second;
1998 }
1999 else
2000 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002001 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002002 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002003 }
2004
2005 if (encoder)
2006 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002007 d3dUniform->registerElement =
2008 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002009 unsigned int reg =
2010 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002011 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002012 {
2013 d3dUniform->psRegisterIndex = reg;
2014 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002015 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002016 {
Jamie Madill91445bc2015-09-23 16:47:53 -04002017 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002018 d3dUniform->vsRegisterIndex = reg;
2019 }
Jamie Madillfb536032015-09-11 13:19:49 -04002020
2021 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002022 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002023 {
2024 encoder->exitAggregateType();
2025 }
2026 }
2027}
2028
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002029template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002030void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002031{
Jamie Madill334d6152015-10-22 14:00:28 -04002032 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002033 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
2034
Jamie Madill62d31cb2015-09-11 13:25:51 -04002035 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002036
Jamie Madill62d31cb2015-09-11 13:25:51 -04002037 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002038 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002039 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002040
2041 if (targetUniform->type == targetUniformType)
2042 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002043 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002044
Jamie Madill62d31cb2015-09-11 13:25:51 -04002045 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002046 {
Jamie Madill334d6152015-10-22 14:00:28 -04002047 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002048 const T *source = v + (i * components);
2049
2050 for (int c = 0; c < components; c++)
2051 {
2052 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
2053 }
2054 for (int c = components; c < 4; c++)
2055 {
2056 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
2057 }
2058 }
2059 }
2060 else if (targetUniform->type == targetBoolType)
2061 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002062 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002063
Jamie Madill62d31cb2015-09-11 13:25:51 -04002064 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002065 {
Jamie Madill334d6152015-10-22 14:00:28 -04002066 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002067 const T *source = v + (i * components);
2068
2069 for (int c = 0; c < components; c++)
2070 {
Jamie Madill334d6152015-10-22 14:00:28 -04002071 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
2072 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002073 }
2074 for (int c = components; c < 4; c++)
2075 {
2076 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2077 }
2078 }
2079 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002080 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002081 {
2082 ASSERT(targetUniformType == GL_INT);
2083
Jamie Madill62d31cb2015-09-11 13:25:51 -04002084 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002085
2086 bool wasDirty = targetUniform->dirty;
2087
Jamie Madill62d31cb2015-09-11 13:25:51 -04002088 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002089 {
Jamie Madill334d6152015-10-22 14:00:28 -04002090 GLint *dest = target + (i * 4);
2091 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002092
2093 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2094 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2095 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2096 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2097 }
2098
2099 if (!wasDirty && targetUniform->dirty)
2100 {
2101 mDirtySamplerMapping = true;
2102 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002103 }
Jamie Madill334d6152015-10-22 14:00:28 -04002104 else
2105 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002106}
2107
2108template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002109void ProgramD3D::setUniformMatrixfv(GLint location,
2110 GLsizei countIn,
2111 GLboolean transpose,
2112 const GLfloat *value,
2113 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002114{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002115 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002116
Jamie Madill62d31cb2015-09-11 13:25:51 -04002117 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002118 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002119 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002120
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002121 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002122 GLfloat *target =
2123 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002124
Jamie Madill62d31cb2015-09-11 13:25:51 -04002125 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002126 {
2127 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2128 if (transpose == GL_FALSE)
2129 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002130 targetUniform->dirty =
2131 TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002132 }
2133 else
2134 {
Jamie Madill334d6152015-10-22 14:00:28 -04002135 targetUniform->dirty =
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002136 ExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002137 }
2138 target += targetMatrixStride;
2139 value += cols * rows;
2140 }
2141}
2142
Jamie Madill4a3c2342015-10-08 12:58:45 -04002143size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002144{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002145 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002146
Jamie Madill62d31cb2015-09-11 13:25:51 -04002147 // define member uniforms
2148 sh::Std140BlockEncoder std140Encoder;
2149 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2150 sh::BlockLayoutEncoder *encoder = nullptr;
2151
2152 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002153 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002154 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002155 }
2156 else
2157 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002158 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002159 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002160
Jamie Madill39046162016-02-08 15:05:17 -05002161 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2162 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002163
2164 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002165}
2166
Jamie Madill62d31cb2015-09-11 13:25:51 -04002167void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002168{
Olli Etuaho96963162016-03-21 11:54:33 +02002169 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002170 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002171 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002172 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002173 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002174 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002175 }
2176}
2177
Olli Etuaho96963162016-03-21 11:54:33 +02002178void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002179{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002180 ASSERT(d3dUniform->isSampler());
Jamie Madill48ef11b2016-04-27 15:21:52 -04002181 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2182 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Olli Etuaho96963162016-03-21 11:54:33 +02002183 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) || fragmentShaderD3D->hasUniform(d3dUniform));
2184 if (vertexShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002185 {
Olli Etuaho96963162016-03-21 11:54:33 +02002186 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2187 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002188 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2189 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002190 }
Olli Etuaho96963162016-03-21 11:54:33 +02002191 if (fragmentShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002192 {
Olli Etuaho96963162016-03-21 11:54:33 +02002193 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2194 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002195 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2196 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002197 }
2198}
2199
Jamie Madill62d31cb2015-09-11 13:25:51 -04002200// static
2201void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002202 GLenum samplerType,
2203 unsigned int samplerCount,
2204 std::vector<Sampler> &outSamplers,
2205 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002206{
2207 unsigned int samplerIndex = startSamplerIndex;
2208
2209 do
2210 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002211 ASSERT(samplerIndex < outSamplers.size());
2212 Sampler *sampler = &outSamplers[samplerIndex];
2213 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002214 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002215 sampler->logicalTextureUnit = 0;
2216 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002217 samplerIndex++;
2218 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002219}
2220
Brandon Jonesc9610c52014-08-25 17:02:59 -07002221void ProgramD3D::reset()
2222{
Brandon Joneseb994362014-09-24 10:27:28 -07002223 SafeDeleteContainer(mVertexExecutables);
2224 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002225
2226 for (auto &element : mGeometryExecutables)
2227 {
2228 SafeDelete(element);
2229 }
Brandon Joneseb994362014-09-24 10:27:28 -07002230
Brandon Jones22502d52014-08-29 16:58:36 -07002231 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002232 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002233
2234 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002235 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002236 mUsesFragDepth = false;
2237 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002238 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002239 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002240
Jamie Madill62d31cb2015-09-11 13:25:51 -04002241 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002242 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002243
Brandon Jonesc9610c52014-08-25 17:02:59 -07002244 SafeDelete(mVertexUniformStorage);
2245 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002246
2247 mSamplersPS.clear();
2248 mSamplersVS.clear();
2249
2250 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002251 mUsedPixelSamplerRange = 0;
2252 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002253
Jamie Madill8047c0d2016-03-07 13:02:12 -05002254 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002255
Jamie Madill9fc36822015-11-18 13:08:07 -05002256 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002257
2258 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002259}
2260
Geoff Lang7dd2e102014-11-10 15:19:26 -05002261unsigned int ProgramD3D::getSerial() const
2262{
2263 return mSerial;
2264}
2265
2266unsigned int ProgramD3D::issueSerial()
2267{
2268 return mCurrentSerial++;
2269}
2270
Jamie Madill8047c0d2016-03-07 13:02:12 -05002271void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002272{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002273 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002274 ASSERT(vertexShader != nullptr);
2275
2276 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002277 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002278 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002279 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2280 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002281
Jamie Madill8047c0d2016-03-07 13:02:12 -05002282 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002283 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002284 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002285 }
2286 }
Jamie Madill437d2662014-12-05 14:23:35 -05002287}
2288
Jamie Madill63805b42015-08-25 13:17:39 -04002289void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002290{
Jamie Madillbd136f92015-08-10 14:51:37 -04002291 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002292 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002293
Jamie Madill01074252016-11-28 15:55:51 -05002294 for (unsigned int locationIndex : IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002295 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002296 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002297
Jamie Madill8047c0d2016-03-07 13:02:12 -05002298 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002299 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002300 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002301 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002302 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002303 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002304 mCachedInputLayout[d3dSemantic] =
2305 GetVertexFormatType(vertexAttributes[locationIndex],
2306 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002307 }
2308 }
2309}
2310
Jamie Madill9fc36822015-11-18 13:08:07 -05002311void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002312{
Jamie Madill9fc36822015-11-18 13:08:07 -05002313 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2314
2315 const std::string &varyingSemantic =
2316 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2317
Jamie Madillccdf74b2015-08-18 10:46:12 -04002318 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002319 mStreamOutVaryings.clear();
2320
Jamie Madill48ef11b2016-04-27 15:21:52 -04002321 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002322 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2323 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002324 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002325 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2326 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002327 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002328 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002329 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002330 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2331 builtins.glPosition.index, 4, outputSlot));
2332 }
2333 }
2334 else if (tfVaryingName == "gl_FragCoord")
2335 {
2336 if (builtins.glFragCoord.enabled)
2337 {
2338 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2339 builtins.glFragCoord.index, 4, outputSlot));
2340 }
2341 }
2342 else if (tfVaryingName == "gl_PointSize")
2343 {
2344 if (builtins.glPointSize.enabled)
2345 {
2346 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2347 }
2348 }
2349 else
2350 {
2351 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2352 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002353 const auto &varying = *registerInfo.packedVarying->varying;
2354 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002355 int componentCount = gl::VariableColumnCount(transposedType);
2356 ASSERT(!varying.isBuiltIn());
2357
Jamie Madill55c25d02015-11-18 13:08:08 -05002358 // Transform feedback for varying structs is underspecified.
2359 // See Khronos bug 9856.
2360 // TODO(jmadill): Figure out how to be spec-compliant here.
2361 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2362 continue;
2363
Jamie Madill9fc36822015-11-18 13:08:07 -05002364 // There can be more than one register assigned to a particular varying, and each
2365 // register needs its own stream out entry.
2366 if (tfVaryingName == varying.name)
2367 {
2368 mStreamOutVaryings.push_back(D3DVarying(
2369 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2370 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002371 }
2372 }
2373 }
2374}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002375
2376D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2377{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002378 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002379}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002380
2381bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2382{
2383 std::string baseName = blockName;
2384 gl::ParseAndStripArrayIndex(&baseName);
2385
2386 auto sizeIter = mBlockDataSizes.find(baseName);
2387 if (sizeIter == mBlockDataSizes.end())
2388 {
2389 *sizeOut = 0;
2390 return false;
2391 }
2392
2393 *sizeOut = sizeIter->second;
2394 return true;
2395}
2396
2397bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2398 sh::BlockMemberInfo *memberInfoOut) const
2399{
2400 auto infoIter = mBlockInfo.find(memberUniformName);
2401 if (infoIter == mBlockInfo.end())
2402 {
2403 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2404 return false;
2405 }
2406
2407 *memberInfoOut = infoIter->second;
2408 return true;
2409}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002410
2411void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2412 GLenum genMode,
2413 GLint components,
2414 const GLfloat *coeffs)
2415{
2416 UNREACHABLE();
2417}
2418
Jamie Madill8047c0d2016-03-07 13:02:12 -05002419} // namespace rx