blob: 2ca56c9e899f4d4087f3cbf536a7c8fb7981fb56 [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{
Jamie Madill63286672015-11-24 13:00:08 -0500416 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500417}
418
419bool ProgramD3DMetadata::usesPointCoord() const
420{
421 return mFragmentShader->usesPointCoord();
422}
423
424bool ProgramD3DMetadata::usesFragCoord() const
425{
426 return mFragmentShader->usesFragCoord();
427}
428
429bool ProgramD3DMetadata::usesPointSize() const
430{
431 return mVertexShader->usesPointSize();
432}
433
434bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
435{
436 return !usesPointSize() && usesPointCoord() && mRendererMajorShaderModel >= 4;
437}
438
439bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
440{
441 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader
442 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
443 // GeometryShader PointSprite emulation does not require this additional entry because the
444 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the
445 // PS_INPUT of the generated pixel shader. The Geometry Shader point sprite implementation needs
446 // gl_PointSize to be in VS_OUTPUT and GS_INPUT. Instanced point sprites doesn't need
447 // gl_PointSize in VS_OUTPUT.
448 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
449 usesInsertedPointCoordValue();
450}
451
452bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
453{
454 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
455 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
456 // the vertex shader in this case. This saves us 1 output vector.
457 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
458}
459
460bool ProgramD3DMetadata::usesSystemValuePointSize() const
461{
462 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
463}
464
465bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
466{
467 return mFragmentShader->usesMultipleRenderTargets();
468}
469
470GLint ProgramD3DMetadata::getMajorShaderVersion() const
471{
472 return mVertexShader->getData().getShaderVersion();
473}
474
475const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
476{
477 return mFragmentShader;
478}
479
Jamie Madill28afae52015-11-09 15:07:57 -0500480// ProgramD3D Implementation
481
Jamie Madilld3dfda22015-07-06 08:28:49 -0400482ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
483 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500484 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400485 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700486{
Brandon Joneseb994362014-09-24 10:27:28 -0700487}
488
489ProgramD3D::VertexExecutable::~VertexExecutable()
490{
491 SafeDelete(mShaderExecutable);
492}
493
Jamie Madilld3dfda22015-07-06 08:28:49 -0400494// static
495void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
496 const gl::InputLayout &inputLayout,
497 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700498{
Jamie Madillbd136f92015-08-10 14:51:37 -0400499 signatureOut->resize(inputLayout.size());
Jamie Madilld3dfda22015-07-06 08:28:49 -0400500
501 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700502 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400503 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbd136f92015-08-10 14:51:37 -0400504 bool converted = false;
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400505 if (vertexFormatType != gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700506 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400507 VertexConversionType conversionType =
508 renderer->getVertexConversionType(vertexFormatType);
Jamie Madillbd136f92015-08-10 14:51:37 -0400509 converted = ((conversionType & VERTEX_CONVERT_GPU) != 0);
Brandon Joneseb994362014-09-24 10:27:28 -0700510 }
Jamie Madillbd136f92015-08-10 14:51:37 -0400511
512 (*signatureOut)[index] = converted;
Brandon Joneseb994362014-09-24 10:27:28 -0700513 }
Brandon Joneseb994362014-09-24 10:27:28 -0700514}
515
Jamie Madilld3dfda22015-07-06 08:28:49 -0400516bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
517{
Jamie Madillbd136f92015-08-10 14:51:37 -0400518 size_t limit = std::max(mSignature.size(), signature.size());
519 for (size_t index = 0; index < limit; ++index)
520 {
521 // treat undefined indexes as 'not converted'
522 bool a = index < signature.size() ? signature[index] : false;
523 bool b = index < mSignature.size() ? mSignature[index] : false;
524 if (a != b)
525 return false;
526 }
527
528 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400529}
530
531ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
532 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400533 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700534{
535}
536
537ProgramD3D::PixelExecutable::~PixelExecutable()
538{
539 SafeDelete(mShaderExecutable);
540}
541
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700542ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
543{
544}
545
Geoff Lang7dd2e102014-11-10 15:19:26 -0500546unsigned int ProgramD3D::mCurrentSerial = 1;
547
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400548ProgramD3D::ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer)
549 : ProgramImpl(data),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700550 mRenderer(renderer),
551 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400552 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700553 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400554 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700555 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700556 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700557 mUsedVertexSamplerRange(0),
558 mUsedPixelSamplerRange(0),
559 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500560 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700561{
Brandon Joneseb994362014-09-24 10:27:28 -0700562 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700563}
564
565ProgramD3D::~ProgramD3D()
566{
567 reset();
568 SafeDelete(mDynamicHLSL);
569}
570
Brandon Jones44151a92014-09-10 11:32:25 -0700571bool ProgramD3D::usesPointSpriteEmulation() const
572{
573 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
574}
575
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400576bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700577{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400578 if (drawMode != GL_POINTS)
579 {
580 return mUsesFlatInterpolation;
581 }
582
Cooper Partine6664f02015-01-09 16:22:24 -0800583 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
584}
585
586bool ProgramD3D::usesInstancedPointSpriteEmulation() const
587{
588 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700589}
590
Jamie Madill334d6152015-10-22 14:00:28 -0400591GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
592 unsigned int samplerIndex,
593 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700594{
595 GLint logicalTextureUnit = -1;
596
597 switch (type)
598 {
Jamie Madill334d6152015-10-22 14:00:28 -0400599 case gl::SAMPLER_PIXEL:
600 ASSERT(samplerIndex < caps.maxTextureImageUnits);
601 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
602 {
603 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
604 }
605 break;
606 case gl::SAMPLER_VERTEX:
607 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
608 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
609 {
610 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
611 }
612 break;
613 default:
614 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700615 }
616
Jamie Madill334d6152015-10-22 14:00:28 -0400617 if (logicalTextureUnit >= 0 &&
618 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700619 {
620 return logicalTextureUnit;
621 }
622
623 return -1;
624}
625
626// Returns the texture type for a given Direct3D 9 sampler type and
627// index (0-15 for the pixel shader and 0-3 for the vertex shader).
628GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
629{
630 switch (type)
631 {
Jamie Madill334d6152015-10-22 14:00:28 -0400632 case gl::SAMPLER_PIXEL:
633 ASSERT(samplerIndex < mSamplersPS.size());
634 ASSERT(mSamplersPS[samplerIndex].active);
635 return mSamplersPS[samplerIndex].textureType;
636 case gl::SAMPLER_VERTEX:
637 ASSERT(samplerIndex < mSamplersVS.size());
638 ASSERT(mSamplersVS[samplerIndex].active);
639 return mSamplersVS[samplerIndex].textureType;
640 default:
641 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700642 }
643
644 return GL_TEXTURE_2D;
645}
646
647GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
648{
649 switch (type)
650 {
Jamie Madill334d6152015-10-22 14:00:28 -0400651 case gl::SAMPLER_PIXEL:
652 return mUsedPixelSamplerRange;
653 case gl::SAMPLER_VERTEX:
654 return mUsedVertexSamplerRange;
655 default:
656 UNREACHABLE();
657 return 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700658 }
659}
660
661void ProgramD3D::updateSamplerMapping()
662{
663 if (!mDirtySamplerMapping)
664 {
665 return;
666 }
667
668 mDirtySamplerMapping = false;
669
670 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400671 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700672 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400673 if (!d3dUniform->dirty)
674 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700675
Jamie Madill62d31cb2015-09-11 13:25:51 -0400676 if (!d3dUniform->isSampler())
677 continue;
678
679 int count = d3dUniform->elementCount();
680 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
681
682 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700683 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400684 unsigned int firstIndex = d3dUniform->psRegisterIndex;
685
686 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700687 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400688 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700689
Jamie Madill62d31cb2015-09-11 13:25:51 -0400690 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700691 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400692 ASSERT(mSamplersPS[samplerIndex].active);
693 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700694 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400695 }
696 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700697
Jamie Madill62d31cb2015-09-11 13:25:51 -0400698 if (d3dUniform->isReferencedByVertexShader())
699 {
700 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
701
702 for (int i = 0; i < count; i++)
703 {
704 unsigned int samplerIndex = firstIndex + i;
705
706 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700707 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400708 ASSERT(mSamplersVS[samplerIndex].active);
709 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700710 }
711 }
712 }
713 }
714}
715
Geoff Lang7dd2e102014-11-10 15:19:26 -0500716LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700717{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400718 reset();
719
Jamie Madill334d6152015-10-22 14:00:28 -0400720 DeviceIdentifier binaryDeviceIdentifier = {0};
721 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
722 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700723
724 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
725 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
726 {
727 infoLog << "Invalid program binary, device configuration has changed.";
728 return LinkResult(false, gl::Error(GL_NO_ERROR));
729 }
730
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500731 int compileFlags = stream->readInt<int>();
732 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
733 {
Jamie Madillf6113162015-05-07 11:49:21 -0400734 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500735 return LinkResult(false, gl::Error(GL_NO_ERROR));
736 }
737
Jamie Madill63805b42015-08-25 13:17:39 -0400738 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
739 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
740 {
741 stream->readInt(&mSemanticIndexes[i]);
742 }
743
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700744 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
745 for (unsigned int i = 0; i < psSamplerCount; ++i)
746 {
747 Sampler sampler;
748 stream->readBool(&sampler.active);
749 stream->readInt(&sampler.logicalTextureUnit);
750 stream->readInt(&sampler.textureType);
751 mSamplersPS.push_back(sampler);
752 }
753 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
754 for (unsigned int i = 0; i < vsSamplerCount; ++i)
755 {
756 Sampler sampler;
757 stream->readBool(&sampler.active);
758 stream->readInt(&sampler.logicalTextureUnit);
759 stream->readInt(&sampler.textureType);
760 mSamplersVS.push_back(sampler);
761 }
762
763 stream->readInt(&mUsedVertexSamplerRange);
764 stream->readInt(&mUsedPixelSamplerRange);
765
766 const unsigned int uniformCount = stream->readInt<unsigned int>();
767 if (stream->error())
768 {
Jamie Madillf6113162015-05-07 11:49:21 -0400769 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500770 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700771 }
772
Jamie Madill62d31cb2015-09-11 13:25:51 -0400773 const auto &linkedUniforms = mData.getUniforms();
774 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700775 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
776 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400777 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700778
Jamie Madill62d31cb2015-09-11 13:25:51 -0400779 D3DUniform *d3dUniform =
780 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
781 linkedUniform.isInDefaultBlock());
782 stream->readInt(&d3dUniform->psRegisterIndex);
783 stream->readInt(&d3dUniform->vsRegisterIndex);
784 stream->readInt(&d3dUniform->registerCount);
785 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700786
Jamie Madill62d31cb2015-09-11 13:25:51 -0400787 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700788 }
789
Jamie Madill4a3c2342015-10-08 12:58:45 -0400790 const unsigned int blockCount = stream->readInt<unsigned int>();
791 if (stream->error())
792 {
793 infoLog << "Invalid program binary.";
794 return LinkResult(false, gl::Error(GL_NO_ERROR));
795 }
796
797 ASSERT(mD3DUniformBlocks.empty());
798 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
799 {
800 D3DUniformBlock uniformBlock;
801 stream->readInt(&uniformBlock.psRegisterIndex);
802 stream->readInt(&uniformBlock.vsRegisterIndex);
803 mD3DUniformBlocks.push_back(uniformBlock);
804 }
805
Jamie Madill9fc36822015-11-18 13:08:07 -0500806 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
807 mStreamOutVaryings.resize(streamOutVaryingCount);
808 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700809 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500810 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700811
Jamie Madill28afae52015-11-09 15:07:57 -0500812 stream->readString(&varying->semanticName);
813 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500814 stream->readInt(&varying->componentCount);
815 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700816 }
817
Brandon Jones22502d52014-08-29 16:58:36 -0700818 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400819 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
820 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700821 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400822 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
823 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700824 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700825 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400826 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700827
828 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
829 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400830 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
831 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700832 {
833 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
834 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
835 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
836 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
837 }
838
Jamie Madill4e31ad52015-10-29 10:32:57 -0400839 stream->readString(&mGeometryShaderPreamble);
840
Jamie Madill334d6152015-10-22 14:00:28 -0400841 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700842
843 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400844 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
845 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700846 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400847 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400848 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700849
Jamie Madilld3dfda22015-07-06 08:28:49 -0400850 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700851 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400852 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700853 }
854
Jamie Madill334d6152015-10-22 14:00:28 -0400855 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700856 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400857
Jamie Madillada9ecc2015-08-17 12:53:37 -0400858 ShaderExecutableD3D *shaderExecutable = nullptr;
859
860 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500861 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400862 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400863 if (error.isError())
864 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500865 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400866 }
867
Brandon Joneseb994362014-09-24 10:27:28 -0700868 if (!shaderExecutable)
869 {
Jamie Madillf6113162015-05-07 11:49:21 -0400870 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500871 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700872 }
873
874 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400875 VertexExecutable::Signature signature;
876 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700877
878 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400879 mVertexExecutables.push_back(
880 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700881
882 stream->skip(vertexShaderSize);
883 }
884
885 const size_t pixelShaderCount = stream->readInt<unsigned int>();
886 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
887 {
888 const size_t outputCount = stream->readInt<unsigned int>();
889 std::vector<GLenum> outputs(outputCount);
890 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
891 {
892 stream->readInt(&outputs[outputIndex]);
893 }
894
Jamie Madill334d6152015-10-22 14:00:28 -0400895 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700896 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400897 ShaderExecutableD3D *shaderExecutable = nullptr;
898
899 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500900 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400901 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400902 if (error.isError())
903 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500904 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400905 }
Brandon Joneseb994362014-09-24 10:27:28 -0700906
907 if (!shaderExecutable)
908 {
Jamie Madillf6113162015-05-07 11:49:21 -0400909 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500910 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700911 }
912
913 // add new binary
914 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
915
916 stream->skip(pixelShaderSize);
917 }
918
Jamie Madill4e31ad52015-10-29 10:32:57 -0400919 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
920 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700921 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400922 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
923 if (geometryShaderSize == 0)
924 {
925 mGeometryExecutables[geometryExeIndex] = nullptr;
926 continue;
927 }
928
Brandon Joneseb994362014-09-24 10:27:28 -0700929 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400930 bool splitAttribs = (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
931
Jamie Madill28afae52015-11-09 15:07:57 -0500932 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500933 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, mStreamOutVaryings,
934 splitAttribs, &mGeometryExecutables[geometryExeIndex]);
Geoff Langb543aff2014-09-30 14:52:54 -0400935 if (error.isError())
936 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500937 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400938 }
Brandon Joneseb994362014-09-24 10:27:28 -0700939
Jamie Madill4e31ad52015-10-29 10:32:57 -0400940 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700941 {
Jamie Madillf6113162015-05-07 11:49:21 -0400942 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500943 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700944 }
945 stream->skip(geometryShaderSize);
946 }
947
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700948 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500949 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700950
Geoff Lang7dd2e102014-11-10 15:19:26 -0500951 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700952}
953
Geoff Langb543aff2014-09-30 14:52:54 -0400954gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700955{
Austin Kinross137b1512015-06-17 16:14:53 -0700956 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400957 // When we load the binary again later, we can validate the device identifier before trying to
958 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700959 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400960 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
961 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700962
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500963 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
964
Jamie Madill63805b42015-08-25 13:17:39 -0400965 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
966 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
967 {
968 stream->writeInt(mSemanticIndexes[i]);
969 }
970
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700971 stream->writeInt(mSamplersPS.size());
972 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
973 {
974 stream->writeInt(mSamplersPS[i].active);
975 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
976 stream->writeInt(mSamplersPS[i].textureType);
977 }
978
979 stream->writeInt(mSamplersVS.size());
980 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
981 {
982 stream->writeInt(mSamplersVS[i].active);
983 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
984 stream->writeInt(mSamplersVS[i].textureType);
985 }
986
987 stream->writeInt(mUsedVertexSamplerRange);
988 stream->writeInt(mUsedPixelSamplerRange);
989
Jamie Madill62d31cb2015-09-11 13:25:51 -0400990 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -0400991 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700992 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400993 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madill4a3c2342015-10-08 12:58:45 -0400994 stream->writeInt(uniform->psRegisterIndex);
995 stream->writeInt(uniform->vsRegisterIndex);
996 stream->writeInt(uniform->registerCount);
997 stream->writeInt(uniform->registerElement);
998 }
999
1000 stream->writeInt(mD3DUniformBlocks.size());
1001 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1002 {
1003 stream->writeInt(uniformBlock.psRegisterIndex);
1004 stream->writeInt(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001005 }
1006
Jamie Madill9fc36822015-11-18 13:08:07 -05001007 stream->writeInt(mStreamOutVaryings.size());
1008 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001009 {
Brandon Joneseb994362014-09-24 10:27:28 -07001010 stream->writeString(varying.semanticName);
1011 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001012 stream->writeInt(varying.componentCount);
1013 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001014 }
1015
Brandon Jones22502d52014-08-29 16:58:36 -07001016 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001017 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1018 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001019 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001020 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1021 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001022 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001023 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001024 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001025
Brandon Joneseb994362014-09-24 10:27:28 -07001026 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001027 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001028 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1029 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001030 {
Brandon Joneseb994362014-09-24 10:27:28 -07001031 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001032 stream->writeInt(variable.type);
1033 stream->writeString(variable.name);
1034 stream->writeString(variable.source);
1035 stream->writeInt(variable.outputIndex);
1036 }
1037
Jamie Madill4e31ad52015-10-29 10:32:57 -04001038 stream->writeString(mGeometryShaderPreamble);
1039
Brandon Joneseb994362014-09-24 10:27:28 -07001040 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001041 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1042 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001043 {
1044 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1045
Jamie Madilld3dfda22015-07-06 08:28:49 -04001046 const auto &inputLayout = vertexExecutable->inputs();
1047 stream->writeInt(inputLayout.size());
1048
1049 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001050 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001051 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -07001052 }
1053
1054 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1055 stream->writeInt(vertexShaderSize);
1056
1057 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1058 stream->writeBytes(vertexBlob, vertexShaderSize);
1059 }
1060
1061 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001062 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1063 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001064 {
1065 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1066
1067 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1068 stream->writeInt(outputs.size());
1069 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1070 {
1071 stream->writeInt(outputs[outputIndex]);
1072 }
1073
1074 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1075 stream->writeInt(pixelShaderSize);
1076
1077 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1078 stream->writeBytes(pixelBlob, pixelShaderSize);
1079 }
1080
Jamie Madill4e31ad52015-10-29 10:32:57 -04001081 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001082 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001083 if (geometryExe == nullptr)
1084 {
1085 stream->writeInt(0);
1086 continue;
1087 }
1088
1089 size_t geometryShaderSize = geometryExe->getLength();
1090 stream->writeInt(geometryShaderSize);
1091 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001092 }
1093
Geoff Langb543aff2014-09-30 14:52:54 -04001094 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001095}
1096
Jamie Madill334d6152015-10-22 14:00:28 -04001097gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1098 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001099{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001100 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001101
Jamie Madill85a18042015-03-05 15:41:41 -05001102 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill334d6152015-10-22 14:00:28 -04001103 const gl::AttachmentList &colorbuffers =
1104 fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -07001105
1106 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1107 {
1108 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1109
1110 if (colorbuffer)
1111 {
Jamie Madill334d6152015-10-22 14:00:28 -04001112 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1113 ? GL_COLOR_ATTACHMENT0
1114 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001115 }
1116 else
1117 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001118 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001119 }
1120 }
1121
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001122 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001123}
1124
Jamie Madill97399232014-12-23 12:31:15 -05001125gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001126 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001127 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001128{
1129 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1130 {
1131 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1132 {
Geoff Langb543aff2014-09-30 14:52:54 -04001133 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1134 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001135 }
1136 }
1137
Jamie Madill334d6152015-10-22 14:00:28 -04001138 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1139 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001140
1141 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001142 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001143
1144 gl::InfoLog tempInfoLog;
1145 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1146
Jamie Madillada9ecc2015-08-17 12:53:37 -04001147 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001148 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001149 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
1150 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001151 if (error.isError())
1152 {
1153 return error;
1154 }
Brandon Joneseb994362014-09-24 10:27:28 -07001155
Jamie Madill97399232014-12-23 12:31:15 -05001156 if (pixelExecutable)
1157 {
1158 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1159 }
1160 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001161 {
1162 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001163 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001164 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1165 }
Brandon Jones22502d52014-08-29 16:58:36 -07001166
Geoff Langb543aff2014-09-30 14:52:54 -04001167 *outExectuable = pixelExecutable;
1168 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001169}
1170
Jamie Madilld3dfda22015-07-06 08:28:49 -04001171gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001172 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001173 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001174{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001175 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001176
1177 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1178 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001179 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001180 {
Geoff Langb543aff2014-09-30 14:52:54 -04001181 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1182 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001183 }
1184 }
1185
Brandon Jones22502d52014-08-29 16:58:36 -07001186 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001187 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1188 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001189
1190 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001191 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001192
1193 gl::InfoLog tempInfoLog;
1194 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1195
Jamie Madillada9ecc2015-08-17 12:53:37 -04001196 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001197 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001198 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1199 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001200 if (error.isError())
1201 {
1202 return error;
1203 }
1204
Jamie Madill97399232014-12-23 12:31:15 -05001205 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001206 {
Jamie Madill334d6152015-10-22 14:00:28 -04001207 mVertexExecutables.push_back(
1208 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001209 }
Jamie Madill97399232014-12-23 12:31:15 -05001210 else if (!infoLog)
1211 {
1212 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001213 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001214 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1215 }
Brandon Jones22502d52014-08-29 16:58:36 -07001216
Geoff Langb543aff2014-09-30 14:52:54 -04001217 *outExectuable = vertexExecutable;
1218 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001219}
1220
Jamie Madill4e31ad52015-10-29 10:32:57 -04001221gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Data &data,
1222 GLenum drawMode,
1223 ShaderExecutableD3D **outExecutable,
1224 gl::InfoLog *infoLog)
1225{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001226 if (outExecutable)
1227 {
1228 *outExecutable = nullptr;
1229 }
1230
1231 // We only uses a geometry shader for point sprite emulation, or for fixing the provoking
1232 // vertex problem. Otherwise, return a null shader.
1233 if (drawMode != GL_POINTS && !mUsesFlatInterpolation)
1234 {
1235 return gl::Error(GL_NO_ERROR);
1236 }
1237
Jamie Madill4e31ad52015-10-29 10:32:57 -04001238 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1239
1240 if (mGeometryExecutables[geometryShaderType] != nullptr)
1241 {
1242 if (outExecutable)
1243 {
1244 *outExecutable = mGeometryExecutables[geometryShaderType];
1245 }
1246 return gl::Error(GL_NO_ERROR);
1247 }
1248
1249 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
1250 geometryShaderType, data, mData, mGeometryShaderPreamble);
1251
1252 gl::InfoLog tempInfoLog;
1253 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1254
1255 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001256 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001257 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
1258 &mGeometryExecutables[geometryShaderType]);
1259
1260 if (!infoLog && error.isError())
1261 {
1262 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1263 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1264 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1265 }
1266
1267 if (outExecutable)
1268 {
1269 *outExecutable = mGeometryExecutables[geometryShaderType];
1270 }
1271 return error;
1272}
1273
1274LinkResult ProgramD3D::compileProgramExecutables(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001275{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001276 const gl::InputLayout &defaultInputLayout =
1277 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001278 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001279 gl::Error error =
1280 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001281 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001282 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001283 return LinkResult(false, error);
1284 }
Austin Kinross434953e2015-02-20 10:49:51 -08001285
Jamie Madill334d6152015-10-22 14:00:28 -04001286 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001287 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001288 error =
1289 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001290 if (error.isError())
1291 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001293 }
Brandon Jones44151a92014-09-10 11:32:25 -07001294
Jamie Madill4e31ad52015-10-29 10:32:57 -04001295 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill847638a2015-11-20 13:01:41 -05001296 ShaderExecutableD3D *pointGS = nullptr;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001297 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001298 {
Jamie Madill847638a2015-11-20 13:01:41 -05001299 getGeometryExecutableForPrimitiveType(data, GL_POINTS, &pointGS, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001300 }
1301
Jamie Madillca03b352015-09-02 12:38:13 -04001302 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001303
1304 if (usesGeometryShader(GL_POINTS) && pointGS)
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 Madill847638a2015-11-20 13:01:41 -05001310 vertexShaderD3D->appendDebugInfo(pointGS->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 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001325
Jamie Madill847638a2015-11-20 13:01:41 -05001326 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable &&
1327 (!usesGeometryShader(GL_POINTS) || pointGS));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001328 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001329}
1330
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001331LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001332{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001333 reset();
1334
1335 // TODO(jmadill): structures containing samplers
1336 for (const gl::LinkedUniform &linkedUniform : mData.getUniforms())
1337 {
1338 if (linkedUniform.isSampler() && linkedUniform.isField())
1339 {
1340 infoLog << "Structures containing samplers not currently supported in D3D.";
1341 return LinkResult(false, gl::Error(GL_NO_ERROR));
1342 }
1343 }
1344
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001345 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1346 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001347
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001348 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1349 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1350
Jamie Madill63069df2015-09-01 17:26:41 +00001351 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001352 mSamplersPS.resize(data.caps->maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001353
Arun Patole44efa0b2015-03-04 17:11:05 +05301354 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001355 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1356
Austin Kinross02df7962015-07-01 10:03:42 -07001357 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1358 {
1359 if (fragmentShaderD3D->usesFrontFacing())
1360 {
1361 infoLog << "The current renderer doesn't support gl_FrontFacing";
1362 return LinkResult(false, gl::Error(GL_NO_ERROR));
1363 }
1364 }
1365
Jamie Madillca03b352015-09-02 12:38:13 -04001366 std::vector<PackedVarying> packedVaryings =
1367 MergeVaryings(*vertexShader, *fragmentShader, mData.getTransformFeedbackVaryingNames());
1368
Brandon Jones22502d52014-08-29 16:58:36 -07001369 // Map the varyings to the register file
Jamie Madill9fc36822015-11-18 13:08:07 -05001370 VaryingPacking varyingPacking(data.caps->maxVaryingVectors);
1371 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
1372 mData.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001373 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001374 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001375 }
1376
Jamie Madille39a3f02015-11-17 20:42:15 -05001377 ProgramD3DMetadata metadata(mRenderer->getMajorShaderModel(), mRenderer->getShaderModelSuffix(),
1378 usesInstancedPointSpriteEmulation(), vertexShaderD3D,
1379 fragmentShaderD3D);
1380
Jamie Madill9fc36822015-11-18 13:08:07 -05001381 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1382 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1383
1384 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.caps->maxVaryingVectors)
1385 {
1386 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1387 return LinkResult(false, gl::Error(GL_NO_ERROR));
1388 }
1389
1390 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1391 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1392 // intelligently, but D3D9 assumes one semantic per register.
1393 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1394 varyingPacking.getMaxSemanticIndex() > data.caps->maxVaryingVectors)
1395 {
1396 infoLog << "Cannot pack these varyings on D3D9.";
1397 return LinkResult(false, gl::Error(GL_NO_ERROR));
1398 }
1399
1400 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mData, metadata, varyingPacking, &mPixelHLSL,
1401 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001402 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001403 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001404 }
1405
Brandon Jones44151a92014-09-10 11:32:25 -07001406 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madille39a3f02015-11-17 20:42:15 -05001407 mDynamicHLSL->getPixelShaderOutputKey(data, mData, metadata, &mPixelShaderKey);
1408 mUsesFragDepth = metadata.usesFragDepth(mData);
Brandon Jones44151a92014-09-10 11:32:25 -07001409
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001410 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001411 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001412 for (const auto &varying : packedVaryings)
1413 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001414 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001415 {
1416 mUsesFlatInterpolation = true;
1417 break;
1418 }
1419 }
1420
Jamie Madill4e31ad52015-10-29 10:32:57 -04001421 if (mRenderer->getMajorShaderModel() >= 4)
1422 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001423 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1424 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001425 }
1426
Jamie Madill63805b42015-08-25 13:17:39 -04001427 initSemanticIndex();
Jamie Madill437d2662014-12-05 14:23:35 -05001428
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001429 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001430
Jamie Madill9fc36822015-11-18 13:08:07 -05001431 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001432
Jamie Madill4e31ad52015-10-29 10:32:57 -04001433 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001434 if (result.error.isError() || !result.linkSuccess)
1435 {
1436 infoLog << "Failed to create D3D shaders.";
1437 return result;
1438 }
1439
Jamie Madill4a3c2342015-10-08 12:58:45 -04001440 initUniformBlockInfo();
1441
Geoff Lang7dd2e102014-11-10 15:19:26 -05001442 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001443}
1444
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001445GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001446{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001447 // TODO(jmadill): Do something useful here?
1448 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001449}
1450
Jamie Madill4a3c2342015-10-08 12:58:45 -04001451void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001452{
1453 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1454
Jamie Madill62d31cb2015-09-11 13:25:51 -04001455 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1456 {
1457 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1458 continue;
1459
Jamie Madill4a3c2342015-10-08 12:58:45 -04001460 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001461 continue;
1462
Jamie Madill4a3c2342015-10-08 12:58:45 -04001463 size_t dataSize = getUniformBlockInfo(vertexBlock);
1464 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001465 }
1466
1467 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1468
1469 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1470 {
1471 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1472 continue;
1473
Jamie Madill4a3c2342015-10-08 12:58:45 -04001474 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001475 continue;
1476
Jamie Madill4a3c2342015-10-08 12:58:45 -04001477 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1478 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001479 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001480}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001481
Jamie Madill4a3c2342015-10-08 12:58:45 -04001482void ProgramD3D::assignUniformBlockRegisters()
1483{
1484 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001485
1486 // Assign registers and update sizes.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001487 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1488 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001489
Jamie Madill4a3c2342015-10-08 12:58:45 -04001490 for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001491 {
1492 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1493
Jamie Madill4a3c2342015-10-08 12:58:45 -04001494 D3DUniformBlock d3dUniformBlock;
1495
Jamie Madill62d31cb2015-09-11 13:25:51 -04001496 if (uniformBlock.vertexStaticUse)
1497 {
1498 unsigned int baseRegister =
1499 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001500 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001501 }
1502
1503 if (uniformBlock.fragmentStaticUse)
1504 {
1505 unsigned int baseRegister =
1506 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001507 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001508 }
1509
Jamie Madill4a3c2342015-10-08 12:58:45 -04001510 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001511 }
1512}
1513
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001514void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001515{
1516 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001517 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001518 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001519 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001520 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001521 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001522 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001523 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001524 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001525 vertexRegisters = std::max(vertexRegisters,
1526 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001527 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001528 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001529 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001530 fragmentRegisters = std::max(
1531 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001532 }
1533 }
1534 }
1535
Jamie Madill334d6152015-10-22 14:00:28 -04001536 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001537 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1538}
1539
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001540gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001541{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001542 updateSamplerMapping();
1543
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001544 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001545 if (error.isError())
1546 {
1547 return error;
1548 }
1549
Jamie Madill62d31cb2015-09-11 13:25:51 -04001550 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001551 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001552 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001553 }
1554
1555 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001556}
1557
Jamie Madilld1fe1642015-08-21 16:26:04 -04001558gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001559{
Jamie Madill4a3c2342015-10-08 12:58:45 -04001560 if (mData.getUniformBlocks().empty())
1561 {
1562 return gl::Error(GL_NO_ERROR);
1563 }
1564
1565 // Lazy init.
1566 if (mD3DUniformBlocks.empty())
1567 {
1568 assignUniformBlockRegisters();
1569 }
1570
Jamie Madill03260fa2015-06-22 13:57:22 -04001571 mVertexUBOCache.clear();
1572 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001573
1574 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1575 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1576
Jamie Madill4a3c2342015-10-08 12:58:45 -04001577 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001578 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001579 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001580 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
1581 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001582
Brandon Jones18bd4102014-09-22 14:21:44 -07001583 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001584 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001585 {
1586 continue;
1587 }
1588
Jamie Madill4a3c2342015-10-08 12:58:45 -04001589 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001590 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001591 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001592 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001593
Jamie Madill969194d2015-07-20 14:36:56 -04001594 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001595 {
1596 mVertexUBOCache.resize(registerIndex + 1, -1);
1597 }
1598
1599 ASSERT(mVertexUBOCache[registerIndex] == -1);
1600 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001601 }
1602
Jamie Madill4a3c2342015-10-08 12:58:45 -04001603 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001604 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001605 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001606 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001607
1608 if (mFragmentUBOCache.size() <= registerIndex)
1609 {
1610 mFragmentUBOCache.resize(registerIndex + 1, -1);
1611 }
1612
1613 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1614 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001615 }
1616 }
1617
Jamie Madill03260fa2015-06-22 13:57:22 -04001618 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001619}
1620
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001621void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001622{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001623 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001624 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001625 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001626 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001627}
1628
Jamie Madill334d6152015-10-22 14:00:28 -04001629void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001630{
1631 setUniform(location, count, v, GL_FLOAT);
1632}
1633
1634void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1635{
1636 setUniform(location, count, v, GL_FLOAT_VEC2);
1637}
1638
1639void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1640{
1641 setUniform(location, count, v, GL_FLOAT_VEC3);
1642}
1643
1644void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1645{
1646 setUniform(location, count, v, GL_FLOAT_VEC4);
1647}
1648
Jamie Madill334d6152015-10-22 14:00:28 -04001649void ProgramD3D::setUniformMatrix2fv(GLint location,
1650 GLsizei count,
1651 GLboolean transpose,
1652 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001653{
1654 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1655}
1656
Jamie Madill334d6152015-10-22 14:00:28 -04001657void ProgramD3D::setUniformMatrix3fv(GLint location,
1658 GLsizei count,
1659 GLboolean transpose,
1660 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001661{
1662 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1663}
1664
Jamie Madill334d6152015-10-22 14:00:28 -04001665void ProgramD3D::setUniformMatrix4fv(GLint location,
1666 GLsizei count,
1667 GLboolean transpose,
1668 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001669{
1670 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1671}
1672
Jamie Madill334d6152015-10-22 14:00:28 -04001673void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1674 GLsizei count,
1675 GLboolean transpose,
1676 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001677{
1678 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1679}
1680
Jamie Madill334d6152015-10-22 14:00:28 -04001681void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1682 GLsizei count,
1683 GLboolean transpose,
1684 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001685{
1686 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1687}
1688
Jamie Madill334d6152015-10-22 14:00:28 -04001689void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1690 GLsizei count,
1691 GLboolean transpose,
1692 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001693{
1694 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1695}
1696
Jamie Madill334d6152015-10-22 14:00:28 -04001697void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1698 GLsizei count,
1699 GLboolean transpose,
1700 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001701{
1702 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1703}
1704
Jamie Madill334d6152015-10-22 14:00:28 -04001705void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1706 GLsizei count,
1707 GLboolean transpose,
1708 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001709{
1710 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1711}
1712
Jamie Madill334d6152015-10-22 14:00:28 -04001713void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1714 GLsizei count,
1715 GLboolean transpose,
1716 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001717{
1718 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1719}
1720
1721void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1722{
1723 setUniform(location, count, v, GL_INT);
1724}
1725
1726void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1727{
1728 setUniform(location, count, v, GL_INT_VEC2);
1729}
1730
1731void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1732{
1733 setUniform(location, count, v, GL_INT_VEC3);
1734}
1735
1736void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1737{
1738 setUniform(location, count, v, GL_INT_VEC4);
1739}
1740
1741void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1742{
1743 setUniform(location, count, v, GL_UNSIGNED_INT);
1744}
1745
1746void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1747{
1748 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1749}
1750
1751void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1752{
1753 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1754}
1755
1756void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1757{
1758 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1759}
1760
Jamie Madill4a3c2342015-10-08 12:58:45 -04001761void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1762 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001763{
1764}
1765
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001766void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001767{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001768 D3DUniformMap uniformMap;
Jamie Madill334d6152015-10-22 14:00:28 -04001769 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001770 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001771
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001772 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001773 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001774 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001775 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001776 }
1777 }
1778
Jamie Madill334d6152015-10-22 14:00:28 -04001779 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001780 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001781 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001782 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001783 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001784 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001785 }
1786 }
1787
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001788 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
1789 for (const gl::LinkedUniform &glUniform : mData.getUniforms())
1790 {
1791 if (!glUniform.isInDefaultBlock())
1792 continue;
1793
1794 auto mapEntry = uniformMap.find(glUniform.name);
1795 ASSERT(mapEntry != uniformMap.end());
1796 mD3DUniforms.push_back(mapEntry->second);
1797 }
1798
Jamie Madill62d31cb2015-09-11 13:25:51 -04001799 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001800 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001801}
1802
Jamie Madill91445bc2015-09-23 16:47:53 -04001803void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001804 const sh::Uniform &uniform,
1805 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001806{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001807 if (uniform.isBuiltIn())
Jamie Madillfb536032015-09-11 13:19:49 -04001808 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001809 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001810 return;
1811 }
1812
Jamie Madill91445bc2015-09-23 16:47:53 -04001813 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1814
1815 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1816 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001817 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001818 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001819
Jamie Madill91445bc2015-09-23 16:47:53 -04001820 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001821}
1822
Jamie Madill62d31cb2015-09-11 13:25:51 -04001823D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1824{
1825 for (D3DUniform *d3dUniform : mD3DUniforms)
1826 {
1827 if (d3dUniform->name == name)
1828 {
1829 return d3dUniform;
1830 }
1831 }
1832
1833 return nullptr;
1834}
1835
Jamie Madill91445bc2015-09-23 16:47:53 -04001836void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001837 const sh::ShaderVariable &uniform,
1838 const std::string &fullName,
1839 sh::HLSLBlockEncoder *encoder,
1840 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001841{
1842 if (uniform.isStruct())
1843 {
1844 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1845 {
1846 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1847
Jamie Madill55def582015-05-04 11:24:57 -04001848 if (encoder)
1849 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001850
1851 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1852 {
Jamie Madill334d6152015-10-22 14:00:28 -04001853 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001854 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1855
Jamie Madill91445bc2015-09-23 16:47:53 -04001856 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001857 }
1858
Jamie Madill55def582015-05-04 11:24:57 -04001859 if (encoder)
1860 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001861 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001862 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001863 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001864
1865 // Not a struct. Arrays are treated as aggregate types.
1866 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001867 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001868 encoder->enterAggregateType();
1869 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001870
Jamie Madill62d31cb2015-09-11 13:25:51 -04001871 // Advance the uniform offset, to track registers allocation for structs
1872 sh::BlockMemberInfo blockInfo =
1873 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1874 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001875
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001876 auto uniformMapEntry = uniformMap->find(fullName);
1877 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001878
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001879 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001880 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001881 d3dUniform = uniformMapEntry->second;
1882 }
1883 else
1884 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001885 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001886 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001887 }
1888
1889 if (encoder)
1890 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001891 d3dUniform->registerElement =
1892 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001893 unsigned int reg =
1894 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001895 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001896 {
1897 d3dUniform->psRegisterIndex = reg;
1898 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001899 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001900 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001901 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001902 d3dUniform->vsRegisterIndex = reg;
1903 }
Jamie Madillfb536032015-09-11 13:19:49 -04001904
1905 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001906 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001907 {
1908 encoder->exitAggregateType();
1909 }
1910 }
1911}
1912
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001913template <typename T>
Jamie Madill334d6152015-10-22 14:00:28 -04001914static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001915{
1916 ASSERT(dest != NULL);
1917 ASSERT(dirtyFlag != NULL);
1918
1919 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
Jamie Madill334d6152015-10-22 14:00:28 -04001920 *dest = source;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001921}
1922
1923template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001924void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001925{
Jamie Madill334d6152015-10-22 14:00:28 -04001926 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001927 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1928
Jamie Madill62d31cb2015-09-11 13:25:51 -04001929 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001930
Jamie Madill62d31cb2015-09-11 13:25:51 -04001931 unsigned int elementCount = targetUniform->elementCount();
1932 unsigned int arrayElement = mData.getUniformLocations()[location].element;
1933 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001934
1935 if (targetUniform->type == targetUniformType)
1936 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001937 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001938
Jamie Madill62d31cb2015-09-11 13:25:51 -04001939 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001940 {
Jamie Madill334d6152015-10-22 14:00:28 -04001941 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001942 const T *source = v + (i * components);
1943
1944 for (int c = 0; c < components; c++)
1945 {
1946 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1947 }
1948 for (int c = components; c < 4; c++)
1949 {
1950 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1951 }
1952 }
1953 }
1954 else if (targetUniform->type == targetBoolType)
1955 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001956 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001957
Jamie Madill62d31cb2015-09-11 13:25:51 -04001958 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001959 {
Jamie Madill334d6152015-10-22 14:00:28 -04001960 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001961 const T *source = v + (i * components);
1962
1963 for (int c = 0; c < components; c++)
1964 {
Jamie Madill334d6152015-10-22 14:00:28 -04001965 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1966 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001967 }
1968 for (int c = components; c < 4; c++)
1969 {
1970 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1971 }
1972 }
1973 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001974 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001975 {
1976 ASSERT(targetUniformType == GL_INT);
1977
Jamie Madill62d31cb2015-09-11 13:25:51 -04001978 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001979
1980 bool wasDirty = targetUniform->dirty;
1981
Jamie Madill62d31cb2015-09-11 13:25:51 -04001982 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001983 {
Jamie Madill334d6152015-10-22 14:00:28 -04001984 GLint *dest = target + (i * 4);
1985 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986
1987 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1988 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1989 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1990 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1991 }
1992
1993 if (!wasDirty && targetUniform->dirty)
1994 {
1995 mDirtySamplerMapping = true;
1996 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001997 }
Jamie Madill334d6152015-10-22 14:00:28 -04001998 else
1999 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002000}
2001
2002template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002003void ProgramD3D::setUniformMatrixfv(GLint location,
2004 GLsizei countIn,
2005 GLboolean transpose,
2006 const GLfloat *value,
2007 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002008{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002009 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002010
Jamie Madill62d31cb2015-09-11 13:25:51 -04002011 unsigned int elementCount = targetUniform->elementCount();
2012 unsigned int arrayElement = mData.getUniformLocations()[location].element;
2013 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002014
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002015 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002016 GLfloat *target =
2017 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018
Jamie Madill62d31cb2015-09-11 13:25:51 -04002019 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002020 {
2021 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2022 if (transpose == GL_FALSE)
2023 {
Jamie Madill334d6152015-10-22 14:00:28 -04002024 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2025 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002026 }
2027 else
2028 {
Jamie Madill334d6152015-10-22 14:00:28 -04002029 targetUniform->dirty =
2030 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002031 }
2032 target += targetMatrixStride;
2033 value += cols * rows;
2034 }
2035}
2036
Jamie Madill4a3c2342015-10-08 12:58:45 -04002037size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002038{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002039 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002040
Jamie Madill62d31cb2015-09-11 13:25:51 -04002041 // define member uniforms
2042 sh::Std140BlockEncoder std140Encoder;
2043 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2044 sh::BlockLayoutEncoder *encoder = nullptr;
2045
2046 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002047 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002048 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002049 }
2050 else
2051 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002052 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002053 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002054
2055 GetUniformBlockInfo(interfaceBlock.fields, "", encoder, interfaceBlock.isRowMajorLayout,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002056 &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002057
2058 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002059}
2060
Jamie Madill62d31cb2015-09-11 13:25:51 -04002061void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002062{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002063 for (const D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002064 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002065 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002066 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002067 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002068 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002069 }
2070}
2071
Jamie Madill62d31cb2015-09-11 13:25:51 -04002072void ProgramD3D::assignSamplerRegisters(const D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002073{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002074 ASSERT(d3dUniform->isSampler());
2075 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX ||
2076 d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madillfb536032015-09-11 13:19:49 -04002077
Jamie Madill62d31cb2015-09-11 13:25:51 -04002078 if (d3dUniform->vsRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002079 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002080 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2081 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002082 }
2083
Jamie Madill62d31cb2015-09-11 13:25:51 -04002084 if (d3dUniform->psRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002085 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002086 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2087 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002088 }
2089}
2090
Jamie Madill62d31cb2015-09-11 13:25:51 -04002091// static
2092void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002093 GLenum samplerType,
2094 unsigned int samplerCount,
2095 std::vector<Sampler> &outSamplers,
2096 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002097{
2098 unsigned int samplerIndex = startSamplerIndex;
2099
2100 do
2101 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002102 ASSERT(samplerIndex < outSamplers.size());
2103 Sampler *sampler = &outSamplers[samplerIndex];
2104 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002105 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002106 sampler->logicalTextureUnit = 0;
2107 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002108 samplerIndex++;
2109 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002110}
2111
Brandon Jonesc9610c52014-08-25 17:02:59 -07002112void ProgramD3D::reset()
2113{
Brandon Joneseb994362014-09-24 10:27:28 -07002114 SafeDeleteContainer(mVertexExecutables);
2115 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002116
2117 for (auto &element : mGeometryExecutables)
2118 {
2119 SafeDelete(element);
2120 }
Brandon Joneseb994362014-09-24 10:27:28 -07002121
Brandon Jones22502d52014-08-29 16:58:36 -07002122 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002123 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002124
2125 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002126 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002127 mUsesFragDepth = false;
2128 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002129 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002130 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002131
Jamie Madill62d31cb2015-09-11 13:25:51 -04002132 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002133 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002134
Brandon Jonesc9610c52014-08-25 17:02:59 -07002135 SafeDelete(mVertexUniformStorage);
2136 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002137
2138 mSamplersPS.clear();
2139 mSamplersVS.clear();
2140
2141 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002142 mUsedPixelSamplerRange = 0;
2143 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002144
Jamie Madill63805b42015-08-25 13:17:39 -04002145 std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1);
Jamie Madill437d2662014-12-05 14:23:35 -05002146 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002147
Jamie Madill9fc36822015-11-18 13:08:07 -05002148 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002149
2150 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002151}
2152
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153unsigned int ProgramD3D::getSerial() const
2154{
2155 return mSerial;
2156}
2157
2158unsigned int ProgramD3D::issueSerial()
2159{
2160 return mCurrentSerial++;
2161}
2162
Jamie Madill63805b42015-08-25 13:17:39 -04002163void ProgramD3D::initSemanticIndex()
2164{
2165 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2166 ASSERT(vertexShader != nullptr);
2167
2168 // Init semantic index
2169 for (const sh::Attribute &attribute : mData.getAttributes())
2170 {
2171 int attributeIndex = attribute.location;
2172 int index = vertexShader->getSemanticIndex(attribute.name);
2173 int regs = gl::VariableRegisterCount(attribute.type);
2174
2175 for (int reg = 0; reg < regs; ++reg)
2176 {
2177 mSemanticIndexes[attributeIndex + reg] = index + reg;
2178 }
2179 }
2180
2181 initAttributesByLayout();
2182}
2183
Jamie Madill437d2662014-12-05 14:23:35 -05002184void ProgramD3D::initAttributesByLayout()
2185{
2186 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2187 {
2188 mAttributesByLayout[i] = i;
2189 }
2190
Jamie Madill63805b42015-08-25 13:17:39 -04002191 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS],
2192 AttributeSorter(mSemanticIndexes));
Jamie Madill437d2662014-12-05 14:23:35 -05002193}
2194
Jamie Madill334d6152015-10-22 14:00:28 -04002195void ProgramD3D::sortAttributesByLayout(
2196 const std::vector<TranslatedAttribute> &unsortedAttributes,
2197 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2198 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002199{
Jamie Madill476682e2015-06-30 10:04:29 -04002200 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002201 {
Jamie Madill334d6152015-10-22 14:00:28 -04002202 int oldIndex = mAttributesByLayout[attribIndex];
Jamie Madill63805b42015-08-25 13:17:39 -04002203 sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex];
Jamie Madill334d6152015-10-22 14:00:28 -04002204 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002205 }
2206}
2207
Jamie Madill63805b42015-08-25 13:17:39 -04002208void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002209{
Jamie Madillbd136f92015-08-10 14:51:37 -04002210 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002211 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002212
Dian Xianga4928832015-09-15 10:11:17 -07002213 for (unsigned int attributeIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002214 {
Jamie Madill63805b42015-08-25 13:17:39 -04002215 int semanticIndex = mSemanticIndexes[attributeIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002216
2217 if (semanticIndex != -1)
2218 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002219 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2220 {
2221 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2222 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002223 mCachedInputLayout[semanticIndex] =
2224 GetVertexFormatType(vertexAttributes[attributeIndex],
2225 state.getVertexAttribCurrentValue(attributeIndex).Type);
2226 }
2227 }
2228}
2229
Jamie Madill9fc36822015-11-18 13:08:07 -05002230void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002231{
Jamie Madill9fc36822015-11-18 13:08:07 -05002232 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2233
2234 const std::string &varyingSemantic =
2235 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2236
Jamie Madillccdf74b2015-08-18 10:46:12 -04002237 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002238 mStreamOutVaryings.clear();
2239
2240 const auto &tfVaryingNames = mData.getTransformFeedbackVaryingNames();
2241 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2242 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002243 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002244 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2245 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002246 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002247 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002248 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002249 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2250 builtins.glPosition.index, 4, outputSlot));
2251 }
2252 }
2253 else if (tfVaryingName == "gl_FragCoord")
2254 {
2255 if (builtins.glFragCoord.enabled)
2256 {
2257 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2258 builtins.glFragCoord.index, 4, outputSlot));
2259 }
2260 }
2261 else if (tfVaryingName == "gl_PointSize")
2262 {
2263 if (builtins.glPointSize.enabled)
2264 {
2265 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2266 }
2267 }
2268 else
2269 {
2270 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2271 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002272 const auto &varying = *registerInfo.packedVarying->varying;
2273 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002274 int componentCount = gl::VariableColumnCount(transposedType);
2275 ASSERT(!varying.isBuiltIn());
2276
Jamie Madill55c25d02015-11-18 13:08:08 -05002277 // Transform feedback for varying structs is underspecified.
2278 // See Khronos bug 9856.
2279 // TODO(jmadill): Figure out how to be spec-compliant here.
2280 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2281 continue;
2282
Jamie Madill9fc36822015-11-18 13:08:07 -05002283 // There can be more than one register assigned to a particular varying, and each
2284 // register needs its own stream out entry.
2285 if (tfVaryingName == varying.name)
2286 {
2287 mStreamOutVaryings.push_back(D3DVarying(
2288 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2289 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002290 }
2291 }
2292 }
2293}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002294
2295D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2296{
2297 return mD3DUniforms[mData.getUniformLocations()[location].index];
2298}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002299
2300bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2301{
2302 std::string baseName = blockName;
2303 gl::ParseAndStripArrayIndex(&baseName);
2304
2305 auto sizeIter = mBlockDataSizes.find(baseName);
2306 if (sizeIter == mBlockDataSizes.end())
2307 {
2308 *sizeOut = 0;
2309 return false;
2310 }
2311
2312 *sizeOut = sizeIter->second;
2313 return true;
2314}
2315
2316bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2317 sh::BlockMemberInfo *memberInfoOut) const
2318{
2319 auto infoIter = mBlockInfo.find(memberUniformName);
2320 if (infoIter == mBlockInfo.end())
2321 {
2322 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2323 return false;
2324 }
2325
2326 *memberInfoOut = infoIter->second;
2327 return true;
2328}
Brandon Jonesc9610c52014-08-25 17:02:59 -07002329}