blob: d6a2a2735ca994874ad834a6aad8b98605d3cf58 [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
Jamie Madill334d6152015-10-22 14:00:28 -04001107gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1108 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001109{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001110 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001111
Jamie Madill85a18042015-03-05 15:41:41 -05001112 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill334d6152015-10-22 14:00:28 -04001113 const gl::AttachmentList &colorbuffers =
1114 fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -07001115
1116 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1117 {
1118 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1119
1120 if (colorbuffer)
1121 {
Jamie Madill334d6152015-10-22 14:00:28 -04001122 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1123 ? GL_COLOR_ATTACHMENT0
1124 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001125 }
1126 else
1127 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001128 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001129 }
1130 }
1131
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001132 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001133}
1134
Jamie Madill97399232014-12-23 12:31:15 -05001135gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001136 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001137 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001138{
1139 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1140 {
1141 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1142 {
Geoff Langb543aff2014-09-30 14:52:54 -04001143 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1144 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001145 }
1146 }
1147
Jamie Madill334d6152015-10-22 14:00:28 -04001148 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1149 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001150
1151 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001152 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001153
1154 gl::InfoLog tempInfoLog;
1155 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1156
Jamie Madillada9ecc2015-08-17 12:53:37 -04001157 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001158 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001159 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
1160 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001161 if (error.isError())
1162 {
1163 return error;
1164 }
Brandon Joneseb994362014-09-24 10:27:28 -07001165
Jamie Madill97399232014-12-23 12:31:15 -05001166 if (pixelExecutable)
1167 {
1168 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1169 }
1170 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001171 {
1172 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001173 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001174 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1175 }
Brandon Jones22502d52014-08-29 16:58:36 -07001176
Geoff Langb543aff2014-09-30 14:52:54 -04001177 *outExectuable = pixelExecutable;
1178 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001179}
1180
Jamie Madilld3dfda22015-07-06 08:28:49 -04001181gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001182 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001183 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001184{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001185 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001186
1187 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1188 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001189 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001190 {
Geoff Langb543aff2014-09-30 14:52:54 -04001191 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1192 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001193 }
1194 }
1195
Brandon Jones22502d52014-08-29 16:58:36 -07001196 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001197 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1198 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001199
1200 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001201 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001202
1203 gl::InfoLog tempInfoLog;
1204 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1205
Jamie Madillada9ecc2015-08-17 12:53:37 -04001206 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001207 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001208 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1209 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001210 if (error.isError())
1211 {
1212 return error;
1213 }
1214
Jamie Madill97399232014-12-23 12:31:15 -05001215 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001216 {
Jamie Madill334d6152015-10-22 14:00:28 -04001217 mVertexExecutables.push_back(
1218 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001219 }
Jamie Madill97399232014-12-23 12:31:15 -05001220 else if (!infoLog)
1221 {
1222 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001223 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001224 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1225 }
Brandon Jones22502d52014-08-29 16:58:36 -07001226
Geoff Langb543aff2014-09-30 14:52:54 -04001227 *outExectuable = vertexExecutable;
1228 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001229}
1230
Jamie Madill4e31ad52015-10-29 10:32:57 -04001231gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Data &data,
1232 GLenum drawMode,
1233 ShaderExecutableD3D **outExecutable,
1234 gl::InfoLog *infoLog)
1235{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001236 if (outExecutable)
1237 {
1238 *outExecutable = nullptr;
1239 }
1240
1241 // We only uses a geometry shader for point sprite emulation, or for fixing the provoking
1242 // vertex problem. Otherwise, return a null shader.
1243 if (drawMode != GL_POINTS && !mUsesFlatInterpolation)
1244 {
1245 return gl::Error(GL_NO_ERROR);
1246 }
1247
Jamie Madill4e31ad52015-10-29 10:32:57 -04001248 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1249
1250 if (mGeometryExecutables[geometryShaderType] != nullptr)
1251 {
1252 if (outExecutable)
1253 {
1254 *outExecutable = mGeometryExecutables[geometryShaderType];
1255 }
1256 return gl::Error(GL_NO_ERROR);
1257 }
1258
1259 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
1260 geometryShaderType, data, mData, mGeometryShaderPreamble);
1261
1262 gl::InfoLog tempInfoLog;
1263 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1264
1265 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001266 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001267 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
1268 &mGeometryExecutables[geometryShaderType]);
1269
1270 if (!infoLog && error.isError())
1271 {
1272 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1273 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1274 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1275 }
1276
1277 if (outExecutable)
1278 {
1279 *outExecutable = mGeometryExecutables[geometryShaderType];
1280 }
1281 return error;
1282}
1283
1284LinkResult ProgramD3D::compileProgramExecutables(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001285{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001286 const gl::InputLayout &defaultInputLayout =
1287 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001288 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001289 gl::Error error =
1290 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001291 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001292 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001293 return LinkResult(false, error);
1294 }
Austin Kinross434953e2015-02-20 10:49:51 -08001295
Jamie Madill334d6152015-10-22 14:00:28 -04001296 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001297 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001298 error =
1299 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001300 if (error.isError())
1301 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001302 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001303 }
Brandon Jones44151a92014-09-10 11:32:25 -07001304
Jamie Madill4e31ad52015-10-29 10:32:57 -04001305 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill847638a2015-11-20 13:01:41 -05001306 ShaderExecutableD3D *pointGS = nullptr;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001307 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001308 {
Jamie Madill847638a2015-11-20 13:01:41 -05001309 getGeometryExecutableForPrimitiveType(data, GL_POINTS, &pointGS, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001310 }
1311
Jamie Madillca03b352015-09-02 12:38:13 -04001312 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001313
1314 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001315 {
Jamie Madill334d6152015-10-22 14:00:28 -04001316 // Geometry shaders are currently only used internally, so there is no corresponding shader
1317 // object at the interface level. For now the geometry shader debug info is prepended to
1318 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001319 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001320 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001321 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1322 }
1323
1324 if (defaultVertexExecutable)
1325 {
1326 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1327 }
1328
1329 if (defaultPixelExecutable)
1330 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001331 const ShaderD3D *fragmentShaderD3D =
1332 GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001333 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1334 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001335
Jamie Madill847638a2015-11-20 13:01:41 -05001336 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable &&
1337 (!usesGeometryShader(GL_POINTS) || pointGS));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001338 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001339}
1340
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001341LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001342{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001343 reset();
1344
1345 // TODO(jmadill): structures containing samplers
1346 for (const gl::LinkedUniform &linkedUniform : mData.getUniforms())
1347 {
1348 if (linkedUniform.isSampler() && linkedUniform.isField())
1349 {
1350 infoLog << "Structures containing samplers not currently supported in D3D.";
1351 return LinkResult(false, gl::Error(GL_NO_ERROR));
1352 }
1353 }
1354
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001355 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1356 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001357
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001358 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1359 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1360
Jamie Madill63069df2015-09-01 17:26:41 +00001361 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001362 mSamplersPS.resize(data.caps->maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001363
Arun Patole44efa0b2015-03-04 17:11:05 +05301364 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001365 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1366
Austin Kinross02df7962015-07-01 10:03:42 -07001367 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1368 {
1369 if (fragmentShaderD3D->usesFrontFacing())
1370 {
1371 infoLog << "The current renderer doesn't support gl_FrontFacing";
1372 return LinkResult(false, gl::Error(GL_NO_ERROR));
1373 }
1374 }
1375
Jamie Madillca03b352015-09-02 12:38:13 -04001376 std::vector<PackedVarying> packedVaryings =
1377 MergeVaryings(*vertexShader, *fragmentShader, mData.getTransformFeedbackVaryingNames());
1378
Brandon Jones22502d52014-08-29 16:58:36 -07001379 // Map the varyings to the register file
Jamie Madill9fc36822015-11-18 13:08:07 -05001380 VaryingPacking varyingPacking(data.caps->maxVaryingVectors);
1381 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
1382 mData.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001383 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001384 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001385 }
1386
Jamie Madille39a3f02015-11-17 20:42:15 -05001387 ProgramD3DMetadata metadata(mRenderer->getMajorShaderModel(), mRenderer->getShaderModelSuffix(),
1388 usesInstancedPointSpriteEmulation(), vertexShaderD3D,
1389 fragmentShaderD3D);
1390
Jamie Madill9fc36822015-11-18 13:08:07 -05001391 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1392 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1393
1394 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.caps->maxVaryingVectors)
1395 {
1396 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1397 return LinkResult(false, gl::Error(GL_NO_ERROR));
1398 }
1399
1400 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1401 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1402 // intelligently, but D3D9 assumes one semantic per register.
1403 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1404 varyingPacking.getMaxSemanticIndex() > data.caps->maxVaryingVectors)
1405 {
1406 infoLog << "Cannot pack these varyings on D3D9.";
1407 return LinkResult(false, gl::Error(GL_NO_ERROR));
1408 }
1409
1410 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mData, metadata, varyingPacking, &mPixelHLSL,
1411 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001412 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001413 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001414 }
1415
Brandon Jones44151a92014-09-10 11:32:25 -07001416 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madille39a3f02015-11-17 20:42:15 -05001417 mDynamicHLSL->getPixelShaderOutputKey(data, mData, metadata, &mPixelShaderKey);
1418 mUsesFragDepth = metadata.usesFragDepth(mData);
Brandon Jones44151a92014-09-10 11:32:25 -07001419
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001420 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001421 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001422 for (const auto &varying : packedVaryings)
1423 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001424 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001425 {
1426 mUsesFlatInterpolation = true;
1427 break;
1428 }
1429 }
1430
Jamie Madill4e31ad52015-10-29 10:32:57 -04001431 if (mRenderer->getMajorShaderModel() >= 4)
1432 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001433 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1434 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001435 }
1436
Jamie Madill63805b42015-08-25 13:17:39 -04001437 initSemanticIndex();
Jamie Madill437d2662014-12-05 14:23:35 -05001438
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001439 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001440
Jamie Madill9fc36822015-11-18 13:08:07 -05001441 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001442
Jamie Madill4e31ad52015-10-29 10:32:57 -04001443 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001444 if (result.error.isError() || !result.linkSuccess)
1445 {
1446 infoLog << "Failed to create D3D shaders.";
1447 return result;
1448 }
1449
Jamie Madill4a3c2342015-10-08 12:58:45 -04001450 initUniformBlockInfo();
1451
Geoff Lang7dd2e102014-11-10 15:19:26 -05001452 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001453}
1454
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001455GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001456{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001457 // TODO(jmadill): Do something useful here?
1458 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001459}
1460
Jamie Madill4a3c2342015-10-08 12:58:45 -04001461void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001462{
1463 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1464
Jamie Madill62d31cb2015-09-11 13:25:51 -04001465 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1466 {
1467 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1468 continue;
1469
Jamie Madill4a3c2342015-10-08 12:58:45 -04001470 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001471 continue;
1472
Jamie Madill4a3c2342015-10-08 12:58:45 -04001473 size_t dataSize = getUniformBlockInfo(vertexBlock);
1474 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001475 }
1476
1477 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1478
1479 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1480 {
1481 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1482 continue;
1483
Jamie Madill4a3c2342015-10-08 12:58:45 -04001484 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001485 continue;
1486
Jamie Madill4a3c2342015-10-08 12:58:45 -04001487 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1488 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001489 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001490}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001491
Jamie Madill4a3c2342015-10-08 12:58:45 -04001492void ProgramD3D::assignUniformBlockRegisters()
1493{
1494 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001495
1496 // Assign registers and update sizes.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001497 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1498 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001499
Jamie Madill4a3c2342015-10-08 12:58:45 -04001500 for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001501 {
1502 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1503
Jamie Madill4a3c2342015-10-08 12:58:45 -04001504 D3DUniformBlock d3dUniformBlock;
1505
Jamie Madill62d31cb2015-09-11 13:25:51 -04001506 if (uniformBlock.vertexStaticUse)
1507 {
1508 unsigned int baseRegister =
1509 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001510 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001511 }
1512
1513 if (uniformBlock.fragmentStaticUse)
1514 {
1515 unsigned int baseRegister =
1516 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001517 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001518 }
1519
Jamie Madill4a3c2342015-10-08 12:58:45 -04001520 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001521 }
1522}
1523
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001524void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001525{
1526 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001527 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001528 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001529 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001530 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001531 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001532 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001533 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001534 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001535 vertexRegisters = std::max(vertexRegisters,
1536 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001537 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001538 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001539 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001540 fragmentRegisters = std::max(
1541 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001542 }
1543 }
1544 }
1545
Jamie Madill334d6152015-10-22 14:00:28 -04001546 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001547 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1548}
1549
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001550gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001551{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001552 updateSamplerMapping();
1553
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001554 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001555 if (error.isError())
1556 {
1557 return error;
1558 }
1559
Jamie Madill62d31cb2015-09-11 13:25:51 -04001560 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001561 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001562 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001563 }
1564
1565 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001566}
1567
Jamie Madilld1fe1642015-08-21 16:26:04 -04001568gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001569{
Jamie Madill4a3c2342015-10-08 12:58:45 -04001570 if (mData.getUniformBlocks().empty())
1571 {
1572 return gl::Error(GL_NO_ERROR);
1573 }
1574
1575 // Lazy init.
1576 if (mD3DUniformBlocks.empty())
1577 {
1578 assignUniformBlockRegisters();
1579 }
1580
Jamie Madill03260fa2015-06-22 13:57:22 -04001581 mVertexUBOCache.clear();
1582 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001583
1584 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1585 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1586
Jamie Madill4a3c2342015-10-08 12:58:45 -04001587 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001588 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001589 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001590 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
1591 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001592
Brandon Jones18bd4102014-09-22 14:21:44 -07001593 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001594 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001595 {
1596 continue;
1597 }
1598
Jamie Madill4a3c2342015-10-08 12:58:45 -04001599 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001600 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001601 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001602 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001603
Jamie Madill969194d2015-07-20 14:36:56 -04001604 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001605 {
1606 mVertexUBOCache.resize(registerIndex + 1, -1);
1607 }
1608
1609 ASSERT(mVertexUBOCache[registerIndex] == -1);
1610 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001611 }
1612
Jamie Madill4a3c2342015-10-08 12:58:45 -04001613 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001614 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001615 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001616 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001617
1618 if (mFragmentUBOCache.size() <= registerIndex)
1619 {
1620 mFragmentUBOCache.resize(registerIndex + 1, -1);
1621 }
1622
1623 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1624 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001625 }
1626 }
1627
Jamie Madill03260fa2015-06-22 13:57:22 -04001628 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001629}
1630
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001631void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001632{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001633 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001634 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001635 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001636 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001637}
1638
Jamie Madill334d6152015-10-22 14:00:28 -04001639void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001640{
1641 setUniform(location, count, v, GL_FLOAT);
1642}
1643
1644void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1645{
1646 setUniform(location, count, v, GL_FLOAT_VEC2);
1647}
1648
1649void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1650{
1651 setUniform(location, count, v, GL_FLOAT_VEC3);
1652}
1653
1654void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1655{
1656 setUniform(location, count, v, GL_FLOAT_VEC4);
1657}
1658
Jamie Madill334d6152015-10-22 14:00:28 -04001659void ProgramD3D::setUniformMatrix2fv(GLint location,
1660 GLsizei count,
1661 GLboolean transpose,
1662 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001663{
1664 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1665}
1666
Jamie Madill334d6152015-10-22 14:00:28 -04001667void ProgramD3D::setUniformMatrix3fv(GLint location,
1668 GLsizei count,
1669 GLboolean transpose,
1670 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001671{
1672 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1673}
1674
Jamie Madill334d6152015-10-22 14:00:28 -04001675void ProgramD3D::setUniformMatrix4fv(GLint location,
1676 GLsizei count,
1677 GLboolean transpose,
1678 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001679{
1680 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1681}
1682
Jamie Madill334d6152015-10-22 14:00:28 -04001683void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1684 GLsizei count,
1685 GLboolean transpose,
1686 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001687{
1688 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1689}
1690
Jamie Madill334d6152015-10-22 14:00:28 -04001691void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1692 GLsizei count,
1693 GLboolean transpose,
1694 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001695{
1696 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1697}
1698
Jamie Madill334d6152015-10-22 14:00:28 -04001699void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1700 GLsizei count,
1701 GLboolean transpose,
1702 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001703{
1704 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1705}
1706
Jamie Madill334d6152015-10-22 14:00:28 -04001707void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1708 GLsizei count,
1709 GLboolean transpose,
1710 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001711{
1712 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1713}
1714
Jamie Madill334d6152015-10-22 14:00:28 -04001715void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1716 GLsizei count,
1717 GLboolean transpose,
1718 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001719{
1720 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1721}
1722
Jamie Madill334d6152015-10-22 14:00:28 -04001723void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1724 GLsizei count,
1725 GLboolean transpose,
1726 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001727{
1728 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1729}
1730
1731void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1732{
1733 setUniform(location, count, v, GL_INT);
1734}
1735
1736void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1737{
1738 setUniform(location, count, v, GL_INT_VEC2);
1739}
1740
1741void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1742{
1743 setUniform(location, count, v, GL_INT_VEC3);
1744}
1745
1746void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1747{
1748 setUniform(location, count, v, GL_INT_VEC4);
1749}
1750
1751void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1752{
1753 setUniform(location, count, v, GL_UNSIGNED_INT);
1754}
1755
1756void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1757{
1758 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1759}
1760
1761void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1762{
1763 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1764}
1765
1766void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1767{
1768 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1769}
1770
Jamie Madill4a3c2342015-10-08 12:58:45 -04001771void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1772 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001773{
1774}
1775
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001776void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001777{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001778 D3DUniformMap uniformMap;
Jamie Madill334d6152015-10-22 14:00:28 -04001779 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001780 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001781
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001782 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001783 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001784 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001785 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001786 }
1787 }
1788
Jamie Madill334d6152015-10-22 14:00:28 -04001789 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001790 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001791 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001792 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001793 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001794 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001795 }
1796 }
1797
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001798 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
1799 for (const gl::LinkedUniform &glUniform : mData.getUniforms())
1800 {
1801 if (!glUniform.isInDefaultBlock())
1802 continue;
1803
1804 auto mapEntry = uniformMap.find(glUniform.name);
1805 ASSERT(mapEntry != uniformMap.end());
1806 mD3DUniforms.push_back(mapEntry->second);
1807 }
1808
Jamie Madill62d31cb2015-09-11 13:25:51 -04001809 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001810 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001811}
1812
Jamie Madill91445bc2015-09-23 16:47:53 -04001813void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001814 const sh::Uniform &uniform,
1815 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001816{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001817 if (uniform.isBuiltIn())
Jamie Madillfb536032015-09-11 13:19:49 -04001818 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001819 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001820 return;
1821 }
1822
Jamie Madill91445bc2015-09-23 16:47:53 -04001823 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1824
1825 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1826 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001827 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001828 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001829
Jamie Madill91445bc2015-09-23 16:47:53 -04001830 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001831}
1832
Jamie Madill62d31cb2015-09-11 13:25:51 -04001833D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1834{
1835 for (D3DUniform *d3dUniform : mD3DUniforms)
1836 {
1837 if (d3dUniform->name == name)
1838 {
1839 return d3dUniform;
1840 }
1841 }
1842
1843 return nullptr;
1844}
1845
Jamie Madill91445bc2015-09-23 16:47:53 -04001846void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001847 const sh::ShaderVariable &uniform,
1848 const std::string &fullName,
1849 sh::HLSLBlockEncoder *encoder,
1850 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001851{
1852 if (uniform.isStruct())
1853 {
1854 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1855 {
1856 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1857
Jamie Madill55def582015-05-04 11:24:57 -04001858 if (encoder)
1859 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001860
1861 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1862 {
Jamie Madill334d6152015-10-22 14:00:28 -04001863 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001864 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1865
Jamie Madill91445bc2015-09-23 16:47:53 -04001866 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001867 }
1868
Jamie Madill55def582015-05-04 11:24:57 -04001869 if (encoder)
1870 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001871 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001872 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001873 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001874
1875 // Not a struct. Arrays are treated as aggregate types.
1876 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001877 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001878 encoder->enterAggregateType();
1879 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001880
Jamie Madill62d31cb2015-09-11 13:25:51 -04001881 // Advance the uniform offset, to track registers allocation for structs
1882 sh::BlockMemberInfo blockInfo =
1883 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1884 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001885
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001886 auto uniformMapEntry = uniformMap->find(fullName);
1887 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001888
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001889 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001890 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001891 d3dUniform = uniformMapEntry->second;
1892 }
1893 else
1894 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001895 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001896 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001897 }
1898
1899 if (encoder)
1900 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001901 d3dUniform->registerElement =
1902 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001903 unsigned int reg =
1904 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001905 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001906 {
1907 d3dUniform->psRegisterIndex = reg;
1908 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001909 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001910 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001911 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001912 d3dUniform->vsRegisterIndex = reg;
1913 }
Jamie Madillfb536032015-09-11 13:19:49 -04001914
1915 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001916 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001917 {
1918 encoder->exitAggregateType();
1919 }
1920 }
1921}
1922
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001923template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001924void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001925{
Jamie Madill334d6152015-10-22 14:00:28 -04001926 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001927 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1928
Jamie Madill62d31cb2015-09-11 13:25:51 -04001929 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001930
Jamie Madill62d31cb2015-09-11 13:25:51 -04001931 unsigned int elementCount = targetUniform->elementCount();
1932 unsigned int arrayElement = mData.getUniformLocations()[location].element;
1933 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001934
1935 if (targetUniform->type == targetUniformType)
1936 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001937 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001938
Jamie Madill62d31cb2015-09-11 13:25:51 -04001939 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001940 {
Jamie Madill334d6152015-10-22 14:00:28 -04001941 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001942 const T *source = v + (i * components);
1943
1944 for (int c = 0; c < components; c++)
1945 {
1946 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1947 }
1948 for (int c = components; c < 4; c++)
1949 {
1950 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1951 }
1952 }
1953 }
1954 else if (targetUniform->type == targetBoolType)
1955 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001956 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001957
Jamie Madill62d31cb2015-09-11 13:25:51 -04001958 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001959 {
Jamie Madill334d6152015-10-22 14:00:28 -04001960 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001961 const T *source = v + (i * components);
1962
1963 for (int c = 0; c < components; c++)
1964 {
Jamie Madill334d6152015-10-22 14:00:28 -04001965 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1966 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001967 }
1968 for (int c = components; c < 4; c++)
1969 {
1970 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1971 }
1972 }
1973 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001974 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001975 {
1976 ASSERT(targetUniformType == GL_INT);
1977
Jamie Madill62d31cb2015-09-11 13:25:51 -04001978 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001979
1980 bool wasDirty = targetUniform->dirty;
1981
Jamie Madill62d31cb2015-09-11 13:25:51 -04001982 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001983 {
Jamie Madill334d6152015-10-22 14:00:28 -04001984 GLint *dest = target + (i * 4);
1985 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986
1987 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1988 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1989 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1990 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1991 }
1992
1993 if (!wasDirty && targetUniform->dirty)
1994 {
1995 mDirtySamplerMapping = true;
1996 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001997 }
Jamie Madill334d6152015-10-22 14:00:28 -04001998 else
1999 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002000}
2001
2002template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002003void ProgramD3D::setUniformMatrixfv(GLint location,
2004 GLsizei countIn,
2005 GLboolean transpose,
2006 const GLfloat *value,
2007 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002008{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002009 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002010
Jamie Madill62d31cb2015-09-11 13:25:51 -04002011 unsigned int elementCount = targetUniform->elementCount();
2012 unsigned int arrayElement = mData.getUniformLocations()[location].element;
2013 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002014
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002015 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002016 GLfloat *target =
2017 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018
Jamie Madill62d31cb2015-09-11 13:25:51 -04002019 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002020 {
2021 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2022 if (transpose == GL_FALSE)
2023 {
Jamie Madill334d6152015-10-22 14:00:28 -04002024 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2025 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002026 }
2027 else
2028 {
Jamie Madill334d6152015-10-22 14:00:28 -04002029 targetUniform->dirty =
2030 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002031 }
2032 target += targetMatrixStride;
2033 value += cols * rows;
2034 }
2035}
2036
Jamie Madill4a3c2342015-10-08 12:58:45 -04002037size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002038{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002039 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002040
Jamie Madill62d31cb2015-09-11 13:25:51 -04002041 // define member uniforms
2042 sh::Std140BlockEncoder std140Encoder;
2043 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2044 sh::BlockLayoutEncoder *encoder = nullptr;
2045
2046 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002047 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002048 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002049 }
2050 else
2051 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002052 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002053 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002054
2055 GetUniformBlockInfo(interfaceBlock.fields, "", encoder, interfaceBlock.isRowMajorLayout,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002056 &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002057
2058 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002059}
2060
Jamie Madill62d31cb2015-09-11 13:25:51 -04002061void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002062{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002063 for (const D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002064 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002065 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002066 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002067 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002068 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002069 }
2070}
2071
Jamie Madill62d31cb2015-09-11 13:25:51 -04002072void ProgramD3D::assignSamplerRegisters(const D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002073{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002074 ASSERT(d3dUniform->isSampler());
2075 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX ||
2076 d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madillfb536032015-09-11 13:19:49 -04002077
Jamie Madill62d31cb2015-09-11 13:25:51 -04002078 if (d3dUniform->vsRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002079 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002080 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2081 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002082 }
2083
Jamie Madill62d31cb2015-09-11 13:25:51 -04002084 if (d3dUniform->psRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002085 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002086 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2087 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002088 }
2089}
2090
Jamie Madill62d31cb2015-09-11 13:25:51 -04002091// static
2092void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002093 GLenum samplerType,
2094 unsigned int samplerCount,
2095 std::vector<Sampler> &outSamplers,
2096 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002097{
2098 unsigned int samplerIndex = startSamplerIndex;
2099
2100 do
2101 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002102 ASSERT(samplerIndex < outSamplers.size());
2103 Sampler *sampler = &outSamplers[samplerIndex];
2104 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002105 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002106 sampler->logicalTextureUnit = 0;
2107 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002108 samplerIndex++;
2109 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002110}
2111
Brandon Jonesc9610c52014-08-25 17:02:59 -07002112void ProgramD3D::reset()
2113{
Brandon Joneseb994362014-09-24 10:27:28 -07002114 SafeDeleteContainer(mVertexExecutables);
2115 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002116
2117 for (auto &element : mGeometryExecutables)
2118 {
2119 SafeDelete(element);
2120 }
Brandon Joneseb994362014-09-24 10:27:28 -07002121
Brandon Jones22502d52014-08-29 16:58:36 -07002122 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002123 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002124
2125 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002126 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002127 mUsesFragDepth = false;
2128 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002129 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002130 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002131
Jamie Madill62d31cb2015-09-11 13:25:51 -04002132 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002133 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002134
Brandon Jonesc9610c52014-08-25 17:02:59 -07002135 SafeDelete(mVertexUniformStorage);
2136 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002137
2138 mSamplersPS.clear();
2139 mSamplersVS.clear();
2140
2141 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002142 mUsedPixelSamplerRange = 0;
2143 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002144
Jamie Madill63805b42015-08-25 13:17:39 -04002145 std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1);
Jamie Madill437d2662014-12-05 14:23:35 -05002146 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002147
Jamie Madill9fc36822015-11-18 13:08:07 -05002148 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002149
2150 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002151}
2152
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153unsigned int ProgramD3D::getSerial() const
2154{
2155 return mSerial;
2156}
2157
2158unsigned int ProgramD3D::issueSerial()
2159{
2160 return mCurrentSerial++;
2161}
2162
Jamie Madill63805b42015-08-25 13:17:39 -04002163void ProgramD3D::initSemanticIndex()
2164{
2165 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2166 ASSERT(vertexShader != nullptr);
2167
2168 // Init semantic index
2169 for (const sh::Attribute &attribute : mData.getAttributes())
2170 {
2171 int attributeIndex = attribute.location;
2172 int index = vertexShader->getSemanticIndex(attribute.name);
2173 int regs = gl::VariableRegisterCount(attribute.type);
2174
2175 for (int reg = 0; reg < regs; ++reg)
2176 {
2177 mSemanticIndexes[attributeIndex + reg] = index + reg;
2178 }
2179 }
2180
2181 initAttributesByLayout();
2182}
2183
Jamie Madill437d2662014-12-05 14:23:35 -05002184void ProgramD3D::initAttributesByLayout()
2185{
2186 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2187 {
2188 mAttributesByLayout[i] = i;
2189 }
2190
Jamie Madill63805b42015-08-25 13:17:39 -04002191 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS],
2192 AttributeSorter(mSemanticIndexes));
Jamie Madill437d2662014-12-05 14:23:35 -05002193}
2194
Jamie Madill334d6152015-10-22 14:00:28 -04002195void ProgramD3D::sortAttributesByLayout(
2196 const std::vector<TranslatedAttribute> &unsortedAttributes,
2197 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2198 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002199{
Jamie Madill476682e2015-06-30 10:04:29 -04002200 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002201 {
Jamie Madill334d6152015-10-22 14:00:28 -04002202 int oldIndex = mAttributesByLayout[attribIndex];
Jamie Madill63805b42015-08-25 13:17:39 -04002203 sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex];
Jamie Madill334d6152015-10-22 14:00:28 -04002204 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002205 }
2206}
2207
Jamie Madill63805b42015-08-25 13:17:39 -04002208void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002209{
Jamie Madillbd136f92015-08-10 14:51:37 -04002210 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002211 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002212
Dian Xianga4928832015-09-15 10:11:17 -07002213 for (unsigned int attributeIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002214 {
Jamie Madill63805b42015-08-25 13:17:39 -04002215 int semanticIndex = mSemanticIndexes[attributeIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002216
2217 if (semanticIndex != -1)
2218 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002219 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2220 {
2221 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2222 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002223 mCachedInputLayout[semanticIndex] =
2224 GetVertexFormatType(vertexAttributes[attributeIndex],
2225 state.getVertexAttribCurrentValue(attributeIndex).Type);
2226 }
2227 }
2228}
2229
Jamie Madill9fc36822015-11-18 13:08:07 -05002230void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002231{
Jamie Madill9fc36822015-11-18 13:08:07 -05002232 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2233
2234 const std::string &varyingSemantic =
2235 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2236
Jamie Madillccdf74b2015-08-18 10:46:12 -04002237 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002238 mStreamOutVaryings.clear();
2239
2240 const auto &tfVaryingNames = mData.getTransformFeedbackVaryingNames();
2241 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2242 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002243 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002244 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2245 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002246 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002247 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002248 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002249 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2250 builtins.glPosition.index, 4, outputSlot));
2251 }
2252 }
2253 else if (tfVaryingName == "gl_FragCoord")
2254 {
2255 if (builtins.glFragCoord.enabled)
2256 {
2257 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2258 builtins.glFragCoord.index, 4, outputSlot));
2259 }
2260 }
2261 else if (tfVaryingName == "gl_PointSize")
2262 {
2263 if (builtins.glPointSize.enabled)
2264 {
2265 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2266 }
2267 }
2268 else
2269 {
2270 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2271 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002272 const auto &varying = *registerInfo.packedVarying->varying;
2273 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002274 int componentCount = gl::VariableColumnCount(transposedType);
2275 ASSERT(!varying.isBuiltIn());
2276
Jamie Madill55c25d02015-11-18 13:08:08 -05002277 // Transform feedback for varying structs is underspecified.
2278 // See Khronos bug 9856.
2279 // TODO(jmadill): Figure out how to be spec-compliant here.
2280 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2281 continue;
2282
Jamie Madill9fc36822015-11-18 13:08:07 -05002283 // There can be more than one register assigned to a particular varying, and each
2284 // register needs its own stream out entry.
2285 if (tfVaryingName == varying.name)
2286 {
2287 mStreamOutVaryings.push_back(D3DVarying(
2288 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2289 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002290 }
2291 }
2292 }
2293}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002294
2295D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2296{
2297 return mD3DUniforms[mData.getUniformLocations()[location].index];
2298}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002299
2300bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2301{
2302 std::string baseName = blockName;
2303 gl::ParseAndStripArrayIndex(&baseName);
2304
2305 auto sizeIter = mBlockDataSizes.find(baseName);
2306 if (sizeIter == mBlockDataSizes.end())
2307 {
2308 *sizeOut = 0;
2309 return false;
2310 }
2311
2312 *sizeOut = sizeIter->second;
2313 return true;
2314}
2315
2316bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2317 sh::BlockMemberInfo *memberInfoOut) const
2318{
2319 auto infoIter = mBlockInfo.find(memberUniformName);
2320 if (infoIter == mBlockInfo.end())
2321 {
2322 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2323 return false;
2324 }
2325
2326 *memberInfoOut = infoIter->second;
2327 return true;
2328}
Brandon Jonesc9610c52014-08-25 17:02:59 -07002329}