blob: b5120465cccaee1a7769bd28ee09ba5221ec52f2 [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>
Jacek Caban2302e692015-12-01 12:44:43 +0100212static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
213{
214 ASSERT(dest != NULL);
215 ASSERT(dirtyFlag != NULL);
216
217 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
218 *dest = source;
219}
220
221template <typename T>
Jamie Madill334d6152015-10-22 14:00:28 -0400222bool TransposeMatrix(T *target,
223 const GLfloat *value,
224 int targetWidth,
225 int targetHeight,
226 int srcWidth,
227 int srcHeight)
228{
229 bool dirty = false;
230 int copyWidth = std::min(targetHeight, srcWidth);
231 int copyHeight = std::min(targetWidth, srcHeight);
232
233 for (int x = 0; x < copyWidth; x++)
234 {
235 for (int y = 0; y < copyHeight; y++)
236 {
237 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]),
238 &dirty);
239 }
240 }
241 // clear unfilled right side
242 for (int y = 0; y < copyWidth; y++)
243 {
244 for (int x = copyHeight; x < targetWidth; x++)
245 {
246 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
247 }
248 }
249 // clear unfilled bottom.
250 for (int y = copyWidth; y < targetHeight; y++)
251 {
252 for (int x = 0; x < targetWidth; x++)
253 {
254 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
255 }
256 }
257
258 return dirty;
259}
260
261template <typename T>
262bool ExpandMatrix(T *target,
263 const GLfloat *value,
264 int targetWidth,
265 int targetHeight,
266 int srcWidth,
267 int srcHeight)
268{
269 bool dirty = false;
270 int copyWidth = std::min(targetWidth, srcWidth);
271 int copyHeight = std::min(targetHeight, srcHeight);
272
273 for (int y = 0; y < copyHeight; y++)
274 {
275 for (int x = 0; x < copyWidth; x++)
276 {
277 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]),
278 &dirty);
279 }
280 }
281 // clear unfilled right side
282 for (int y = 0; y < copyHeight; y++)
283 {
284 for (int x = copyWidth; x < targetWidth; x++)
285 {
286 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
287 }
288 }
289 // clear unfilled bottom.
290 for (int y = copyHeight; y < targetHeight; y++)
291 {
292 for (int x = 0; x < targetWidth; x++)
293 {
294 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
295 }
296 }
297
298 return dirty;
299}
300
Jamie Madill4e31ad52015-10-29 10:32:57 -0400301gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
302{
303 switch (drawMode)
304 {
305 // Uses the point sprite geometry shader.
306 case GL_POINTS:
307 return gl::PRIMITIVE_POINTS;
308
309 // All line drawing uses the same geometry shader.
310 case GL_LINES:
311 case GL_LINE_STRIP:
312 case GL_LINE_LOOP:
313 return gl::PRIMITIVE_LINES;
314
315 // The triangle fan primitive is emulated with strips in D3D11.
316 case GL_TRIANGLES:
317 case GL_TRIANGLE_FAN:
318 return gl::PRIMITIVE_TRIANGLES;
319
320 // Special case for triangle strips.
321 case GL_TRIANGLE_STRIP:
322 return gl::PRIMITIVE_TRIANGLE_STRIP;
323
324 default:
325 UNREACHABLE();
326 return gl::PRIMITIVE_TYPE_MAX;
327 }
328}
329
Jamie Madillada9ecc2015-08-17 12:53:37 -0400330} // anonymous namespace
331
Jamie Madill28afae52015-11-09 15:07:57 -0500332// D3DUniform Implementation
333
Jamie Madill62d31cb2015-09-11 13:25:51 -0400334D3DUniform::D3DUniform(GLenum typeIn,
335 const std::string &nameIn,
336 unsigned int arraySizeIn,
337 bool defaultBlock)
338 : type(typeIn),
339 name(nameIn),
340 arraySize(arraySizeIn),
341 data(nullptr),
342 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400343 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400344 psRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400345 registerCount(0),
346 registerElement(0)
347{
348 // We use data storage for default block uniforms to cache values that are sent to D3D during
349 // rendering
350 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
351 if (defaultBlock)
352 {
353 size_t bytes = gl::VariableInternalSize(type) * elementCount();
354 data = new uint8_t[bytes];
355 memset(data, 0, bytes);
356
357 // TODO(jmadill): is this correct with non-square matrices?
358 registerCount = gl::VariableRowCount(type) * elementCount();
359 }
360}
361
362D3DUniform::~D3DUniform()
363{
364 SafeDeleteArray(data);
365}
366
367bool D3DUniform::isSampler() const
368{
369 return gl::IsSamplerType(type);
370}
371
372bool D3DUniform::isReferencedByVertexShader() const
373{
374 return vsRegisterIndex != GL_INVALID_INDEX;
375}
376
377bool D3DUniform::isReferencedByFragmentShader() const
378{
379 return psRegisterIndex != GL_INVALID_INDEX;
380}
381
Jamie Madill28afae52015-11-09 15:07:57 -0500382// D3DVarying Implementation
383
Jamie Madill9fc36822015-11-18 13:08:07 -0500384D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500385{
386}
387
Jamie Madill9fc36822015-11-18 13:08:07 -0500388D3DVarying::D3DVarying(const std::string &semanticNameIn,
389 unsigned int semanticIndexIn,
390 unsigned int componentCountIn,
391 unsigned int outputSlotIn)
392 : semanticName(semanticNameIn),
393 semanticIndex(semanticIndexIn),
394 componentCount(componentCountIn),
395 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500396{
397}
398
Jamie Madille39a3f02015-11-17 20:42:15 -0500399// ProgramD3DMetadata Implementation
400
401ProgramD3DMetadata::ProgramD3DMetadata(int rendererMajorShaderModel,
402 const std::string &shaderModelSuffix,
403 bool usesInstancedPointSpriteEmulation,
404 const ShaderD3D *vertexShader,
405 const ShaderD3D *fragmentShader)
406 : mRendererMajorShaderModel(rendererMajorShaderModel),
407 mShaderModelSuffix(shaderModelSuffix),
408 mUsesInstancedPointSpriteEmulation(usesInstancedPointSpriteEmulation),
409 mVertexShader(vertexShader),
410 mFragmentShader(fragmentShader)
411{
412}
413
414int ProgramD3DMetadata::getRendererMajorShaderModel() const
415{
416 return mRendererMajorShaderModel;
417}
418
419bool ProgramD3DMetadata::usesBroadcast(const gl::Data &data) const
420{
421 return (mFragmentShader->usesFragColor() && data.clientVersion < 3);
422}
423
424bool ProgramD3DMetadata::usesFragDepth(const gl::Program::Data &programData) const
425{
Jamie Madill63286672015-11-24 13:00:08 -0500426 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500427}
428
429bool ProgramD3DMetadata::usesPointCoord() const
430{
431 return mFragmentShader->usesPointCoord();
432}
433
434bool ProgramD3DMetadata::usesFragCoord() const
435{
436 return mFragmentShader->usesFragCoord();
437}
438
439bool ProgramD3DMetadata::usesPointSize() const
440{
441 return mVertexShader->usesPointSize();
442}
443
444bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
445{
446 return !usesPointSize() && usesPointCoord() && mRendererMajorShaderModel >= 4;
447}
448
449bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
450{
451 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader
452 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
453 // GeometryShader PointSprite emulation does not require this additional entry because the
454 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the
455 // PS_INPUT of the generated pixel shader. The Geometry Shader point sprite implementation needs
456 // gl_PointSize to be in VS_OUTPUT and GS_INPUT. Instanced point sprites doesn't need
457 // gl_PointSize in VS_OUTPUT.
458 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
459 usesInsertedPointCoordValue();
460}
461
462bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
463{
464 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
465 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
466 // the vertex shader in this case. This saves us 1 output vector.
467 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
468}
469
470bool ProgramD3DMetadata::usesSystemValuePointSize() const
471{
472 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
473}
474
475bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
476{
477 return mFragmentShader->usesMultipleRenderTargets();
478}
479
480GLint ProgramD3DMetadata::getMajorShaderVersion() const
481{
482 return mVertexShader->getData().getShaderVersion();
483}
484
485const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
486{
487 return mFragmentShader;
488}
489
Jamie Madill28afae52015-11-09 15:07:57 -0500490// ProgramD3D Implementation
491
Jamie Madilld3dfda22015-07-06 08:28:49 -0400492ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
493 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500494 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400495 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700496{
Brandon Joneseb994362014-09-24 10:27:28 -0700497}
498
499ProgramD3D::VertexExecutable::~VertexExecutable()
500{
501 SafeDelete(mShaderExecutable);
502}
503
Jamie Madilld3dfda22015-07-06 08:28:49 -0400504// static
505void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
506 const gl::InputLayout &inputLayout,
507 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700508{
Jamie Madillbd136f92015-08-10 14:51:37 -0400509 signatureOut->resize(inputLayout.size());
Jamie Madilld3dfda22015-07-06 08:28:49 -0400510
511 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700512 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400513 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbd136f92015-08-10 14:51:37 -0400514 bool converted = false;
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400515 if (vertexFormatType != gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700516 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400517 VertexConversionType conversionType =
518 renderer->getVertexConversionType(vertexFormatType);
Jamie Madillbd136f92015-08-10 14:51:37 -0400519 converted = ((conversionType & VERTEX_CONVERT_GPU) != 0);
Brandon Joneseb994362014-09-24 10:27:28 -0700520 }
Jamie Madillbd136f92015-08-10 14:51:37 -0400521
522 (*signatureOut)[index] = converted;
Brandon Joneseb994362014-09-24 10:27:28 -0700523 }
Brandon Joneseb994362014-09-24 10:27:28 -0700524}
525
Jamie Madilld3dfda22015-07-06 08:28:49 -0400526bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
527{
Jamie Madillbd136f92015-08-10 14:51:37 -0400528 size_t limit = std::max(mSignature.size(), signature.size());
529 for (size_t index = 0; index < limit; ++index)
530 {
531 // treat undefined indexes as 'not converted'
532 bool a = index < signature.size() ? signature[index] : false;
533 bool b = index < mSignature.size() ? mSignature[index] : false;
534 if (a != b)
535 return false;
536 }
537
538 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400539}
540
541ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
542 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400543 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700544{
545}
546
547ProgramD3D::PixelExecutable::~PixelExecutable()
548{
549 SafeDelete(mShaderExecutable);
550}
551
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700552ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
553{
554}
555
Geoff Lang7dd2e102014-11-10 15:19:26 -0500556unsigned int ProgramD3D::mCurrentSerial = 1;
557
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400558ProgramD3D::ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer)
559 : ProgramImpl(data),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700560 mRenderer(renderer),
561 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400562 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700563 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400564 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700565 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700566 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700567 mUsedVertexSamplerRange(0),
568 mUsedPixelSamplerRange(0),
569 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500570 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700571{
Brandon Joneseb994362014-09-24 10:27:28 -0700572 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700573}
574
575ProgramD3D::~ProgramD3D()
576{
577 reset();
578 SafeDelete(mDynamicHLSL);
579}
580
Brandon Jones44151a92014-09-10 11:32:25 -0700581bool ProgramD3D::usesPointSpriteEmulation() const
582{
583 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
584}
585
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400586bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700587{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400588 if (drawMode != GL_POINTS)
589 {
590 return mUsesFlatInterpolation;
591 }
592
Cooper Partine6664f02015-01-09 16:22:24 -0800593 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
594}
595
596bool ProgramD3D::usesInstancedPointSpriteEmulation() const
597{
598 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700599}
600
Jamie Madill334d6152015-10-22 14:00:28 -0400601GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
602 unsigned int samplerIndex,
603 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700604{
605 GLint logicalTextureUnit = -1;
606
607 switch (type)
608 {
Jamie Madill334d6152015-10-22 14:00:28 -0400609 case gl::SAMPLER_PIXEL:
610 ASSERT(samplerIndex < caps.maxTextureImageUnits);
611 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
612 {
613 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
614 }
615 break;
616 case gl::SAMPLER_VERTEX:
617 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
618 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
619 {
620 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
621 }
622 break;
623 default:
624 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700625 }
626
Jamie Madill334d6152015-10-22 14:00:28 -0400627 if (logicalTextureUnit >= 0 &&
628 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700629 {
630 return logicalTextureUnit;
631 }
632
633 return -1;
634}
635
636// Returns the texture type for a given Direct3D 9 sampler type and
637// index (0-15 for the pixel shader and 0-3 for the vertex shader).
638GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
639{
640 switch (type)
641 {
Jamie Madill334d6152015-10-22 14:00:28 -0400642 case gl::SAMPLER_PIXEL:
643 ASSERT(samplerIndex < mSamplersPS.size());
644 ASSERT(mSamplersPS[samplerIndex].active);
645 return mSamplersPS[samplerIndex].textureType;
646 case gl::SAMPLER_VERTEX:
647 ASSERT(samplerIndex < mSamplersVS.size());
648 ASSERT(mSamplersVS[samplerIndex].active);
649 return mSamplersVS[samplerIndex].textureType;
650 default:
651 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700652 }
653
654 return GL_TEXTURE_2D;
655}
656
657GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
658{
659 switch (type)
660 {
Jamie Madill334d6152015-10-22 14:00:28 -0400661 case gl::SAMPLER_PIXEL:
662 return mUsedPixelSamplerRange;
663 case gl::SAMPLER_VERTEX:
664 return mUsedVertexSamplerRange;
665 default:
666 UNREACHABLE();
667 return 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700668 }
669}
670
671void ProgramD3D::updateSamplerMapping()
672{
673 if (!mDirtySamplerMapping)
674 {
675 return;
676 }
677
678 mDirtySamplerMapping = false;
679
680 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400681 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700682 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400683 if (!d3dUniform->dirty)
684 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700685
Jamie Madill62d31cb2015-09-11 13:25:51 -0400686 if (!d3dUniform->isSampler())
687 continue;
688
689 int count = d3dUniform->elementCount();
690 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
691
692 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700693 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400694 unsigned int firstIndex = d3dUniform->psRegisterIndex;
695
696 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700697 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400698 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700699
Jamie Madill62d31cb2015-09-11 13:25:51 -0400700 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700701 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400702 ASSERT(mSamplersPS[samplerIndex].active);
703 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700704 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400705 }
706 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700707
Jamie Madill62d31cb2015-09-11 13:25:51 -0400708 if (d3dUniform->isReferencedByVertexShader())
709 {
710 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
711
712 for (int i = 0; i < count; i++)
713 {
714 unsigned int samplerIndex = firstIndex + i;
715
716 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700717 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400718 ASSERT(mSamplersVS[samplerIndex].active);
719 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700720 }
721 }
722 }
723 }
724}
725
Geoff Lang7dd2e102014-11-10 15:19:26 -0500726LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700727{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400728 reset();
729
Jamie Madill334d6152015-10-22 14:00:28 -0400730 DeviceIdentifier binaryDeviceIdentifier = {0};
731 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
732 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700733
734 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
735 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
736 {
737 infoLog << "Invalid program binary, device configuration has changed.";
738 return LinkResult(false, gl::Error(GL_NO_ERROR));
739 }
740
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500741 int compileFlags = stream->readInt<int>();
742 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
743 {
Jamie Madillf6113162015-05-07 11:49:21 -0400744 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500745 return LinkResult(false, gl::Error(GL_NO_ERROR));
746 }
747
Jamie Madill63805b42015-08-25 13:17:39 -0400748 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
749 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
750 {
751 stream->readInt(&mSemanticIndexes[i]);
752 }
753
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700754 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
755 for (unsigned int i = 0; i < psSamplerCount; ++i)
756 {
757 Sampler sampler;
758 stream->readBool(&sampler.active);
759 stream->readInt(&sampler.logicalTextureUnit);
760 stream->readInt(&sampler.textureType);
761 mSamplersPS.push_back(sampler);
762 }
763 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
764 for (unsigned int i = 0; i < vsSamplerCount; ++i)
765 {
766 Sampler sampler;
767 stream->readBool(&sampler.active);
768 stream->readInt(&sampler.logicalTextureUnit);
769 stream->readInt(&sampler.textureType);
770 mSamplersVS.push_back(sampler);
771 }
772
773 stream->readInt(&mUsedVertexSamplerRange);
774 stream->readInt(&mUsedPixelSamplerRange);
775
776 const unsigned int uniformCount = stream->readInt<unsigned int>();
777 if (stream->error())
778 {
Jamie Madillf6113162015-05-07 11:49:21 -0400779 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500780 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700781 }
782
Jamie Madill62d31cb2015-09-11 13:25:51 -0400783 const auto &linkedUniforms = mData.getUniforms();
784 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700785 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
786 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400787 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700788
Jamie Madill62d31cb2015-09-11 13:25:51 -0400789 D3DUniform *d3dUniform =
790 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
791 linkedUniform.isInDefaultBlock());
792 stream->readInt(&d3dUniform->psRegisterIndex);
793 stream->readInt(&d3dUniform->vsRegisterIndex);
794 stream->readInt(&d3dUniform->registerCount);
795 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700796
Jamie Madill62d31cb2015-09-11 13:25:51 -0400797 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700798 }
799
Jamie Madill4a3c2342015-10-08 12:58:45 -0400800 const unsigned int blockCount = stream->readInt<unsigned int>();
801 if (stream->error())
802 {
803 infoLog << "Invalid program binary.";
804 return LinkResult(false, gl::Error(GL_NO_ERROR));
805 }
806
807 ASSERT(mD3DUniformBlocks.empty());
808 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
809 {
810 D3DUniformBlock uniformBlock;
811 stream->readInt(&uniformBlock.psRegisterIndex);
812 stream->readInt(&uniformBlock.vsRegisterIndex);
813 mD3DUniformBlocks.push_back(uniformBlock);
814 }
815
Jamie Madill9fc36822015-11-18 13:08:07 -0500816 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
817 mStreamOutVaryings.resize(streamOutVaryingCount);
818 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700819 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500820 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700821
Jamie Madill28afae52015-11-09 15:07:57 -0500822 stream->readString(&varying->semanticName);
823 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500824 stream->readInt(&varying->componentCount);
825 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700826 }
827
Brandon Jones22502d52014-08-29 16:58:36 -0700828 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400829 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
830 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700831 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400832 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
833 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700834 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700835 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400836 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700837
838 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
839 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400840 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
841 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700842 {
843 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
844 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
845 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
846 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
847 }
848
Jamie Madill4e31ad52015-10-29 10:32:57 -0400849 stream->readString(&mGeometryShaderPreamble);
850
Jamie Madill334d6152015-10-22 14:00:28 -0400851 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700852
853 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400854 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
855 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700856 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400857 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400858 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700859
Jamie Madilld3dfda22015-07-06 08:28:49 -0400860 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700861 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400862 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700863 }
864
Jamie Madill334d6152015-10-22 14:00:28 -0400865 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700866 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400867
Jamie Madillada9ecc2015-08-17 12:53:37 -0400868 ShaderExecutableD3D *shaderExecutable = nullptr;
869
870 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500871 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400872 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400873 if (error.isError())
874 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500875 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400876 }
877
Brandon Joneseb994362014-09-24 10:27:28 -0700878 if (!shaderExecutable)
879 {
Jamie Madillf6113162015-05-07 11:49:21 -0400880 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500881 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700882 }
883
884 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400885 VertexExecutable::Signature signature;
886 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700887
888 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400889 mVertexExecutables.push_back(
890 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700891
892 stream->skip(vertexShaderSize);
893 }
894
895 const size_t pixelShaderCount = stream->readInt<unsigned int>();
896 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
897 {
898 const size_t outputCount = stream->readInt<unsigned int>();
899 std::vector<GLenum> outputs(outputCount);
900 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
901 {
902 stream->readInt(&outputs[outputIndex]);
903 }
904
Jamie Madill334d6152015-10-22 14:00:28 -0400905 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700906 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400907 ShaderExecutableD3D *shaderExecutable = nullptr;
908
909 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500910 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400911 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400912 if (error.isError())
913 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500914 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400915 }
Brandon Joneseb994362014-09-24 10:27:28 -0700916
917 if (!shaderExecutable)
918 {
Jamie Madillf6113162015-05-07 11:49:21 -0400919 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500920 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700921 }
922
923 // add new binary
924 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
925
926 stream->skip(pixelShaderSize);
927 }
928
Jamie Madill4e31ad52015-10-29 10:32:57 -0400929 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
930 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700931 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400932 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
933 if (geometryShaderSize == 0)
934 {
935 mGeometryExecutables[geometryExeIndex] = nullptr;
936 continue;
937 }
938
Brandon Joneseb994362014-09-24 10:27:28 -0700939 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400940 bool splitAttribs = (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
941
Jamie Madill28afae52015-11-09 15:07:57 -0500942 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500943 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, mStreamOutVaryings,
944 splitAttribs, &mGeometryExecutables[geometryExeIndex]);
Geoff Langb543aff2014-09-30 14:52:54 -0400945 if (error.isError())
946 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500947 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400948 }
Brandon Joneseb994362014-09-24 10:27:28 -0700949
Jamie Madill4e31ad52015-10-29 10:32:57 -0400950 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700951 {
Jamie Madillf6113162015-05-07 11:49:21 -0400952 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500953 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700954 }
955 stream->skip(geometryShaderSize);
956 }
957
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700958 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500959 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700960
Geoff Lang7dd2e102014-11-10 15:19:26 -0500961 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700962}
963
Geoff Langb543aff2014-09-30 14:52:54 -0400964gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700965{
Austin Kinross137b1512015-06-17 16:14:53 -0700966 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400967 // When we load the binary again later, we can validate the device identifier before trying to
968 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700969 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400970 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
971 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700972
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500973 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
974
Jamie Madill63805b42015-08-25 13:17:39 -0400975 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
976 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
977 {
978 stream->writeInt(mSemanticIndexes[i]);
979 }
980
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700981 stream->writeInt(mSamplersPS.size());
982 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
983 {
984 stream->writeInt(mSamplersPS[i].active);
985 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
986 stream->writeInt(mSamplersPS[i].textureType);
987 }
988
989 stream->writeInt(mSamplersVS.size());
990 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
991 {
992 stream->writeInt(mSamplersVS[i].active);
993 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
994 stream->writeInt(mSamplersVS[i].textureType);
995 }
996
997 stream->writeInt(mUsedVertexSamplerRange);
998 stream->writeInt(mUsedPixelSamplerRange);
999
Jamie Madill62d31cb2015-09-11 13:25:51 -04001000 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001001 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001002 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001003 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001004 stream->writeInt(uniform->psRegisterIndex);
1005 stream->writeInt(uniform->vsRegisterIndex);
1006 stream->writeInt(uniform->registerCount);
1007 stream->writeInt(uniform->registerElement);
1008 }
1009
1010 stream->writeInt(mD3DUniformBlocks.size());
1011 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1012 {
1013 stream->writeInt(uniformBlock.psRegisterIndex);
1014 stream->writeInt(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001015 }
1016
Jamie Madill9fc36822015-11-18 13:08:07 -05001017 stream->writeInt(mStreamOutVaryings.size());
1018 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001019 {
Brandon Joneseb994362014-09-24 10:27:28 -07001020 stream->writeString(varying.semanticName);
1021 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001022 stream->writeInt(varying.componentCount);
1023 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001024 }
1025
Brandon Jones22502d52014-08-29 16:58:36 -07001026 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001027 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1028 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001029 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001030 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1031 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001032 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001033 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001034 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001035
Brandon Joneseb994362014-09-24 10:27:28 -07001036 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001037 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001038 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1039 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001040 {
Brandon Joneseb994362014-09-24 10:27:28 -07001041 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001042 stream->writeInt(variable.type);
1043 stream->writeString(variable.name);
1044 stream->writeString(variable.source);
1045 stream->writeInt(variable.outputIndex);
1046 }
1047
Jamie Madill4e31ad52015-10-29 10:32:57 -04001048 stream->writeString(mGeometryShaderPreamble);
1049
Brandon Joneseb994362014-09-24 10:27:28 -07001050 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001051 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1052 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001053 {
1054 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1055
Jamie Madilld3dfda22015-07-06 08:28:49 -04001056 const auto &inputLayout = vertexExecutable->inputs();
1057 stream->writeInt(inputLayout.size());
1058
1059 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001060 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001061 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -07001062 }
1063
1064 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1065 stream->writeInt(vertexShaderSize);
1066
1067 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1068 stream->writeBytes(vertexBlob, vertexShaderSize);
1069 }
1070
1071 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001072 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1073 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001074 {
1075 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1076
1077 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1078 stream->writeInt(outputs.size());
1079 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1080 {
1081 stream->writeInt(outputs[outputIndex]);
1082 }
1083
1084 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1085 stream->writeInt(pixelShaderSize);
1086
1087 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1088 stream->writeBytes(pixelBlob, pixelShaderSize);
1089 }
1090
Jamie Madill4e31ad52015-10-29 10:32:57 -04001091 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001092 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001093 if (geometryExe == nullptr)
1094 {
1095 stream->writeInt(0);
1096 continue;
1097 }
1098
1099 size_t geometryShaderSize = geometryExe->getLength();
1100 stream->writeInt(geometryShaderSize);
1101 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001102 }
1103
Geoff Langb543aff2014-09-30 14:52:54 -04001104 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001105}
1106
Geoff Langc5629752015-12-07 16:29:04 -05001107void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1108{
1109}
1110
Jamie Madill334d6152015-10-22 14:00:28 -04001111gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1112 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001113{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001114 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001115
Jamie Madill85a18042015-03-05 15:41:41 -05001116 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill334d6152015-10-22 14:00:28 -04001117 const gl::AttachmentList &colorbuffers =
1118 fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -07001119
1120 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1121 {
1122 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1123
1124 if (colorbuffer)
1125 {
Jamie Madill334d6152015-10-22 14:00:28 -04001126 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1127 ? GL_COLOR_ATTACHMENT0
1128 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001129 }
1130 else
1131 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001132 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001133 }
1134 }
1135
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001136 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001137}
1138
Jamie Madill97399232014-12-23 12:31:15 -05001139gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001140 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001141 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001142{
1143 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1144 {
1145 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1146 {
Geoff Langb543aff2014-09-30 14:52:54 -04001147 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1148 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001149 }
1150 }
1151
Jamie Madill334d6152015-10-22 14:00:28 -04001152 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1153 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001154
1155 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001156 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001157
1158 gl::InfoLog tempInfoLog;
1159 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1160
Jamie Madillada9ecc2015-08-17 12:53:37 -04001161 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001162 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001163 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
1164 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001165 if (error.isError())
1166 {
1167 return error;
1168 }
Brandon Joneseb994362014-09-24 10:27:28 -07001169
Jamie Madill97399232014-12-23 12:31:15 -05001170 if (pixelExecutable)
1171 {
1172 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1173 }
1174 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001175 {
1176 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001177 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001178 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1179 }
Brandon Jones22502d52014-08-29 16:58:36 -07001180
Geoff Langb543aff2014-09-30 14:52:54 -04001181 *outExectuable = pixelExecutable;
1182 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001183}
1184
Jamie Madilld3dfda22015-07-06 08:28:49 -04001185gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001186 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001187 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001188{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001189 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001190
1191 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1192 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001193 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001194 {
Geoff Langb543aff2014-09-30 14:52:54 -04001195 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1196 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001197 }
1198 }
1199
Brandon Jones22502d52014-08-29 16:58:36 -07001200 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001201 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1202 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001203
1204 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001205 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001206
1207 gl::InfoLog tempInfoLog;
1208 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1209
Jamie Madillada9ecc2015-08-17 12:53:37 -04001210 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001211 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001212 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1213 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001214 if (error.isError())
1215 {
1216 return error;
1217 }
1218
Jamie Madill97399232014-12-23 12:31:15 -05001219 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001220 {
Jamie Madill334d6152015-10-22 14:00:28 -04001221 mVertexExecutables.push_back(
1222 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001223 }
Jamie Madill97399232014-12-23 12:31:15 -05001224 else if (!infoLog)
1225 {
1226 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001227 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001228 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1229 }
Brandon Jones22502d52014-08-29 16:58:36 -07001230
Geoff Langb543aff2014-09-30 14:52:54 -04001231 *outExectuable = vertexExecutable;
1232 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001233}
1234
Jamie Madill4e31ad52015-10-29 10:32:57 -04001235gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Data &data,
1236 GLenum drawMode,
1237 ShaderExecutableD3D **outExecutable,
1238 gl::InfoLog *infoLog)
1239{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001240 if (outExecutable)
1241 {
1242 *outExecutable = nullptr;
1243 }
1244
Austin Kinross88829e82016-01-12 13:04:41 -08001245 // Return a null shader if the current rendering doesn't use a geometry shader
1246 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001247 {
1248 return gl::Error(GL_NO_ERROR);
1249 }
1250
Jamie Madill4e31ad52015-10-29 10:32:57 -04001251 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1252
1253 if (mGeometryExecutables[geometryShaderType] != nullptr)
1254 {
1255 if (outExecutable)
1256 {
1257 *outExecutable = mGeometryExecutables[geometryShaderType];
1258 }
1259 return gl::Error(GL_NO_ERROR);
1260 }
1261
1262 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
1263 geometryShaderType, data, mData, mGeometryShaderPreamble);
1264
1265 gl::InfoLog tempInfoLog;
1266 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1267
1268 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001269 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001270 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
1271 &mGeometryExecutables[geometryShaderType]);
1272
1273 if (!infoLog && error.isError())
1274 {
1275 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1276 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1277 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1278 }
1279
1280 if (outExecutable)
1281 {
1282 *outExecutable = mGeometryExecutables[geometryShaderType];
1283 }
1284 return error;
1285}
1286
1287LinkResult ProgramD3D::compileProgramExecutables(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001288{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001289 const gl::InputLayout &defaultInputLayout =
1290 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001291 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001292 gl::Error error =
1293 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001294 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001295 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001296 return LinkResult(false, error);
1297 }
Austin Kinross434953e2015-02-20 10:49:51 -08001298
Jamie Madill334d6152015-10-22 14:00:28 -04001299 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001300 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001301 error =
1302 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001303 if (error.isError())
1304 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001306 }
Brandon Jones44151a92014-09-10 11:32:25 -07001307
Jamie Madill4e31ad52015-10-29 10:32:57 -04001308 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill847638a2015-11-20 13:01:41 -05001309 ShaderExecutableD3D *pointGS = nullptr;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001310 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001311 {
Jamie Madill847638a2015-11-20 13:01:41 -05001312 getGeometryExecutableForPrimitiveType(data, GL_POINTS, &pointGS, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001313 }
1314
Jamie Madillca03b352015-09-02 12:38:13 -04001315 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001316
1317 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001318 {
Jamie Madill334d6152015-10-22 14:00:28 -04001319 // Geometry shaders are currently only used internally, so there is no corresponding shader
1320 // object at the interface level. For now the geometry shader debug info is prepended to
1321 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001322 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001323 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001324 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1325 }
1326
1327 if (defaultVertexExecutable)
1328 {
1329 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1330 }
1331
1332 if (defaultPixelExecutable)
1333 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001334 const ShaderD3D *fragmentShaderD3D =
1335 GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001336 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1337 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001338
Jamie Madill847638a2015-11-20 13:01:41 -05001339 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable &&
1340 (!usesGeometryShader(GL_POINTS) || pointGS));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001341 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001342}
1343
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001344LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001345{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001346 reset();
1347
1348 // TODO(jmadill): structures containing samplers
1349 for (const gl::LinkedUniform &linkedUniform : mData.getUniforms())
1350 {
1351 if (linkedUniform.isSampler() && linkedUniform.isField())
1352 {
1353 infoLog << "Structures containing samplers not currently supported in D3D.";
1354 return LinkResult(false, gl::Error(GL_NO_ERROR));
1355 }
1356 }
1357
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001358 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1359 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001360
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001361 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1362 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1363
Jamie Madill63069df2015-09-01 17:26:41 +00001364 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001365 mSamplersPS.resize(data.caps->maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001366
Arun Patole44efa0b2015-03-04 17:11:05 +05301367 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001368 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1369
Austin Kinross02df7962015-07-01 10:03:42 -07001370 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1371 {
1372 if (fragmentShaderD3D->usesFrontFacing())
1373 {
1374 infoLog << "The current renderer doesn't support gl_FrontFacing";
1375 return LinkResult(false, gl::Error(GL_NO_ERROR));
1376 }
1377 }
1378
Jamie Madillca03b352015-09-02 12:38:13 -04001379 std::vector<PackedVarying> packedVaryings =
1380 MergeVaryings(*vertexShader, *fragmentShader, mData.getTransformFeedbackVaryingNames());
1381
Brandon Jones22502d52014-08-29 16:58:36 -07001382 // Map the varyings to the register file
Jamie Madill9fc36822015-11-18 13:08:07 -05001383 VaryingPacking varyingPacking(data.caps->maxVaryingVectors);
1384 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
1385 mData.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001386 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001387 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001388 }
1389
Jamie Madille39a3f02015-11-17 20:42:15 -05001390 ProgramD3DMetadata metadata(mRenderer->getMajorShaderModel(), mRenderer->getShaderModelSuffix(),
1391 usesInstancedPointSpriteEmulation(), vertexShaderD3D,
1392 fragmentShaderD3D);
1393
Jamie Madill9fc36822015-11-18 13:08:07 -05001394 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1395 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1396
1397 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.caps->maxVaryingVectors)
1398 {
1399 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1400 return LinkResult(false, gl::Error(GL_NO_ERROR));
1401 }
1402
1403 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1404 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1405 // intelligently, but D3D9 assumes one semantic per register.
1406 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1407 varyingPacking.getMaxSemanticIndex() > data.caps->maxVaryingVectors)
1408 {
1409 infoLog << "Cannot pack these varyings on D3D9.";
1410 return LinkResult(false, gl::Error(GL_NO_ERROR));
1411 }
1412
1413 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mData, metadata, varyingPacking, &mPixelHLSL,
1414 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001415 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001416 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001417 }
1418
Brandon Jones44151a92014-09-10 11:32:25 -07001419 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madille39a3f02015-11-17 20:42:15 -05001420 mDynamicHLSL->getPixelShaderOutputKey(data, mData, metadata, &mPixelShaderKey);
1421 mUsesFragDepth = metadata.usesFragDepth(mData);
Brandon Jones44151a92014-09-10 11:32:25 -07001422
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001423 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001424 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001425 for (const auto &varying : packedVaryings)
1426 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001427 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001428 {
1429 mUsesFlatInterpolation = true;
1430 break;
1431 }
1432 }
1433
Jamie Madill4e31ad52015-10-29 10:32:57 -04001434 if (mRenderer->getMajorShaderModel() >= 4)
1435 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001436 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1437 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001438 }
1439
Jamie Madill63805b42015-08-25 13:17:39 -04001440 initSemanticIndex();
Jamie Madill437d2662014-12-05 14:23:35 -05001441
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001442 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001443
Jamie Madill9fc36822015-11-18 13:08:07 -05001444 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001445
Jamie Madill4e31ad52015-10-29 10:32:57 -04001446 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001447 if (result.error.isError() || !result.linkSuccess)
1448 {
1449 infoLog << "Failed to create D3D shaders.";
1450 return result;
1451 }
1452
Jamie Madill4a3c2342015-10-08 12:58:45 -04001453 initUniformBlockInfo();
1454
Geoff Lang7dd2e102014-11-10 15:19:26 -05001455 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001456}
1457
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001458GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001459{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001460 // TODO(jmadill): Do something useful here?
1461 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001462}
1463
Jamie Madill4a3c2342015-10-08 12:58:45 -04001464void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001465{
1466 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1467
Jamie Madill62d31cb2015-09-11 13:25:51 -04001468 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1469 {
1470 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1471 continue;
1472
Jamie Madill4a3c2342015-10-08 12:58:45 -04001473 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001474 continue;
1475
Jamie Madill4a3c2342015-10-08 12:58:45 -04001476 size_t dataSize = getUniformBlockInfo(vertexBlock);
1477 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001478 }
1479
1480 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1481
1482 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1483 {
1484 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1485 continue;
1486
Jamie Madill4a3c2342015-10-08 12:58:45 -04001487 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001488 continue;
1489
Jamie Madill4a3c2342015-10-08 12:58:45 -04001490 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1491 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001492 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001493}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001494
Jamie Madill4a3c2342015-10-08 12:58:45 -04001495void ProgramD3D::assignUniformBlockRegisters()
1496{
1497 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001498
1499 // Assign registers and update sizes.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001500 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1501 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001502
Jamie Madill4a3c2342015-10-08 12:58:45 -04001503 for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001504 {
1505 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1506
Jamie Madill4a3c2342015-10-08 12:58:45 -04001507 D3DUniformBlock d3dUniformBlock;
1508
Jamie Madill62d31cb2015-09-11 13:25:51 -04001509 if (uniformBlock.vertexStaticUse)
1510 {
1511 unsigned int baseRegister =
1512 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001513 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001514 }
1515
1516 if (uniformBlock.fragmentStaticUse)
1517 {
1518 unsigned int baseRegister =
1519 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001520 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001521 }
1522
Jamie Madill4a3c2342015-10-08 12:58:45 -04001523 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001524 }
1525}
1526
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001527void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001528{
1529 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001530 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001531 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001532 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001533 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001534 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001535 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001536 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001537 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001538 vertexRegisters = std::max(vertexRegisters,
1539 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001540 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001541 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001542 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001543 fragmentRegisters = std::max(
1544 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001545 }
1546 }
1547 }
1548
Jamie Madill334d6152015-10-22 14:00:28 -04001549 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001550 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1551}
1552
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001553gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001554{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001555 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001556
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001557 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001558 if (error.isError())
1559 {
1560 return error;
1561 }
1562
Jamie Madill62d31cb2015-09-11 13:25:51 -04001563 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001564 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001565 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001566 }
1567
1568 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001569}
1570
Jamie Madilld1fe1642015-08-21 16:26:04 -04001571gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001572{
Jamie Madill4a3c2342015-10-08 12:58:45 -04001573 if (mData.getUniformBlocks().empty())
1574 {
1575 return gl::Error(GL_NO_ERROR);
1576 }
1577
1578 // Lazy init.
1579 if (mD3DUniformBlocks.empty())
1580 {
1581 assignUniformBlockRegisters();
1582 }
1583
Jamie Madill03260fa2015-06-22 13:57:22 -04001584 mVertexUBOCache.clear();
1585 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001586
1587 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1588 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1589
Jamie Madill4a3c2342015-10-08 12:58:45 -04001590 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001591 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001592 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001593 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
1594 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001595
Brandon Jones18bd4102014-09-22 14:21:44 -07001596 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001597 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001598 {
1599 continue;
1600 }
1601
Jamie Madill4a3c2342015-10-08 12:58:45 -04001602 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001603 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001604 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001605 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001606
Jamie Madill969194d2015-07-20 14:36:56 -04001607 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001608 {
1609 mVertexUBOCache.resize(registerIndex + 1, -1);
1610 }
1611
1612 ASSERT(mVertexUBOCache[registerIndex] == -1);
1613 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001614 }
1615
Jamie Madill4a3c2342015-10-08 12:58:45 -04001616 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001617 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001618 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001619 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001620
1621 if (mFragmentUBOCache.size() <= registerIndex)
1622 {
1623 mFragmentUBOCache.resize(registerIndex + 1, -1);
1624 }
1625
1626 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1627 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001628 }
1629 }
1630
Jamie Madill03260fa2015-06-22 13:57:22 -04001631 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001632}
1633
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001634void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001635{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001636 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001637 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001638 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001639 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001640}
1641
Jamie Madill334d6152015-10-22 14:00:28 -04001642void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001643{
1644 setUniform(location, count, v, GL_FLOAT);
1645}
1646
1647void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1648{
1649 setUniform(location, count, v, GL_FLOAT_VEC2);
1650}
1651
1652void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1653{
1654 setUniform(location, count, v, GL_FLOAT_VEC3);
1655}
1656
1657void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1658{
1659 setUniform(location, count, v, GL_FLOAT_VEC4);
1660}
1661
Jamie Madill334d6152015-10-22 14:00:28 -04001662void ProgramD3D::setUniformMatrix2fv(GLint location,
1663 GLsizei count,
1664 GLboolean transpose,
1665 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001666{
1667 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1668}
1669
Jamie Madill334d6152015-10-22 14:00:28 -04001670void ProgramD3D::setUniformMatrix3fv(GLint location,
1671 GLsizei count,
1672 GLboolean transpose,
1673 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001674{
1675 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1676}
1677
Jamie Madill334d6152015-10-22 14:00:28 -04001678void ProgramD3D::setUniformMatrix4fv(GLint location,
1679 GLsizei count,
1680 GLboolean transpose,
1681 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001682{
1683 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1684}
1685
Jamie Madill334d6152015-10-22 14:00:28 -04001686void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1687 GLsizei count,
1688 GLboolean transpose,
1689 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001690{
1691 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1692}
1693
Jamie Madill334d6152015-10-22 14:00:28 -04001694void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1695 GLsizei count,
1696 GLboolean transpose,
1697 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001698{
1699 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1700}
1701
Jamie Madill334d6152015-10-22 14:00:28 -04001702void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1703 GLsizei count,
1704 GLboolean transpose,
1705 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001706{
1707 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1708}
1709
Jamie Madill334d6152015-10-22 14:00:28 -04001710void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1711 GLsizei count,
1712 GLboolean transpose,
1713 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001714{
1715 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1716}
1717
Jamie Madill334d6152015-10-22 14:00:28 -04001718void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1719 GLsizei count,
1720 GLboolean transpose,
1721 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001722{
1723 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1724}
1725
Jamie Madill334d6152015-10-22 14:00:28 -04001726void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1727 GLsizei count,
1728 GLboolean transpose,
1729 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001730{
1731 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1732}
1733
1734void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1735{
1736 setUniform(location, count, v, GL_INT);
1737}
1738
1739void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1740{
1741 setUniform(location, count, v, GL_INT_VEC2);
1742}
1743
1744void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1745{
1746 setUniform(location, count, v, GL_INT_VEC3);
1747}
1748
1749void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1750{
1751 setUniform(location, count, v, GL_INT_VEC4);
1752}
1753
1754void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1755{
1756 setUniform(location, count, v, GL_UNSIGNED_INT);
1757}
1758
1759void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1760{
1761 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1762}
1763
1764void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1765{
1766 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1767}
1768
1769void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1770{
1771 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1772}
1773
Jamie Madill4a3c2342015-10-08 12:58:45 -04001774void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1775 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001776{
1777}
1778
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001779void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001780{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001781 D3DUniformMap uniformMap;
Jamie Madill334d6152015-10-22 14:00:28 -04001782 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001783 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001784
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001785 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001786 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001787 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001788 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001789 }
1790 }
1791
Jamie Madill334d6152015-10-22 14:00:28 -04001792 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001793 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001794 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001795 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001796 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001797 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001798 }
1799 }
1800
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001801 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
1802 for (const gl::LinkedUniform &glUniform : mData.getUniforms())
1803 {
1804 if (!glUniform.isInDefaultBlock())
1805 continue;
1806
1807 auto mapEntry = uniformMap.find(glUniform.name);
1808 ASSERT(mapEntry != uniformMap.end());
1809 mD3DUniforms.push_back(mapEntry->second);
1810 }
1811
Jamie Madill62d31cb2015-09-11 13:25:51 -04001812 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001813 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001814}
1815
Jamie Madill91445bc2015-09-23 16:47:53 -04001816void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001817 const sh::Uniform &uniform,
1818 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001819{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001820 if (uniform.isBuiltIn())
Jamie Madillfb536032015-09-11 13:19:49 -04001821 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001822 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001823 return;
1824 }
1825
Jamie Madill91445bc2015-09-23 16:47:53 -04001826 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1827
1828 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1829 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001830 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001831 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001832
Jamie Madill91445bc2015-09-23 16:47:53 -04001833 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001834}
1835
Jamie Madill62d31cb2015-09-11 13:25:51 -04001836D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1837{
1838 for (D3DUniform *d3dUniform : mD3DUniforms)
1839 {
1840 if (d3dUniform->name == name)
1841 {
1842 return d3dUniform;
1843 }
1844 }
1845
1846 return nullptr;
1847}
1848
Jamie Madill91445bc2015-09-23 16:47:53 -04001849void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001850 const sh::ShaderVariable &uniform,
1851 const std::string &fullName,
1852 sh::HLSLBlockEncoder *encoder,
1853 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001854{
1855 if (uniform.isStruct())
1856 {
1857 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1858 {
1859 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1860
Jamie Madill55def582015-05-04 11:24:57 -04001861 if (encoder)
1862 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001863
1864 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1865 {
Jamie Madill334d6152015-10-22 14:00:28 -04001866 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001867 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1868
Jamie Madill91445bc2015-09-23 16:47:53 -04001869 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001870 }
1871
Jamie Madill55def582015-05-04 11:24:57 -04001872 if (encoder)
1873 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001874 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001875 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001876 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001877
1878 // Not a struct. Arrays are treated as aggregate types.
1879 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001880 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001881 encoder->enterAggregateType();
1882 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001883
Jamie Madill62d31cb2015-09-11 13:25:51 -04001884 // Advance the uniform offset, to track registers allocation for structs
1885 sh::BlockMemberInfo blockInfo =
1886 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1887 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001888
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001889 auto uniformMapEntry = uniformMap->find(fullName);
1890 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001891
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001892 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001893 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001894 d3dUniform = uniformMapEntry->second;
1895 }
1896 else
1897 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001898 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001899 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001900 }
1901
1902 if (encoder)
1903 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001904 d3dUniform->registerElement =
1905 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001906 unsigned int reg =
1907 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001908 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001909 {
1910 d3dUniform->psRegisterIndex = reg;
1911 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001912 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001913 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001914 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001915 d3dUniform->vsRegisterIndex = reg;
1916 }
Jamie Madillfb536032015-09-11 13:19:49 -04001917
1918 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001919 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001920 {
1921 encoder->exitAggregateType();
1922 }
1923 }
1924}
1925
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001926template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001927void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001928{
Jamie Madill334d6152015-10-22 14:00:28 -04001929 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001930 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1931
Jamie Madill62d31cb2015-09-11 13:25:51 -04001932 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001933
Jamie Madill62d31cb2015-09-11 13:25:51 -04001934 unsigned int elementCount = targetUniform->elementCount();
1935 unsigned int arrayElement = mData.getUniformLocations()[location].element;
1936 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001937
1938 if (targetUniform->type == targetUniformType)
1939 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001940 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001941
Jamie Madill62d31cb2015-09-11 13:25:51 -04001942 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001943 {
Jamie Madill334d6152015-10-22 14:00:28 -04001944 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001945 const T *source = v + (i * components);
1946
1947 for (int c = 0; c < components; c++)
1948 {
1949 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1950 }
1951 for (int c = components; c < 4; c++)
1952 {
1953 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1954 }
1955 }
1956 }
1957 else if (targetUniform->type == targetBoolType)
1958 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001959 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001960
Jamie Madill62d31cb2015-09-11 13:25:51 -04001961 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001962 {
Jamie Madill334d6152015-10-22 14:00:28 -04001963 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001964 const T *source = v + (i * components);
1965
1966 for (int c = 0; c < components; c++)
1967 {
Jamie Madill334d6152015-10-22 14:00:28 -04001968 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1969 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001970 }
1971 for (int c = components; c < 4; c++)
1972 {
1973 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1974 }
1975 }
1976 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001977 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001978 {
1979 ASSERT(targetUniformType == GL_INT);
1980
Jamie Madill62d31cb2015-09-11 13:25:51 -04001981 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001982
1983 bool wasDirty = targetUniform->dirty;
1984
Jamie Madill62d31cb2015-09-11 13:25:51 -04001985 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986 {
Jamie Madill334d6152015-10-22 14:00:28 -04001987 GLint *dest = target + (i * 4);
1988 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001989
1990 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1991 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1992 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1993 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1994 }
1995
1996 if (!wasDirty && targetUniform->dirty)
1997 {
1998 mDirtySamplerMapping = true;
1999 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002000 }
Jamie Madill334d6152015-10-22 14:00:28 -04002001 else
2002 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002003}
2004
2005template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002006void ProgramD3D::setUniformMatrixfv(GLint location,
2007 GLsizei countIn,
2008 GLboolean transpose,
2009 const GLfloat *value,
2010 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002011{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002012 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002013
Jamie Madill62d31cb2015-09-11 13:25:51 -04002014 unsigned int elementCount = targetUniform->elementCount();
2015 unsigned int arrayElement = mData.getUniformLocations()[location].element;
2016 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002019 GLfloat *target =
2020 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002021
Jamie Madill62d31cb2015-09-11 13:25:51 -04002022 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002023 {
2024 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2025 if (transpose == GL_FALSE)
2026 {
Jamie Madill334d6152015-10-22 14:00:28 -04002027 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2028 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002029 }
2030 else
2031 {
Jamie Madill334d6152015-10-22 14:00:28 -04002032 targetUniform->dirty =
2033 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002034 }
2035 target += targetMatrixStride;
2036 value += cols * rows;
2037 }
2038}
2039
Jamie Madill4a3c2342015-10-08 12:58:45 -04002040size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002041{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002042 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002043
Jamie Madill62d31cb2015-09-11 13:25:51 -04002044 // define member uniforms
2045 sh::Std140BlockEncoder std140Encoder;
2046 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2047 sh::BlockLayoutEncoder *encoder = nullptr;
2048
2049 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002050 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002051 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002052 }
2053 else
2054 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002055 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002056 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002057
2058 GetUniformBlockInfo(interfaceBlock.fields, "", encoder, interfaceBlock.isRowMajorLayout,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002059 &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002060
2061 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002062}
2063
Jamie Madill62d31cb2015-09-11 13:25:51 -04002064void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002065{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002066 for (const D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002067 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002068 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002069 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002070 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002071 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002072 }
2073}
2074
Jamie Madill62d31cb2015-09-11 13:25:51 -04002075void ProgramD3D::assignSamplerRegisters(const D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002076{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002077 ASSERT(d3dUniform->isSampler());
2078 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX ||
2079 d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madillfb536032015-09-11 13:19:49 -04002080
Jamie Madill62d31cb2015-09-11 13:25:51 -04002081 if (d3dUniform->vsRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002082 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002083 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2084 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002085 }
2086
Jamie Madill62d31cb2015-09-11 13:25:51 -04002087 if (d3dUniform->psRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002088 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002089 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2090 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002091 }
2092}
2093
Jamie Madill62d31cb2015-09-11 13:25:51 -04002094// static
2095void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002096 GLenum samplerType,
2097 unsigned int samplerCount,
2098 std::vector<Sampler> &outSamplers,
2099 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002100{
2101 unsigned int samplerIndex = startSamplerIndex;
2102
2103 do
2104 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002105 ASSERT(samplerIndex < outSamplers.size());
2106 Sampler *sampler = &outSamplers[samplerIndex];
2107 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002108 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002109 sampler->logicalTextureUnit = 0;
2110 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002111 samplerIndex++;
2112 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002113}
2114
Brandon Jonesc9610c52014-08-25 17:02:59 -07002115void ProgramD3D::reset()
2116{
Brandon Joneseb994362014-09-24 10:27:28 -07002117 SafeDeleteContainer(mVertexExecutables);
2118 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002119
2120 for (auto &element : mGeometryExecutables)
2121 {
2122 SafeDelete(element);
2123 }
Brandon Joneseb994362014-09-24 10:27:28 -07002124
Brandon Jones22502d52014-08-29 16:58:36 -07002125 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002126 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002127
2128 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002129 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002130 mUsesFragDepth = false;
2131 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002132 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002133 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002134
Jamie Madill62d31cb2015-09-11 13:25:51 -04002135 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002136 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002137
Brandon Jonesc9610c52014-08-25 17:02:59 -07002138 SafeDelete(mVertexUniformStorage);
2139 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002140
2141 mSamplersPS.clear();
2142 mSamplersVS.clear();
2143
2144 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002145 mUsedPixelSamplerRange = 0;
2146 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002147
Jamie Madill63805b42015-08-25 13:17:39 -04002148 std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1);
Jamie Madill437d2662014-12-05 14:23:35 -05002149 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002150
Jamie Madill9fc36822015-11-18 13:08:07 -05002151 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002152
2153 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002154}
2155
Geoff Lang7dd2e102014-11-10 15:19:26 -05002156unsigned int ProgramD3D::getSerial() const
2157{
2158 return mSerial;
2159}
2160
2161unsigned int ProgramD3D::issueSerial()
2162{
2163 return mCurrentSerial++;
2164}
2165
Jamie Madill63805b42015-08-25 13:17:39 -04002166void ProgramD3D::initSemanticIndex()
2167{
2168 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2169 ASSERT(vertexShader != nullptr);
2170
2171 // Init semantic index
2172 for (const sh::Attribute &attribute : mData.getAttributes())
2173 {
2174 int attributeIndex = attribute.location;
2175 int index = vertexShader->getSemanticIndex(attribute.name);
2176 int regs = gl::VariableRegisterCount(attribute.type);
2177
2178 for (int reg = 0; reg < regs; ++reg)
2179 {
2180 mSemanticIndexes[attributeIndex + reg] = index + reg;
2181 }
2182 }
2183
2184 initAttributesByLayout();
2185}
2186
Jamie Madill437d2662014-12-05 14:23:35 -05002187void ProgramD3D::initAttributesByLayout()
2188{
2189 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2190 {
2191 mAttributesByLayout[i] = i;
2192 }
2193
Jamie Madill63805b42015-08-25 13:17:39 -04002194 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS],
2195 AttributeSorter(mSemanticIndexes));
Jamie Madill437d2662014-12-05 14:23:35 -05002196}
2197
Jamie Madill334d6152015-10-22 14:00:28 -04002198void ProgramD3D::sortAttributesByLayout(
2199 const std::vector<TranslatedAttribute> &unsortedAttributes,
2200 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2201 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002202{
Jamie Madill476682e2015-06-30 10:04:29 -04002203 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002204 {
Jamie Madill334d6152015-10-22 14:00:28 -04002205 int oldIndex = mAttributesByLayout[attribIndex];
Jamie Madill63805b42015-08-25 13:17:39 -04002206 sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex];
Jamie Madill334d6152015-10-22 14:00:28 -04002207 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002208 }
2209}
2210
Jamie Madill63805b42015-08-25 13:17:39 -04002211void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002212{
Jamie Madillbd136f92015-08-10 14:51:37 -04002213 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002214 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002215
Dian Xianga4928832015-09-15 10:11:17 -07002216 for (unsigned int attributeIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002217 {
Jamie Madill63805b42015-08-25 13:17:39 -04002218 int semanticIndex = mSemanticIndexes[attributeIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002219
2220 if (semanticIndex != -1)
2221 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002222 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2223 {
2224 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2225 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002226 mCachedInputLayout[semanticIndex] =
2227 GetVertexFormatType(vertexAttributes[attributeIndex],
2228 state.getVertexAttribCurrentValue(attributeIndex).Type);
2229 }
2230 }
2231}
2232
Jamie Madill9fc36822015-11-18 13:08:07 -05002233void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002234{
Jamie Madill9fc36822015-11-18 13:08:07 -05002235 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2236
2237 const std::string &varyingSemantic =
2238 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2239
Jamie Madillccdf74b2015-08-18 10:46:12 -04002240 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002241 mStreamOutVaryings.clear();
2242
2243 const auto &tfVaryingNames = mData.getTransformFeedbackVaryingNames();
2244 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2245 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002246 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002247 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2248 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002249 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002250 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002251 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002252 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2253 builtins.glPosition.index, 4, outputSlot));
2254 }
2255 }
2256 else if (tfVaryingName == "gl_FragCoord")
2257 {
2258 if (builtins.glFragCoord.enabled)
2259 {
2260 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2261 builtins.glFragCoord.index, 4, outputSlot));
2262 }
2263 }
2264 else if (tfVaryingName == "gl_PointSize")
2265 {
2266 if (builtins.glPointSize.enabled)
2267 {
2268 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2269 }
2270 }
2271 else
2272 {
2273 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2274 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002275 const auto &varying = *registerInfo.packedVarying->varying;
2276 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002277 int componentCount = gl::VariableColumnCount(transposedType);
2278 ASSERT(!varying.isBuiltIn());
2279
Jamie Madill55c25d02015-11-18 13:08:08 -05002280 // Transform feedback for varying structs is underspecified.
2281 // See Khronos bug 9856.
2282 // TODO(jmadill): Figure out how to be spec-compliant here.
2283 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2284 continue;
2285
Jamie Madill9fc36822015-11-18 13:08:07 -05002286 // There can be more than one register assigned to a particular varying, and each
2287 // register needs its own stream out entry.
2288 if (tfVaryingName == varying.name)
2289 {
2290 mStreamOutVaryings.push_back(D3DVarying(
2291 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2292 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002293 }
2294 }
2295 }
2296}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002297
2298D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2299{
2300 return mD3DUniforms[mData.getUniformLocations()[location].index];
2301}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002302
2303bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2304{
2305 std::string baseName = blockName;
2306 gl::ParseAndStripArrayIndex(&baseName);
2307
2308 auto sizeIter = mBlockDataSizes.find(baseName);
2309 if (sizeIter == mBlockDataSizes.end())
2310 {
2311 *sizeOut = 0;
2312 return false;
2313 }
2314
2315 *sizeOut = sizeIter->second;
2316 return true;
2317}
2318
2319bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2320 sh::BlockMemberInfo *memberInfoOut) const
2321{
2322 auto infoIter = mBlockInfo.find(memberUniformName);
2323 if (infoIter == mBlockInfo.end())
2324 {
2325 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2326 return false;
2327 }
2328
2329 *memberInfoOut = infoIter->second;
2330 return true;
2331}
Brandon Jonesc9610c52014-08-25 17:02:59 -07002332}