blob: 30020b765d180164ebf611a55e1a3ba547b47176 [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 Madilld3dfda22015-07-06 08:28:49 -040016#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000017#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050018#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050019#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/renderer/d3d/RendererD3D.h"
21#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050022#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill65345da2015-11-13 11:25:23 -050023#include "libANGLE/renderer/d3d/VaryingPacking.h"
Jamie Madill437d2662014-12-05 14:23:35 -050024#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050025
Brandon Jonesc9610c52014-08-25 17:02:59 -070026namespace rx
27{
28
Brandon Joneseb994362014-09-24 10:27:28 -070029namespace
30{
31
Jamie Madillf8dd7b12015-08-05 13:50:08 -040032gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070033{
Jamie Madillbd136f92015-08-10 14:51:37 -040034 gl::InputLayout defaultLayout;
35 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070036 {
Brandon Joneseb994362014-09-24 10:27:28 -070037 if (shaderAttr.type != GL_NONE)
38 {
39 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
40
Jamie Madilld3dfda22015-07-06 08:28:49 -040041 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040042 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070043 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040044 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040045 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040046 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040047 gl::VertexFormatType defaultType =
48 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070049
Jamie Madillbd136f92015-08-10 14:51:37 -040050 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070051 }
52 }
53 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040054
55 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070056}
57
Jamie Madill334d6152015-10-22 14:00:28 -040058std::vector<GLenum> GetDefaultOutputLayoutFromShader(
59 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070060{
Jamie Madillb4463142014-12-19 14:56:54 -050061 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070062
Jamie Madillb4463142014-12-19 14:56:54 -050063 if (!shaderOutputVars.empty())
64 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070065 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
66 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050067 }
Brandon Joneseb994362014-09-24 10:27:28 -070068
69 return defaultPixelOutput;
70}
71
Brandon Jones1a8a7e32014-10-01 12:49:30 -070072bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
73{
74 return var.isRowMajorLayout;
75}
76
77bool IsRowMajorLayout(const sh::ShaderVariable &var)
78{
79 return false;
80}
81
Jamie Madill437d2662014-12-05 14:23:35 -050082struct AttributeSorter
83{
Jamie Madill63805b42015-08-25 13:17:39 -040084 AttributeSorter(const ProgramD3D::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -050085 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -050086 {
87 }
88
89 bool operator()(int a, int b)
90 {
Jamie Madill80d934b2015-02-19 10:16:12 -050091 int indexA = (*originalIndices)[a];
92 int indexB = (*originalIndices)[b];
93
Jamie Madill334d6152015-10-22 14:00:28 -040094 if (indexA == -1)
95 return false;
96 if (indexB == -1)
97 return true;
Jamie Madill80d934b2015-02-19 10:16:12 -050098 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -050099 }
100
Jamie Madill63805b42015-08-25 13:17:39 -0400101 const ProgramD3D::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500102};
103
Jamie Madill9fc36822015-11-18 13:08:07 -0500104// true if varying x has a higher priority in packing than y
105bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
106{
Jamie Madill55c25d02015-11-18 13:08:08 -0500107 return gl::CompareShaderVar(*x.varying, *y.varying);
Jamie Madill9fc36822015-11-18 13:08:07 -0500108}
109
Jamie Madillca03b352015-09-02 12:38:13 -0400110std::vector<PackedVarying> MergeVaryings(const gl::Shader &vertexShader,
111 const gl::Shader &fragmentShader,
112 const std::vector<std::string> &tfVaryings)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400113{
Jamie Madillca03b352015-09-02 12:38:13 -0400114 std::vector<PackedVarying> packedVaryings;
115
116 for (const sh::Varying &output : vertexShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400117 {
Jamie Madillca03b352015-09-02 12:38:13 -0400118 bool packed = false;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400119
120 // Built-in varyings obey special rules
Jamie Madillca03b352015-09-02 12:38:13 -0400121 if (output.isBuiltIn())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400122 {
123 continue;
124 }
125
Jamie Madillca03b352015-09-02 12:38:13 -0400126 for (const sh::Varying &input : fragmentShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400127 {
Jamie Madillca03b352015-09-02 12:38:13 -0400128 if (output.name == input.name)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400129 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500130 if (output.isStruct())
131 {
132 ASSERT(!output.isArray());
133 for (const auto &field : output.fields)
134 {
135 ASSERT(!field.isStruct() && !field.isArray());
136 packedVaryings.push_back(
137 PackedVarying(field, input.interpolation, input.name));
138 }
139 }
140 else
141 {
142 packedVaryings.push_back(PackedVarying(input, input.interpolation));
143 }
Jamie Madillca03b352015-09-02 12:38:13 -0400144 packed = true;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400145 break;
146 }
147 }
148
Jamie Madillca03b352015-09-02 12:38:13 -0400149 // Keep Transform FB varyings in the merged list always.
150 if (!packed)
151 {
152 for (const std::string &tfVarying : tfVaryings)
153 {
154 if (tfVarying == output.name)
155 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500156 // Transform feedback for varying structs is underspecified.
157 // See Khronos bug 9856.
158 // TODO(jmadill): Figure out how to be spec-compliant here.
159 if (!output.isStruct())
160 {
161 packedVaryings.push_back(PackedVarying(output, output.interpolation));
162 packedVaryings.back().vertexOnly = true;
163 }
Jamie Madillca03b352015-09-02 12:38:13 -0400164 break;
165 }
166 }
167 }
Jamie Madillada9ecc2015-08-17 12:53:37 -0400168 }
169
Jamie Madill9fc36822015-11-18 13:08:07 -0500170 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
171
Jamie Madillca03b352015-09-02 12:38:13 -0400172 return packedVaryings;
Brandon Joneseb994362014-09-24 10:27:28 -0700173}
174
Jamie Madill62d31cb2015-09-11 13:25:51 -0400175template <typename VarT>
176void GetUniformBlockInfo(const std::vector<VarT> &fields,
177 const std::string &prefix,
178 sh::BlockLayoutEncoder *encoder,
179 bool inRowMajorLayout,
180 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
181{
182 for (const VarT &field : fields)
183 {
184 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
185
186 if (field.isStruct())
187 {
188 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
189
190 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
191 {
192 encoder->enterAggregateType();
193
194 const std::string uniformElementName =
195 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
196 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
197 blockInfoOut);
198
199 encoder->exitAggregateType();
200 }
201 }
202 else
203 {
204 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
205 (*blockInfoOut)[fieldName] =
206 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
207 }
208 }
209}
210
Jamie Madill334d6152015-10-22 14:00:28 -0400211template <typename T>
212bool TransposeMatrix(T *target,
213 const GLfloat *value,
214 int targetWidth,
215 int targetHeight,
216 int srcWidth,
217 int srcHeight)
218{
219 bool dirty = false;
220 int copyWidth = std::min(targetHeight, srcWidth);
221 int copyHeight = std::min(targetWidth, srcHeight);
222
223 for (int x = 0; x < copyWidth; x++)
224 {
225 for (int y = 0; y < copyHeight; y++)
226 {
227 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]),
228 &dirty);
229 }
230 }
231 // clear unfilled right side
232 for (int y = 0; y < copyWidth; y++)
233 {
234 for (int x = copyHeight; x < targetWidth; x++)
235 {
236 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
237 }
238 }
239 // clear unfilled bottom.
240 for (int y = copyWidth; y < targetHeight; y++)
241 {
242 for (int x = 0; x < targetWidth; x++)
243 {
244 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
245 }
246 }
247
248 return dirty;
249}
250
251template <typename T>
252bool ExpandMatrix(T *target,
253 const GLfloat *value,
254 int targetWidth,
255 int targetHeight,
256 int srcWidth,
257 int srcHeight)
258{
259 bool dirty = false;
260 int copyWidth = std::min(targetWidth, srcWidth);
261 int copyHeight = std::min(targetHeight, srcHeight);
262
263 for (int y = 0; y < copyHeight; y++)
264 {
265 for (int x = 0; x < copyWidth; x++)
266 {
267 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]),
268 &dirty);
269 }
270 }
271 // clear unfilled right side
272 for (int y = 0; y < copyHeight; y++)
273 {
274 for (int x = copyWidth; x < targetWidth; x++)
275 {
276 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
277 }
278 }
279 // clear unfilled bottom.
280 for (int y = copyHeight; y < targetHeight; y++)
281 {
282 for (int x = 0; x < targetWidth; x++)
283 {
284 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
285 }
286 }
287
288 return dirty;
289}
290
Jamie Madill4e31ad52015-10-29 10:32:57 -0400291gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
292{
293 switch (drawMode)
294 {
295 // Uses the point sprite geometry shader.
296 case GL_POINTS:
297 return gl::PRIMITIVE_POINTS;
298
299 // All line drawing uses the same geometry shader.
300 case GL_LINES:
301 case GL_LINE_STRIP:
302 case GL_LINE_LOOP:
303 return gl::PRIMITIVE_LINES;
304
305 // The triangle fan primitive is emulated with strips in D3D11.
306 case GL_TRIANGLES:
307 case GL_TRIANGLE_FAN:
308 return gl::PRIMITIVE_TRIANGLES;
309
310 // Special case for triangle strips.
311 case GL_TRIANGLE_STRIP:
312 return gl::PRIMITIVE_TRIANGLE_STRIP;
313
314 default:
315 UNREACHABLE();
316 return gl::PRIMITIVE_TYPE_MAX;
317 }
318}
319
Jamie Madillada9ecc2015-08-17 12:53:37 -0400320} // anonymous namespace
321
Jamie Madill28afae52015-11-09 15:07:57 -0500322// D3DUniform Implementation
323
Jamie Madill62d31cb2015-09-11 13:25:51 -0400324D3DUniform::D3DUniform(GLenum typeIn,
325 const std::string &nameIn,
326 unsigned int arraySizeIn,
327 bool defaultBlock)
328 : type(typeIn),
329 name(nameIn),
330 arraySize(arraySizeIn),
331 data(nullptr),
332 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400333 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400334 psRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400335 registerCount(0),
336 registerElement(0)
337{
338 // We use data storage for default block uniforms to cache values that are sent to D3D during
339 // rendering
340 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
341 if (defaultBlock)
342 {
343 size_t bytes = gl::VariableInternalSize(type) * elementCount();
344 data = new uint8_t[bytes];
345 memset(data, 0, bytes);
346
347 // TODO(jmadill): is this correct with non-square matrices?
348 registerCount = gl::VariableRowCount(type) * elementCount();
349 }
350}
351
352D3DUniform::~D3DUniform()
353{
354 SafeDeleteArray(data);
355}
356
357bool D3DUniform::isSampler() const
358{
359 return gl::IsSamplerType(type);
360}
361
362bool D3DUniform::isReferencedByVertexShader() const
363{
364 return vsRegisterIndex != GL_INVALID_INDEX;
365}
366
367bool D3DUniform::isReferencedByFragmentShader() const
368{
369 return psRegisterIndex != GL_INVALID_INDEX;
370}
371
Jamie Madill28afae52015-11-09 15:07:57 -0500372// D3DVarying Implementation
373
Jamie Madill9fc36822015-11-18 13:08:07 -0500374D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500375{
376}
377
Jamie Madill9fc36822015-11-18 13:08:07 -0500378D3DVarying::D3DVarying(const std::string &semanticNameIn,
379 unsigned int semanticIndexIn,
380 unsigned int componentCountIn,
381 unsigned int outputSlotIn)
382 : semanticName(semanticNameIn),
383 semanticIndex(semanticIndexIn),
384 componentCount(componentCountIn),
385 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500386{
387}
388
Jamie Madille39a3f02015-11-17 20:42:15 -0500389// ProgramD3DMetadata Implementation
390
391ProgramD3DMetadata::ProgramD3DMetadata(int rendererMajorShaderModel,
392 const std::string &shaderModelSuffix,
393 bool usesInstancedPointSpriteEmulation,
394 const ShaderD3D *vertexShader,
395 const ShaderD3D *fragmentShader)
396 : mRendererMajorShaderModel(rendererMajorShaderModel),
397 mShaderModelSuffix(shaderModelSuffix),
398 mUsesInstancedPointSpriteEmulation(usesInstancedPointSpriteEmulation),
399 mVertexShader(vertexShader),
400 mFragmentShader(fragmentShader)
401{
402}
403
404int ProgramD3DMetadata::getRendererMajorShaderModel() const
405{
406 return mRendererMajorShaderModel;
407}
408
409bool ProgramD3DMetadata::usesBroadcast(const gl::Data &data) const
410{
411 return (mFragmentShader->usesFragColor() && data.clientVersion < 3);
412}
413
414bool ProgramD3DMetadata::usesFragDepth(const gl::Program::Data &programData) const
415{
416 // TODO(jmadill): Rename this or check if we need it for version 300
417 return (getMajorShaderVersion() < 300 && mFragmentShader->usesFragDepth());
418}
419
420bool ProgramD3DMetadata::usesPointCoord() const
421{
422 return mFragmentShader->usesPointCoord();
423}
424
425bool ProgramD3DMetadata::usesFragCoord() const
426{
427 return mFragmentShader->usesFragCoord();
428}
429
430bool ProgramD3DMetadata::usesPointSize() const
431{
432 return mVertexShader->usesPointSize();
433}
434
435bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
436{
437 return !usesPointSize() && usesPointCoord() && mRendererMajorShaderModel >= 4;
438}
439
440bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
441{
442 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader
443 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
444 // GeometryShader PointSprite emulation does not require this additional entry because the
445 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the
446 // PS_INPUT of the generated pixel shader. The Geometry Shader point sprite implementation needs
447 // gl_PointSize to be in VS_OUTPUT and GS_INPUT. Instanced point sprites doesn't need
448 // gl_PointSize in VS_OUTPUT.
449 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
450 usesInsertedPointCoordValue();
451}
452
453bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
454{
455 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
456 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
457 // the vertex shader in this case. This saves us 1 output vector.
458 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
459}
460
461bool ProgramD3DMetadata::usesSystemValuePointSize() const
462{
463 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
464}
465
466bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
467{
468 return mFragmentShader->usesMultipleRenderTargets();
469}
470
471GLint ProgramD3DMetadata::getMajorShaderVersion() const
472{
473 return mVertexShader->getData().getShaderVersion();
474}
475
476const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
477{
478 return mFragmentShader;
479}
480
Jamie Madill28afae52015-11-09 15:07:57 -0500481// ProgramD3D Implementation
482
Jamie Madilld3dfda22015-07-06 08:28:49 -0400483ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
484 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500485 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400486 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700487{
Brandon Joneseb994362014-09-24 10:27:28 -0700488}
489
490ProgramD3D::VertexExecutable::~VertexExecutable()
491{
492 SafeDelete(mShaderExecutable);
493}
494
Jamie Madilld3dfda22015-07-06 08:28:49 -0400495// static
496void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
497 const gl::InputLayout &inputLayout,
498 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700499{
Jamie Madillbd136f92015-08-10 14:51:37 -0400500 signatureOut->resize(inputLayout.size());
Jamie Madilld3dfda22015-07-06 08:28:49 -0400501
502 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700503 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400504 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbd136f92015-08-10 14:51:37 -0400505 bool converted = false;
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400506 if (vertexFormatType != gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700507 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400508 VertexConversionType conversionType =
509 renderer->getVertexConversionType(vertexFormatType);
Jamie Madillbd136f92015-08-10 14:51:37 -0400510 converted = ((conversionType & VERTEX_CONVERT_GPU) != 0);
Brandon Joneseb994362014-09-24 10:27:28 -0700511 }
Jamie Madillbd136f92015-08-10 14:51:37 -0400512
513 (*signatureOut)[index] = converted;
Brandon Joneseb994362014-09-24 10:27:28 -0700514 }
Brandon Joneseb994362014-09-24 10:27:28 -0700515}
516
Jamie Madilld3dfda22015-07-06 08:28:49 -0400517bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
518{
Jamie Madillbd136f92015-08-10 14:51:37 -0400519 size_t limit = std::max(mSignature.size(), signature.size());
520 for (size_t index = 0; index < limit; ++index)
521 {
522 // treat undefined indexes as 'not converted'
523 bool a = index < signature.size() ? signature[index] : false;
524 bool b = index < mSignature.size() ? mSignature[index] : false;
525 if (a != b)
526 return false;
527 }
528
529 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400530}
531
532ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
533 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400534 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700535{
536}
537
538ProgramD3D::PixelExecutable::~PixelExecutable()
539{
540 SafeDelete(mShaderExecutable);
541}
542
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700543ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
544{
545}
546
Geoff Lang7dd2e102014-11-10 15:19:26 -0500547unsigned int ProgramD3D::mCurrentSerial = 1;
548
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400549ProgramD3D::ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer)
550 : ProgramImpl(data),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700551 mRenderer(renderer),
552 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400553 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700554 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400555 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700556 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700557 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700558 mUsedVertexSamplerRange(0),
559 mUsedPixelSamplerRange(0),
560 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500561 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700562{
Brandon Joneseb994362014-09-24 10:27:28 -0700563 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700564}
565
566ProgramD3D::~ProgramD3D()
567{
568 reset();
569 SafeDelete(mDynamicHLSL);
570}
571
Brandon Jones44151a92014-09-10 11:32:25 -0700572bool ProgramD3D::usesPointSpriteEmulation() const
573{
574 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
575}
576
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400577bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700578{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400579 if (drawMode != GL_POINTS)
580 {
581 return mUsesFlatInterpolation;
582 }
583
Cooper Partine6664f02015-01-09 16:22:24 -0800584 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
585}
586
587bool ProgramD3D::usesInstancedPointSpriteEmulation() const
588{
589 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700590}
591
Jamie Madill334d6152015-10-22 14:00:28 -0400592GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
593 unsigned int samplerIndex,
594 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700595{
596 GLint logicalTextureUnit = -1;
597
598 switch (type)
599 {
Jamie Madill334d6152015-10-22 14:00:28 -0400600 case gl::SAMPLER_PIXEL:
601 ASSERT(samplerIndex < caps.maxTextureImageUnits);
602 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
603 {
604 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
605 }
606 break;
607 case gl::SAMPLER_VERTEX:
608 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
609 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
610 {
611 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
612 }
613 break;
614 default:
615 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700616 }
617
Jamie Madill334d6152015-10-22 14:00:28 -0400618 if (logicalTextureUnit >= 0 &&
619 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700620 {
621 return logicalTextureUnit;
622 }
623
624 return -1;
625}
626
627// Returns the texture type for a given Direct3D 9 sampler type and
628// index (0-15 for the pixel shader and 0-3 for the vertex shader).
629GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
630{
631 switch (type)
632 {
Jamie Madill334d6152015-10-22 14:00:28 -0400633 case gl::SAMPLER_PIXEL:
634 ASSERT(samplerIndex < mSamplersPS.size());
635 ASSERT(mSamplersPS[samplerIndex].active);
636 return mSamplersPS[samplerIndex].textureType;
637 case gl::SAMPLER_VERTEX:
638 ASSERT(samplerIndex < mSamplersVS.size());
639 ASSERT(mSamplersVS[samplerIndex].active);
640 return mSamplersVS[samplerIndex].textureType;
641 default:
642 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700643 }
644
645 return GL_TEXTURE_2D;
646}
647
648GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
649{
650 switch (type)
651 {
Jamie Madill334d6152015-10-22 14:00:28 -0400652 case gl::SAMPLER_PIXEL:
653 return mUsedPixelSamplerRange;
654 case gl::SAMPLER_VERTEX:
655 return mUsedVertexSamplerRange;
656 default:
657 UNREACHABLE();
658 return 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700659 }
660}
661
662void ProgramD3D::updateSamplerMapping()
663{
664 if (!mDirtySamplerMapping)
665 {
666 return;
667 }
668
669 mDirtySamplerMapping = false;
670
671 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400672 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700673 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400674 if (!d3dUniform->dirty)
675 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700676
Jamie Madill62d31cb2015-09-11 13:25:51 -0400677 if (!d3dUniform->isSampler())
678 continue;
679
680 int count = d3dUniform->elementCount();
681 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
682
683 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700684 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400685 unsigned int firstIndex = d3dUniform->psRegisterIndex;
686
687 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700688 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400689 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700690
Jamie Madill62d31cb2015-09-11 13:25:51 -0400691 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700692 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400693 ASSERT(mSamplersPS[samplerIndex].active);
694 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700695 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400696 }
697 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700698
Jamie Madill62d31cb2015-09-11 13:25:51 -0400699 if (d3dUniform->isReferencedByVertexShader())
700 {
701 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
702
703 for (int i = 0; i < count; i++)
704 {
705 unsigned int samplerIndex = firstIndex + i;
706
707 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700708 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400709 ASSERT(mSamplersVS[samplerIndex].active);
710 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700711 }
712 }
713 }
714 }
715}
716
Geoff Lang7dd2e102014-11-10 15:19:26 -0500717LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700718{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400719 reset();
720
Jamie Madill334d6152015-10-22 14:00:28 -0400721 DeviceIdentifier binaryDeviceIdentifier = {0};
722 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
723 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700724
725 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
726 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
727 {
728 infoLog << "Invalid program binary, device configuration has changed.";
729 return LinkResult(false, gl::Error(GL_NO_ERROR));
730 }
731
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500732 int compileFlags = stream->readInt<int>();
733 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
734 {
Jamie Madillf6113162015-05-07 11:49:21 -0400735 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500736 return LinkResult(false, gl::Error(GL_NO_ERROR));
737 }
738
Jamie Madill63805b42015-08-25 13:17:39 -0400739 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
740 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
741 {
742 stream->readInt(&mSemanticIndexes[i]);
743 }
744
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700745 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
746 for (unsigned int i = 0; i < psSamplerCount; ++i)
747 {
748 Sampler sampler;
749 stream->readBool(&sampler.active);
750 stream->readInt(&sampler.logicalTextureUnit);
751 stream->readInt(&sampler.textureType);
752 mSamplersPS.push_back(sampler);
753 }
754 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
755 for (unsigned int i = 0; i < vsSamplerCount; ++i)
756 {
757 Sampler sampler;
758 stream->readBool(&sampler.active);
759 stream->readInt(&sampler.logicalTextureUnit);
760 stream->readInt(&sampler.textureType);
761 mSamplersVS.push_back(sampler);
762 }
763
764 stream->readInt(&mUsedVertexSamplerRange);
765 stream->readInt(&mUsedPixelSamplerRange);
766
767 const unsigned int uniformCount = stream->readInt<unsigned int>();
768 if (stream->error())
769 {
Jamie Madillf6113162015-05-07 11:49:21 -0400770 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500771 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700772 }
773
Jamie Madill62d31cb2015-09-11 13:25:51 -0400774 const auto &linkedUniforms = mData.getUniforms();
775 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700776 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
777 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400778 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700779
Jamie Madill62d31cb2015-09-11 13:25:51 -0400780 D3DUniform *d3dUniform =
781 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
782 linkedUniform.isInDefaultBlock());
783 stream->readInt(&d3dUniform->psRegisterIndex);
784 stream->readInt(&d3dUniform->vsRegisterIndex);
785 stream->readInt(&d3dUniform->registerCount);
786 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700787
Jamie Madill62d31cb2015-09-11 13:25:51 -0400788 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700789 }
790
Jamie Madill4a3c2342015-10-08 12:58:45 -0400791 const unsigned int blockCount = stream->readInt<unsigned int>();
792 if (stream->error())
793 {
794 infoLog << "Invalid program binary.";
795 return LinkResult(false, gl::Error(GL_NO_ERROR));
796 }
797
798 ASSERT(mD3DUniformBlocks.empty());
799 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
800 {
801 D3DUniformBlock uniformBlock;
802 stream->readInt(&uniformBlock.psRegisterIndex);
803 stream->readInt(&uniformBlock.vsRegisterIndex);
804 mD3DUniformBlocks.push_back(uniformBlock);
805 }
806
Jamie Madill9fc36822015-11-18 13:08:07 -0500807 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
808 mStreamOutVaryings.resize(streamOutVaryingCount);
809 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700810 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500811 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700812
Jamie Madill28afae52015-11-09 15:07:57 -0500813 stream->readString(&varying->semanticName);
814 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500815 stream->readInt(&varying->componentCount);
816 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700817 }
818
Brandon Jones22502d52014-08-29 16:58:36 -0700819 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400820 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
821 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700822 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400823 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
824 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700825 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700826 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400827 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700828
829 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
830 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400831 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
832 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700833 {
834 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
835 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
836 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
837 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
838 }
839
Jamie Madill4e31ad52015-10-29 10:32:57 -0400840 stream->readString(&mGeometryShaderPreamble);
841
Jamie Madill334d6152015-10-22 14:00:28 -0400842 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700843
844 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400845 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
846 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700847 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400848 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400849 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700850
Jamie Madilld3dfda22015-07-06 08:28:49 -0400851 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700852 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400853 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700854 }
855
Jamie Madill334d6152015-10-22 14:00:28 -0400856 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700857 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400858
Jamie Madillada9ecc2015-08-17 12:53:37 -0400859 ShaderExecutableD3D *shaderExecutable = nullptr;
860
861 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500862 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400863 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400864 if (error.isError())
865 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500866 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400867 }
868
Brandon Joneseb994362014-09-24 10:27:28 -0700869 if (!shaderExecutable)
870 {
Jamie Madillf6113162015-05-07 11:49:21 -0400871 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500872 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700873 }
874
875 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400876 VertexExecutable::Signature signature;
877 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700878
879 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400880 mVertexExecutables.push_back(
881 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700882
883 stream->skip(vertexShaderSize);
884 }
885
886 const size_t pixelShaderCount = stream->readInt<unsigned int>();
887 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
888 {
889 const size_t outputCount = stream->readInt<unsigned int>();
890 std::vector<GLenum> outputs(outputCount);
891 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
892 {
893 stream->readInt(&outputs[outputIndex]);
894 }
895
Jamie Madill334d6152015-10-22 14:00:28 -0400896 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700897 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400898 ShaderExecutableD3D *shaderExecutable = nullptr;
899
900 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500901 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400902 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400903 if (error.isError())
904 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500905 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400906 }
Brandon Joneseb994362014-09-24 10:27:28 -0700907
908 if (!shaderExecutable)
909 {
Jamie Madillf6113162015-05-07 11:49:21 -0400910 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500911 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700912 }
913
914 // add new binary
915 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
916
917 stream->skip(pixelShaderSize);
918 }
919
Jamie Madill4e31ad52015-10-29 10:32:57 -0400920 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
921 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700922 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400923 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
924 if (geometryShaderSize == 0)
925 {
926 mGeometryExecutables[geometryExeIndex] = nullptr;
927 continue;
928 }
929
Brandon Joneseb994362014-09-24 10:27:28 -0700930 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400931 bool splitAttribs = (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
932
Jamie Madill28afae52015-11-09 15:07:57 -0500933 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500934 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, mStreamOutVaryings,
935 splitAttribs, &mGeometryExecutables[geometryExeIndex]);
Geoff Langb543aff2014-09-30 14:52:54 -0400936 if (error.isError())
937 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500938 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400939 }
Brandon Joneseb994362014-09-24 10:27:28 -0700940
Jamie Madill4e31ad52015-10-29 10:32:57 -0400941 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700942 {
Jamie Madillf6113162015-05-07 11:49:21 -0400943 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500944 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700945 }
946 stream->skip(geometryShaderSize);
947 }
948
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700949 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500950 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700951
Geoff Lang7dd2e102014-11-10 15:19:26 -0500952 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700953}
954
Geoff Langb543aff2014-09-30 14:52:54 -0400955gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700956{
Austin Kinross137b1512015-06-17 16:14:53 -0700957 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400958 // When we load the binary again later, we can validate the device identifier before trying to
959 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700960 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400961 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
962 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700963
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500964 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
965
Jamie Madill63805b42015-08-25 13:17:39 -0400966 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
967 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
968 {
969 stream->writeInt(mSemanticIndexes[i]);
970 }
971
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700972 stream->writeInt(mSamplersPS.size());
973 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
974 {
975 stream->writeInt(mSamplersPS[i].active);
976 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
977 stream->writeInt(mSamplersPS[i].textureType);
978 }
979
980 stream->writeInt(mSamplersVS.size());
981 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
982 {
983 stream->writeInt(mSamplersVS[i].active);
984 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
985 stream->writeInt(mSamplersVS[i].textureType);
986 }
987
988 stream->writeInt(mUsedVertexSamplerRange);
989 stream->writeInt(mUsedPixelSamplerRange);
990
Jamie Madill62d31cb2015-09-11 13:25:51 -0400991 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -0400992 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700993 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400994 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madill4a3c2342015-10-08 12:58:45 -0400995 stream->writeInt(uniform->psRegisterIndex);
996 stream->writeInt(uniform->vsRegisterIndex);
997 stream->writeInt(uniform->registerCount);
998 stream->writeInt(uniform->registerElement);
999 }
1000
1001 stream->writeInt(mD3DUniformBlocks.size());
1002 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1003 {
1004 stream->writeInt(uniformBlock.psRegisterIndex);
1005 stream->writeInt(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001006 }
1007
Jamie Madill9fc36822015-11-18 13:08:07 -05001008 stream->writeInt(mStreamOutVaryings.size());
1009 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001010 {
Brandon Joneseb994362014-09-24 10:27:28 -07001011 stream->writeString(varying.semanticName);
1012 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001013 stream->writeInt(varying.componentCount);
1014 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001015 }
1016
Brandon Jones22502d52014-08-29 16:58:36 -07001017 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001018 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1019 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001020 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001021 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1022 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001023 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001024 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001025 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001026
Brandon Joneseb994362014-09-24 10:27:28 -07001027 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001028 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001029 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1030 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001031 {
Brandon Joneseb994362014-09-24 10:27:28 -07001032 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001033 stream->writeInt(variable.type);
1034 stream->writeString(variable.name);
1035 stream->writeString(variable.source);
1036 stream->writeInt(variable.outputIndex);
1037 }
1038
Jamie Madill4e31ad52015-10-29 10:32:57 -04001039 stream->writeString(mGeometryShaderPreamble);
1040
Brandon Joneseb994362014-09-24 10:27:28 -07001041 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001042 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1043 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001044 {
1045 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1046
Jamie Madilld3dfda22015-07-06 08:28:49 -04001047 const auto &inputLayout = vertexExecutable->inputs();
1048 stream->writeInt(inputLayout.size());
1049
1050 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001051 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001052 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -07001053 }
1054
1055 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1056 stream->writeInt(vertexShaderSize);
1057
1058 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1059 stream->writeBytes(vertexBlob, vertexShaderSize);
1060 }
1061
1062 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001063 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1064 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001065 {
1066 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1067
1068 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1069 stream->writeInt(outputs.size());
1070 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1071 {
1072 stream->writeInt(outputs[outputIndex]);
1073 }
1074
1075 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1076 stream->writeInt(pixelShaderSize);
1077
1078 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1079 stream->writeBytes(pixelBlob, pixelShaderSize);
1080 }
1081
Jamie Madill4e31ad52015-10-29 10:32:57 -04001082 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001083 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001084 if (geometryExe == nullptr)
1085 {
1086 stream->writeInt(0);
1087 continue;
1088 }
1089
1090 size_t geometryShaderSize = geometryExe->getLength();
1091 stream->writeInt(geometryShaderSize);
1092 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001093 }
1094
Geoff Langb543aff2014-09-30 14:52:54 -04001095 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001096}
1097
Jamie Madill334d6152015-10-22 14:00:28 -04001098gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1099 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001100{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001101 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001102
Jamie Madill85a18042015-03-05 15:41:41 -05001103 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill334d6152015-10-22 14:00:28 -04001104 const gl::AttachmentList &colorbuffers =
1105 fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -07001106
1107 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1108 {
1109 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1110
1111 if (colorbuffer)
1112 {
Jamie Madill334d6152015-10-22 14:00:28 -04001113 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1114 ? GL_COLOR_ATTACHMENT0
1115 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001116 }
1117 else
1118 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001119 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001120 }
1121 }
1122
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001123 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001124}
1125
Jamie Madill97399232014-12-23 12:31:15 -05001126gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001127 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001128 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001129{
1130 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1131 {
1132 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1133 {
Geoff Langb543aff2014-09-30 14:52:54 -04001134 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1135 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001136 }
1137 }
1138
Jamie Madill334d6152015-10-22 14:00:28 -04001139 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1140 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001141
1142 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001143 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001144
1145 gl::InfoLog tempInfoLog;
1146 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1147
Jamie Madillada9ecc2015-08-17 12:53:37 -04001148 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001149 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001150 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
1151 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001152 if (error.isError())
1153 {
1154 return error;
1155 }
Brandon Joneseb994362014-09-24 10:27:28 -07001156
Jamie Madill97399232014-12-23 12:31:15 -05001157 if (pixelExecutable)
1158 {
1159 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1160 }
1161 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001162 {
1163 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001164 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001165 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1166 }
Brandon Jones22502d52014-08-29 16:58:36 -07001167
Geoff Langb543aff2014-09-30 14:52:54 -04001168 *outExectuable = pixelExecutable;
1169 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001170}
1171
Jamie Madilld3dfda22015-07-06 08:28:49 -04001172gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001173 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001174 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001175{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001176 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001177
1178 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1179 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001180 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001181 {
Geoff Langb543aff2014-09-30 14:52:54 -04001182 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1183 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001184 }
1185 }
1186
Brandon Jones22502d52014-08-29 16:58:36 -07001187 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001188 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1189 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001190
1191 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001192 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001193
1194 gl::InfoLog tempInfoLog;
1195 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1196
Jamie Madillada9ecc2015-08-17 12:53:37 -04001197 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001198 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001199 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1200 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001201 if (error.isError())
1202 {
1203 return error;
1204 }
1205
Jamie Madill97399232014-12-23 12:31:15 -05001206 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001207 {
Jamie Madill334d6152015-10-22 14:00:28 -04001208 mVertexExecutables.push_back(
1209 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001210 }
Jamie Madill97399232014-12-23 12:31:15 -05001211 else if (!infoLog)
1212 {
1213 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001214 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001215 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1216 }
Brandon Jones22502d52014-08-29 16:58:36 -07001217
Geoff Langb543aff2014-09-30 14:52:54 -04001218 *outExectuable = vertexExecutable;
1219 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001220}
1221
Jamie Madill4e31ad52015-10-29 10:32:57 -04001222gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Data &data,
1223 GLenum drawMode,
1224 ShaderExecutableD3D **outExecutable,
1225 gl::InfoLog *infoLog)
1226{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001227 if (outExecutable)
1228 {
1229 *outExecutable = nullptr;
1230 }
1231
1232 // We only uses a geometry shader for point sprite emulation, or for fixing the provoking
1233 // vertex problem. Otherwise, return a null shader.
1234 if (drawMode != GL_POINTS && !mUsesFlatInterpolation)
1235 {
1236 return gl::Error(GL_NO_ERROR);
1237 }
1238
Jamie Madill4e31ad52015-10-29 10:32:57 -04001239 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1240
1241 if (mGeometryExecutables[geometryShaderType] != nullptr)
1242 {
1243 if (outExecutable)
1244 {
1245 *outExecutable = mGeometryExecutables[geometryShaderType];
1246 }
1247 return gl::Error(GL_NO_ERROR);
1248 }
1249
1250 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
1251 geometryShaderType, data, mData, mGeometryShaderPreamble);
1252
1253 gl::InfoLog tempInfoLog;
1254 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1255
1256 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001257 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001258 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
1259 &mGeometryExecutables[geometryShaderType]);
1260
1261 if (!infoLog && error.isError())
1262 {
1263 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1264 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1265 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1266 }
1267
1268 if (outExecutable)
1269 {
1270 *outExecutable = mGeometryExecutables[geometryShaderType];
1271 }
1272 return error;
1273}
1274
1275LinkResult ProgramD3D::compileProgramExecutables(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001276{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001277 const gl::InputLayout &defaultInputLayout =
1278 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001279 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001280 gl::Error error =
1281 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001282 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001283 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001284 return LinkResult(false, error);
1285 }
Austin Kinross434953e2015-02-20 10:49:51 -08001286
Jamie Madill334d6152015-10-22 14:00:28 -04001287 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001288 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001289 error =
1290 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001291 if (error.isError())
1292 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001293 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001294 }
Brandon Jones44151a92014-09-10 11:32:25 -07001295
Jamie Madill4e31ad52015-10-29 10:32:57 -04001296 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001297 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001298 {
Jamie Madill57d116a2015-11-20 17:59:18 +00001299 getGeometryExecutableForPrimitiveType(data, GL_POINTS, nullptr, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001300 }
1301
Jamie Madill57d116a2015-11-20 17:59:18 +00001302#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Jamie Madillca03b352015-09-02 12:38:13 -04001303 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
Jamie Madill57d116a2015-11-20 17:59:18 +00001304 if (usesGeometryShader() && mGeometryExecutable)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001305 {
Jamie Madill334d6152015-10-22 14:00:28 -04001306 // Geometry shaders are currently only used internally, so there is no corresponding shader
1307 // object at the interface level. For now the geometry shader debug info is prepended to
1308 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001309 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill57d116a2015-11-20 17:59:18 +00001310 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001311 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1312 }
1313
1314 if (defaultVertexExecutable)
1315 {
1316 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1317 }
1318
1319 if (defaultPixelExecutable)
1320 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001321 const ShaderD3D *fragmentShaderD3D =
1322 GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001323 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1324 }
Jamie Madill57d116a2015-11-20 17:59:18 +00001325#endif
Tibor den Ouden97049c62014-10-06 21:39:16 +02001326
Jamie Madill57d116a2015-11-20 17:59:18 +00001327 bool linkSuccess =
1328 (defaultVertexExecutable && defaultPixelExecutable &&
1329 (!usesGeometryShader(GL_POINTS) || mGeometryExecutables[gl::PRIMITIVE_POINTS]));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001330 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001331}
1332
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001333LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001334{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001335 reset();
1336
1337 // TODO(jmadill): structures containing samplers
1338 for (const gl::LinkedUniform &linkedUniform : mData.getUniforms())
1339 {
1340 if (linkedUniform.isSampler() && linkedUniform.isField())
1341 {
1342 infoLog << "Structures containing samplers not currently supported in D3D.";
1343 return LinkResult(false, gl::Error(GL_NO_ERROR));
1344 }
1345 }
1346
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001347 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1348 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001349
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001350 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1351 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1352
Jamie Madill63069df2015-09-01 17:26:41 +00001353 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001354 mSamplersPS.resize(data.caps->maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001355
Arun Patole44efa0b2015-03-04 17:11:05 +05301356 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001357 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1358
Austin Kinross02df7962015-07-01 10:03:42 -07001359 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1360 {
1361 if (fragmentShaderD3D->usesFrontFacing())
1362 {
1363 infoLog << "The current renderer doesn't support gl_FrontFacing";
1364 return LinkResult(false, gl::Error(GL_NO_ERROR));
1365 }
1366 }
1367
Jamie Madillca03b352015-09-02 12:38:13 -04001368 std::vector<PackedVarying> packedVaryings =
1369 MergeVaryings(*vertexShader, *fragmentShader, mData.getTransformFeedbackVaryingNames());
1370
Brandon Jones22502d52014-08-29 16:58:36 -07001371 // Map the varyings to the register file
Jamie Madill9fc36822015-11-18 13:08:07 -05001372 VaryingPacking varyingPacking(data.caps->maxVaryingVectors);
1373 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
1374 mData.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001375 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001376 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001377 }
1378
Jamie Madille39a3f02015-11-17 20:42:15 -05001379 ProgramD3DMetadata metadata(mRenderer->getMajorShaderModel(), mRenderer->getShaderModelSuffix(),
1380 usesInstancedPointSpriteEmulation(), vertexShaderD3D,
1381 fragmentShaderD3D);
1382
Jamie Madill9fc36822015-11-18 13:08:07 -05001383 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1384 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1385
1386 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.caps->maxVaryingVectors)
1387 {
1388 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1389 return LinkResult(false, gl::Error(GL_NO_ERROR));
1390 }
1391
1392 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1393 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1394 // intelligently, but D3D9 assumes one semantic per register.
1395 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1396 varyingPacking.getMaxSemanticIndex() > data.caps->maxVaryingVectors)
1397 {
1398 infoLog << "Cannot pack these varyings on D3D9.";
1399 return LinkResult(false, gl::Error(GL_NO_ERROR));
1400 }
1401
1402 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mData, metadata, varyingPacking, &mPixelHLSL,
1403 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001404 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001405 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001406 }
1407
Brandon Jones44151a92014-09-10 11:32:25 -07001408 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madille39a3f02015-11-17 20:42:15 -05001409 mDynamicHLSL->getPixelShaderOutputKey(data, mData, metadata, &mPixelShaderKey);
1410 mUsesFragDepth = metadata.usesFragDepth(mData);
Brandon Jones44151a92014-09-10 11:32:25 -07001411
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001412 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001413 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001414 for (const auto &varying : packedVaryings)
1415 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001416 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001417 {
1418 mUsesFlatInterpolation = true;
1419 break;
1420 }
1421 }
1422
Jamie Madill4e31ad52015-10-29 10:32:57 -04001423 if (mRenderer->getMajorShaderModel() >= 4)
1424 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001425 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1426 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001427 }
1428
Jamie Madill63805b42015-08-25 13:17:39 -04001429 initSemanticIndex();
Jamie Madill437d2662014-12-05 14:23:35 -05001430
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001431 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001432
Jamie Madill9fc36822015-11-18 13:08:07 -05001433 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001434
Jamie Madill4e31ad52015-10-29 10:32:57 -04001435 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001436 if (result.error.isError() || !result.linkSuccess)
1437 {
1438 infoLog << "Failed to create D3D shaders.";
1439 return result;
1440 }
1441
Jamie Madill4a3c2342015-10-08 12:58:45 -04001442 initUniformBlockInfo();
1443
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001445}
1446
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001447GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001448{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001449 // TODO(jmadill): Do something useful here?
1450 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001451}
1452
Jamie Madill4a3c2342015-10-08 12:58:45 -04001453void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001454{
1455 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1456
Jamie Madill62d31cb2015-09-11 13:25:51 -04001457 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1458 {
1459 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1460 continue;
1461
Jamie Madill4a3c2342015-10-08 12:58:45 -04001462 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001463 continue;
1464
Jamie Madill4a3c2342015-10-08 12:58:45 -04001465 size_t dataSize = getUniformBlockInfo(vertexBlock);
1466 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001467 }
1468
1469 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1470
1471 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1472 {
1473 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1474 continue;
1475
Jamie Madill4a3c2342015-10-08 12:58:45 -04001476 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001477 continue;
1478
Jamie Madill4a3c2342015-10-08 12:58:45 -04001479 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1480 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001481 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001482}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001483
Jamie Madill4a3c2342015-10-08 12:58:45 -04001484void ProgramD3D::assignUniformBlockRegisters()
1485{
1486 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001487
1488 // Assign registers and update sizes.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001489 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1490 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001491
Jamie Madill4a3c2342015-10-08 12:58:45 -04001492 for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001493 {
1494 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1495
Jamie Madill4a3c2342015-10-08 12:58:45 -04001496 D3DUniformBlock d3dUniformBlock;
1497
Jamie Madill62d31cb2015-09-11 13:25:51 -04001498 if (uniformBlock.vertexStaticUse)
1499 {
1500 unsigned int baseRegister =
1501 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001502 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001503 }
1504
1505 if (uniformBlock.fragmentStaticUse)
1506 {
1507 unsigned int baseRegister =
1508 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001509 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001510 }
1511
Jamie Madill4a3c2342015-10-08 12:58:45 -04001512 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001513 }
1514}
1515
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001516void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001517{
1518 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001519 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001520 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001521 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001522 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001523 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001524 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001525 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001526 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001527 vertexRegisters = std::max(vertexRegisters,
1528 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001529 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001530 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001531 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001532 fragmentRegisters = std::max(
1533 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001534 }
1535 }
1536 }
1537
Jamie Madill334d6152015-10-22 14:00:28 -04001538 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001539 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1540}
1541
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001542gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001543{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001544 updateSamplerMapping();
1545
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001546 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001547 if (error.isError())
1548 {
1549 return error;
1550 }
1551
Jamie Madill62d31cb2015-09-11 13:25:51 -04001552 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001553 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001554 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001555 }
1556
1557 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001558}
1559
Jamie Madilld1fe1642015-08-21 16:26:04 -04001560gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001561{
Jamie Madill4a3c2342015-10-08 12:58:45 -04001562 if (mData.getUniformBlocks().empty())
1563 {
1564 return gl::Error(GL_NO_ERROR);
1565 }
1566
1567 // Lazy init.
1568 if (mD3DUniformBlocks.empty())
1569 {
1570 assignUniformBlockRegisters();
1571 }
1572
Jamie Madill03260fa2015-06-22 13:57:22 -04001573 mVertexUBOCache.clear();
1574 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001575
1576 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1577 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1578
Jamie Madill4a3c2342015-10-08 12:58:45 -04001579 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001580 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001581 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001582 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
1583 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001584
Brandon Jones18bd4102014-09-22 14:21:44 -07001585 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001586 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001587 {
1588 continue;
1589 }
1590
Jamie Madill4a3c2342015-10-08 12:58:45 -04001591 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001592 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001593 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001594 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001595
Jamie Madill969194d2015-07-20 14:36:56 -04001596 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001597 {
1598 mVertexUBOCache.resize(registerIndex + 1, -1);
1599 }
1600
1601 ASSERT(mVertexUBOCache[registerIndex] == -1);
1602 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001603 }
1604
Jamie Madill4a3c2342015-10-08 12:58:45 -04001605 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001606 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001607 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001608 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001609
1610 if (mFragmentUBOCache.size() <= registerIndex)
1611 {
1612 mFragmentUBOCache.resize(registerIndex + 1, -1);
1613 }
1614
1615 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1616 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001617 }
1618 }
1619
Jamie Madill03260fa2015-06-22 13:57:22 -04001620 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001621}
1622
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001623void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001624{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001625 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001626 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001627 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001628 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001629}
1630
Jamie Madill334d6152015-10-22 14:00:28 -04001631void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001632{
1633 setUniform(location, count, v, GL_FLOAT);
1634}
1635
1636void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1637{
1638 setUniform(location, count, v, GL_FLOAT_VEC2);
1639}
1640
1641void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1642{
1643 setUniform(location, count, v, GL_FLOAT_VEC3);
1644}
1645
1646void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1647{
1648 setUniform(location, count, v, GL_FLOAT_VEC4);
1649}
1650
Jamie Madill334d6152015-10-22 14:00:28 -04001651void ProgramD3D::setUniformMatrix2fv(GLint location,
1652 GLsizei count,
1653 GLboolean transpose,
1654 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001655{
1656 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1657}
1658
Jamie Madill334d6152015-10-22 14:00:28 -04001659void ProgramD3D::setUniformMatrix3fv(GLint location,
1660 GLsizei count,
1661 GLboolean transpose,
1662 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001663{
1664 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1665}
1666
Jamie Madill334d6152015-10-22 14:00:28 -04001667void ProgramD3D::setUniformMatrix4fv(GLint location,
1668 GLsizei count,
1669 GLboolean transpose,
1670 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001671{
1672 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1673}
1674
Jamie Madill334d6152015-10-22 14:00:28 -04001675void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1676 GLsizei count,
1677 GLboolean transpose,
1678 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001679{
1680 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1681}
1682
Jamie Madill334d6152015-10-22 14:00:28 -04001683void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1684 GLsizei count,
1685 GLboolean transpose,
1686 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001687{
1688 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1689}
1690
Jamie Madill334d6152015-10-22 14:00:28 -04001691void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1692 GLsizei count,
1693 GLboolean transpose,
1694 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001695{
1696 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1697}
1698
Jamie Madill334d6152015-10-22 14:00:28 -04001699void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1700 GLsizei count,
1701 GLboolean transpose,
1702 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001703{
1704 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1705}
1706
Jamie Madill334d6152015-10-22 14:00:28 -04001707void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1708 GLsizei count,
1709 GLboolean transpose,
1710 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001711{
1712 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1713}
1714
Jamie Madill334d6152015-10-22 14:00:28 -04001715void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1716 GLsizei count,
1717 GLboolean transpose,
1718 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001719{
1720 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1721}
1722
1723void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1724{
1725 setUniform(location, count, v, GL_INT);
1726}
1727
1728void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1729{
1730 setUniform(location, count, v, GL_INT_VEC2);
1731}
1732
1733void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1734{
1735 setUniform(location, count, v, GL_INT_VEC3);
1736}
1737
1738void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1739{
1740 setUniform(location, count, v, GL_INT_VEC4);
1741}
1742
1743void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1744{
1745 setUniform(location, count, v, GL_UNSIGNED_INT);
1746}
1747
1748void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1749{
1750 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1751}
1752
1753void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1754{
1755 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1756}
1757
1758void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1759{
1760 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1761}
1762
Jamie Madill4a3c2342015-10-08 12:58:45 -04001763void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1764 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001765{
1766}
1767
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001768void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001769{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001770 D3DUniformMap uniformMap;
Jamie Madill334d6152015-10-22 14:00:28 -04001771 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001772 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001773
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001774 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001775 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001776 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001777 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001778 }
1779 }
1780
Jamie Madill334d6152015-10-22 14:00:28 -04001781 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001782 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001783 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001784 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001785 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001786 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001787 }
1788 }
1789
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001790 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
1791 for (const gl::LinkedUniform &glUniform : mData.getUniforms())
1792 {
1793 if (!glUniform.isInDefaultBlock())
1794 continue;
1795
1796 auto mapEntry = uniformMap.find(glUniform.name);
1797 ASSERT(mapEntry != uniformMap.end());
1798 mD3DUniforms.push_back(mapEntry->second);
1799 }
1800
Jamie Madill62d31cb2015-09-11 13:25:51 -04001801 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001802 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001803}
1804
Jamie Madill91445bc2015-09-23 16:47:53 -04001805void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001806 const sh::Uniform &uniform,
1807 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001808{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001809 if (uniform.isBuiltIn())
Jamie Madillfb536032015-09-11 13:19:49 -04001810 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001811 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001812 return;
1813 }
1814
Jamie Madill91445bc2015-09-23 16:47:53 -04001815 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1816
1817 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1818 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001819 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001820 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001821
Jamie Madill91445bc2015-09-23 16:47:53 -04001822 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001823}
1824
Jamie Madill62d31cb2015-09-11 13:25:51 -04001825D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1826{
1827 for (D3DUniform *d3dUniform : mD3DUniforms)
1828 {
1829 if (d3dUniform->name == name)
1830 {
1831 return d3dUniform;
1832 }
1833 }
1834
1835 return nullptr;
1836}
1837
Jamie Madill91445bc2015-09-23 16:47:53 -04001838void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001839 const sh::ShaderVariable &uniform,
1840 const std::string &fullName,
1841 sh::HLSLBlockEncoder *encoder,
1842 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001843{
1844 if (uniform.isStruct())
1845 {
1846 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1847 {
1848 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1849
Jamie Madill55def582015-05-04 11:24:57 -04001850 if (encoder)
1851 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001852
1853 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1854 {
Jamie Madill334d6152015-10-22 14:00:28 -04001855 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001856 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1857
Jamie Madill91445bc2015-09-23 16:47:53 -04001858 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001859 }
1860
Jamie Madill55def582015-05-04 11:24:57 -04001861 if (encoder)
1862 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001863 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001864 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001865 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001866
1867 // Not a struct. Arrays are treated as aggregate types.
1868 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001869 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001870 encoder->enterAggregateType();
1871 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001872
Jamie Madill62d31cb2015-09-11 13:25:51 -04001873 // Advance the uniform offset, to track registers allocation for structs
1874 sh::BlockMemberInfo blockInfo =
1875 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1876 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001877
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001878 auto uniformMapEntry = uniformMap->find(fullName);
1879 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001880
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001881 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001882 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001883 d3dUniform = uniformMapEntry->second;
1884 }
1885 else
1886 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001887 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001888 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001889 }
1890
1891 if (encoder)
1892 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001893 d3dUniform->registerElement =
1894 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001895 unsigned int reg =
1896 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001897 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001898 {
1899 d3dUniform->psRegisterIndex = reg;
1900 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001901 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001902 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001903 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001904 d3dUniform->vsRegisterIndex = reg;
1905 }
Jamie Madillfb536032015-09-11 13:19:49 -04001906
1907 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001908 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001909 {
1910 encoder->exitAggregateType();
1911 }
1912 }
1913}
1914
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001915template <typename T>
Jamie Madill334d6152015-10-22 14:00:28 -04001916static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001917{
1918 ASSERT(dest != NULL);
1919 ASSERT(dirtyFlag != NULL);
1920
1921 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
Jamie Madill334d6152015-10-22 14:00:28 -04001922 *dest = source;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001923}
1924
1925template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001926void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001927{
Jamie Madill334d6152015-10-22 14:00:28 -04001928 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001929 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1930
Jamie Madill62d31cb2015-09-11 13:25:51 -04001931 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001932
Jamie Madill62d31cb2015-09-11 13:25:51 -04001933 unsigned int elementCount = targetUniform->elementCount();
1934 unsigned int arrayElement = mData.getUniformLocations()[location].element;
1935 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001936
1937 if (targetUniform->type == targetUniformType)
1938 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001939 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001940
Jamie Madill62d31cb2015-09-11 13:25:51 -04001941 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001942 {
Jamie Madill334d6152015-10-22 14:00:28 -04001943 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001944 const T *source = v + (i * components);
1945
1946 for (int c = 0; c < components; c++)
1947 {
1948 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1949 }
1950 for (int c = components; c < 4; c++)
1951 {
1952 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1953 }
1954 }
1955 }
1956 else if (targetUniform->type == targetBoolType)
1957 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001958 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001959
Jamie Madill62d31cb2015-09-11 13:25:51 -04001960 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001961 {
Jamie Madill334d6152015-10-22 14:00:28 -04001962 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001963 const T *source = v + (i * components);
1964
1965 for (int c = 0; c < components; c++)
1966 {
Jamie Madill334d6152015-10-22 14:00:28 -04001967 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1968 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001969 }
1970 for (int c = components; c < 4; c++)
1971 {
1972 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1973 }
1974 }
1975 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001976 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001977 {
1978 ASSERT(targetUniformType == GL_INT);
1979
Jamie Madill62d31cb2015-09-11 13:25:51 -04001980 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001981
1982 bool wasDirty = targetUniform->dirty;
1983
Jamie Madill62d31cb2015-09-11 13:25:51 -04001984 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001985 {
Jamie Madill334d6152015-10-22 14:00:28 -04001986 GLint *dest = target + (i * 4);
1987 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001988
1989 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1990 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1991 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1992 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1993 }
1994
1995 if (!wasDirty && targetUniform->dirty)
1996 {
1997 mDirtySamplerMapping = true;
1998 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001999 }
Jamie Madill334d6152015-10-22 14:00:28 -04002000 else
2001 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002002}
2003
2004template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002005void ProgramD3D::setUniformMatrixfv(GLint location,
2006 GLsizei countIn,
2007 GLboolean transpose,
2008 const GLfloat *value,
2009 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002010{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002011 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002012
Jamie Madill62d31cb2015-09-11 13:25:51 -04002013 unsigned int elementCount = targetUniform->elementCount();
2014 unsigned int arrayElement = mData.getUniformLocations()[location].element;
2015 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002016
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002018 GLfloat *target =
2019 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002020
Jamie Madill62d31cb2015-09-11 13:25:51 -04002021 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002022 {
2023 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2024 if (transpose == GL_FALSE)
2025 {
Jamie Madill334d6152015-10-22 14:00:28 -04002026 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2027 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002028 }
2029 else
2030 {
Jamie Madill334d6152015-10-22 14:00:28 -04002031 targetUniform->dirty =
2032 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002033 }
2034 target += targetMatrixStride;
2035 value += cols * rows;
2036 }
2037}
2038
Jamie Madill4a3c2342015-10-08 12:58:45 -04002039size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002040{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002041 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002042
Jamie Madill62d31cb2015-09-11 13:25:51 -04002043 // define member uniforms
2044 sh::Std140BlockEncoder std140Encoder;
2045 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2046 sh::BlockLayoutEncoder *encoder = nullptr;
2047
2048 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002049 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002050 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002051 }
2052 else
2053 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002054 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002055 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002056
2057 GetUniformBlockInfo(interfaceBlock.fields, "", encoder, interfaceBlock.isRowMajorLayout,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002058 &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002059
2060 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002061}
2062
Jamie Madill62d31cb2015-09-11 13:25:51 -04002063void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002064{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002065 for (const D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002066 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002067 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002068 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002069 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002070 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002071 }
2072}
2073
Jamie Madill62d31cb2015-09-11 13:25:51 -04002074void ProgramD3D::assignSamplerRegisters(const D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002075{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002076 ASSERT(d3dUniform->isSampler());
2077 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX ||
2078 d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madillfb536032015-09-11 13:19:49 -04002079
Jamie Madill62d31cb2015-09-11 13:25:51 -04002080 if (d3dUniform->vsRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002081 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002082 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2083 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002084 }
2085
Jamie Madill62d31cb2015-09-11 13:25:51 -04002086 if (d3dUniform->psRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002087 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002088 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2089 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002090 }
2091}
2092
Jamie Madill62d31cb2015-09-11 13:25:51 -04002093// static
2094void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002095 GLenum samplerType,
2096 unsigned int samplerCount,
2097 std::vector<Sampler> &outSamplers,
2098 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002099{
2100 unsigned int samplerIndex = startSamplerIndex;
2101
2102 do
2103 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002104 ASSERT(samplerIndex < outSamplers.size());
2105 Sampler *sampler = &outSamplers[samplerIndex];
2106 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002107 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002108 sampler->logicalTextureUnit = 0;
2109 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002110 samplerIndex++;
2111 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002112}
2113
Brandon Jonesc9610c52014-08-25 17:02:59 -07002114void ProgramD3D::reset()
2115{
Brandon Joneseb994362014-09-24 10:27:28 -07002116 SafeDeleteContainer(mVertexExecutables);
2117 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002118
2119 for (auto &element : mGeometryExecutables)
2120 {
2121 SafeDelete(element);
2122 }
Brandon Joneseb994362014-09-24 10:27:28 -07002123
Brandon Jones22502d52014-08-29 16:58:36 -07002124 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002125 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002126
2127 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002128 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002129 mUsesFragDepth = false;
2130 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002131 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002132 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002133
Jamie Madill62d31cb2015-09-11 13:25:51 -04002134 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002135 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002136
Brandon Jonesc9610c52014-08-25 17:02:59 -07002137 SafeDelete(mVertexUniformStorage);
2138 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002139
2140 mSamplersPS.clear();
2141 mSamplersVS.clear();
2142
2143 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002144 mUsedPixelSamplerRange = 0;
2145 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002146
Jamie Madill63805b42015-08-25 13:17:39 -04002147 std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1);
Jamie Madill437d2662014-12-05 14:23:35 -05002148 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002149
Jamie Madill9fc36822015-11-18 13:08:07 -05002150 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002151
2152 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002153}
2154
Geoff Lang7dd2e102014-11-10 15:19:26 -05002155unsigned int ProgramD3D::getSerial() const
2156{
2157 return mSerial;
2158}
2159
2160unsigned int ProgramD3D::issueSerial()
2161{
2162 return mCurrentSerial++;
2163}
2164
Jamie Madill63805b42015-08-25 13:17:39 -04002165void ProgramD3D::initSemanticIndex()
2166{
2167 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2168 ASSERT(vertexShader != nullptr);
2169
2170 // Init semantic index
2171 for (const sh::Attribute &attribute : mData.getAttributes())
2172 {
2173 int attributeIndex = attribute.location;
2174 int index = vertexShader->getSemanticIndex(attribute.name);
2175 int regs = gl::VariableRegisterCount(attribute.type);
2176
2177 for (int reg = 0; reg < regs; ++reg)
2178 {
2179 mSemanticIndexes[attributeIndex + reg] = index + reg;
2180 }
2181 }
2182
2183 initAttributesByLayout();
2184}
2185
Jamie Madill437d2662014-12-05 14:23:35 -05002186void ProgramD3D::initAttributesByLayout()
2187{
2188 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2189 {
2190 mAttributesByLayout[i] = i;
2191 }
2192
Jamie Madill63805b42015-08-25 13:17:39 -04002193 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS],
2194 AttributeSorter(mSemanticIndexes));
Jamie Madill437d2662014-12-05 14:23:35 -05002195}
2196
Jamie Madill334d6152015-10-22 14:00:28 -04002197void ProgramD3D::sortAttributesByLayout(
2198 const std::vector<TranslatedAttribute> &unsortedAttributes,
2199 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2200 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002201{
Jamie Madill476682e2015-06-30 10:04:29 -04002202 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002203 {
Jamie Madill334d6152015-10-22 14:00:28 -04002204 int oldIndex = mAttributesByLayout[attribIndex];
Jamie Madill63805b42015-08-25 13:17:39 -04002205 sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex];
Jamie Madill334d6152015-10-22 14:00:28 -04002206 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002207 }
2208}
2209
Jamie Madill63805b42015-08-25 13:17:39 -04002210void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002211{
Jamie Madillbd136f92015-08-10 14:51:37 -04002212 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002213 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002214
Dian Xianga4928832015-09-15 10:11:17 -07002215 for (unsigned int attributeIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002216 {
Jamie Madill63805b42015-08-25 13:17:39 -04002217 int semanticIndex = mSemanticIndexes[attributeIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002218
2219 if (semanticIndex != -1)
2220 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002221 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2222 {
2223 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2224 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002225 mCachedInputLayout[semanticIndex] =
2226 GetVertexFormatType(vertexAttributes[attributeIndex],
2227 state.getVertexAttribCurrentValue(attributeIndex).Type);
2228 }
2229 }
2230}
2231
Jamie Madill9fc36822015-11-18 13:08:07 -05002232void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002233{
Jamie Madill9fc36822015-11-18 13:08:07 -05002234 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2235
2236 const std::string &varyingSemantic =
2237 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2238
Jamie Madillccdf74b2015-08-18 10:46:12 -04002239 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002240 mStreamOutVaryings.clear();
2241
2242 const auto &tfVaryingNames = mData.getTransformFeedbackVaryingNames();
2243 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2244 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002245 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002246 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2247 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002248 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002249 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002250 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002251 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2252 builtins.glPosition.index, 4, outputSlot));
2253 }
2254 }
2255 else if (tfVaryingName == "gl_FragCoord")
2256 {
2257 if (builtins.glFragCoord.enabled)
2258 {
2259 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2260 builtins.glFragCoord.index, 4, outputSlot));
2261 }
2262 }
2263 else if (tfVaryingName == "gl_PointSize")
2264 {
2265 if (builtins.glPointSize.enabled)
2266 {
2267 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2268 }
2269 }
2270 else
2271 {
2272 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2273 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002274 const auto &varying = *registerInfo.packedVarying->varying;
2275 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002276 int componentCount = gl::VariableColumnCount(transposedType);
2277 ASSERT(!varying.isBuiltIn());
2278
Jamie Madill55c25d02015-11-18 13:08:08 -05002279 // Transform feedback for varying structs is underspecified.
2280 // See Khronos bug 9856.
2281 // TODO(jmadill): Figure out how to be spec-compliant here.
2282 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2283 continue;
2284
Jamie Madill9fc36822015-11-18 13:08:07 -05002285 // There can be more than one register assigned to a particular varying, and each
2286 // register needs its own stream out entry.
2287 if (tfVaryingName == varying.name)
2288 {
2289 mStreamOutVaryings.push_back(D3DVarying(
2290 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2291 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002292 }
2293 }
2294 }
2295}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002296
2297D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2298{
2299 return mD3DUniforms[mData.getUniformLocations()[location].index];
2300}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002301
2302bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2303{
2304 std::string baseName = blockName;
2305 gl::ParseAndStripArrayIndex(&baseName);
2306
2307 auto sizeIter = mBlockDataSizes.find(baseName);
2308 if (sizeIter == mBlockDataSizes.end())
2309 {
2310 *sizeOut = 0;
2311 return false;
2312 }
2313
2314 *sizeOut = sizeIter->second;
2315 return true;
2316}
2317
2318bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2319 sh::BlockMemberInfo *memberInfoOut) const
2320{
2321 auto infoIter = mBlockInfo.find(memberUniformName);
2322 if (infoIter == mBlockInfo.end())
2323 {
2324 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2325 return false;
2326 }
2327
2328 *memberInfoOut = infoIter->second;
2329 return true;
2330}
Brandon Jonesc9610c52014-08-25 17:02:59 -07002331}