blob: bde34093a72bf1e14c859426d1604552af5d426f [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
Dian Xianga4928832015-09-15 10:11:17 -070011#include "common/BitSetIterator.h"
Jamie Madill437d2662014-12-05 14:23:35 -050012#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Framebuffer.h"
14#include "libANGLE/FramebufferAttachment.h"
15#include "libANGLE/Program.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040016#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000017#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050018#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050019#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/renderer/d3d/RendererD3D.h"
21#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050022#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill65345da2015-11-13 11:25:23 -050023#include "libANGLE/renderer/d3d/VaryingPacking.h"
Jamie Madill437d2662014-12-05 14:23:35 -050024#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050025
Brandon Jonesc9610c52014-08-25 17:02:59 -070026namespace rx
27{
28
Brandon Joneseb994362014-09-24 10:27:28 -070029namespace
30{
31
Jamie Madillf8dd7b12015-08-05 13:50:08 -040032gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070033{
Jamie Madillbd136f92015-08-10 14:51:37 -040034 gl::InputLayout defaultLayout;
35 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070036 {
Brandon Joneseb994362014-09-24 10:27:28 -070037 if (shaderAttr.type != GL_NONE)
38 {
39 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
40
Jamie Madilld3dfda22015-07-06 08:28:49 -040041 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040042 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070043 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040044 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040045 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040046 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040047 gl::VertexFormatType defaultType =
48 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070049
Jamie Madillbd136f92015-08-10 14:51:37 -040050 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070051 }
52 }
53 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040054
55 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070056}
57
Jamie Madill334d6152015-10-22 14:00:28 -040058std::vector<GLenum> GetDefaultOutputLayoutFromShader(
59 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070060{
Jamie Madillb4463142014-12-19 14:56:54 -050061 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070062
Jamie Madillb4463142014-12-19 14:56:54 -050063 if (!shaderOutputVars.empty())
64 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070065 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
66 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050067 }
Brandon Joneseb994362014-09-24 10:27:28 -070068
69 return defaultPixelOutput;
70}
71
Brandon Jones1a8a7e32014-10-01 12:49:30 -070072bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
73{
74 return var.isRowMajorLayout;
75}
76
77bool IsRowMajorLayout(const sh::ShaderVariable &var)
78{
79 return false;
80}
81
Jamie Madill437d2662014-12-05 14:23:35 -050082struct AttributeSorter
83{
Jamie Madill63805b42015-08-25 13:17:39 -040084 AttributeSorter(const ProgramD3D::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -050085 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -050086 {
87 }
88
89 bool operator()(int a, int b)
90 {
Jamie Madill80d934b2015-02-19 10:16:12 -050091 int indexA = (*originalIndices)[a];
92 int indexB = (*originalIndices)[b];
93
Jamie Madill334d6152015-10-22 14:00:28 -040094 if (indexA == -1)
95 return false;
96 if (indexB == -1)
97 return true;
Jamie Madill80d934b2015-02-19 10:16:12 -050098 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -050099 }
100
Jamie Madill63805b42015-08-25 13:17:39 -0400101 const ProgramD3D::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500102};
103
Jamie Madill9fc36822015-11-18 13:08:07 -0500104// true if varying x has a higher priority in packing than y
105bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
106{
Jamie Madill55c25d02015-11-18 13:08:08 -0500107 return gl::CompareShaderVar(*x.varying, *y.varying);
Jamie Madill9fc36822015-11-18 13:08:07 -0500108}
109
Jamie Madillca03b352015-09-02 12:38:13 -0400110std::vector<PackedVarying> MergeVaryings(const gl::Shader &vertexShader,
111 const gl::Shader &fragmentShader,
112 const std::vector<std::string> &tfVaryings)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400113{
Jamie Madillca03b352015-09-02 12:38:13 -0400114 std::vector<PackedVarying> packedVaryings;
115
116 for (const sh::Varying &output : vertexShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400117 {
Jamie Madillca03b352015-09-02 12:38:13 -0400118 bool packed = false;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400119
120 // Built-in varyings obey special rules
Jamie Madillca03b352015-09-02 12:38:13 -0400121 if (output.isBuiltIn())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400122 {
123 continue;
124 }
125
Jamie Madillca03b352015-09-02 12:38:13 -0400126 for (const sh::Varying &input : fragmentShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400127 {
Jamie Madillca03b352015-09-02 12:38:13 -0400128 if (output.name == input.name)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400129 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500130 if (output.isStruct())
131 {
132 ASSERT(!output.isArray());
133 for (const auto &field : output.fields)
134 {
135 ASSERT(!field.isStruct() && !field.isArray());
136 packedVaryings.push_back(
137 PackedVarying(field, input.interpolation, input.name));
138 }
139 }
140 else
141 {
142 packedVaryings.push_back(PackedVarying(input, input.interpolation));
143 }
Jamie Madillca03b352015-09-02 12:38:13 -0400144 packed = true;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400145 break;
146 }
147 }
148
Jamie Madillca03b352015-09-02 12:38:13 -0400149 // Keep Transform FB varyings in the merged list always.
150 if (!packed)
151 {
152 for (const std::string &tfVarying : tfVaryings)
153 {
154 if (tfVarying == output.name)
155 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500156 // Transform feedback for varying structs is underspecified.
157 // See Khronos bug 9856.
158 // TODO(jmadill): Figure out how to be spec-compliant here.
159 if (!output.isStruct())
160 {
161 packedVaryings.push_back(PackedVarying(output, output.interpolation));
162 packedVaryings.back().vertexOnly = true;
163 }
Jamie Madillca03b352015-09-02 12:38:13 -0400164 break;
165 }
166 }
167 }
Jamie Madillada9ecc2015-08-17 12:53:37 -0400168 }
169
Jamie Madill9fc36822015-11-18 13:08:07 -0500170 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
171
Jamie Madillca03b352015-09-02 12:38:13 -0400172 return packedVaryings;
Brandon Joneseb994362014-09-24 10:27:28 -0700173}
174
Jamie Madill62d31cb2015-09-11 13:25:51 -0400175template <typename VarT>
176void GetUniformBlockInfo(const std::vector<VarT> &fields,
177 const std::string &prefix,
178 sh::BlockLayoutEncoder *encoder,
179 bool inRowMajorLayout,
180 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
181{
182 for (const VarT &field : fields)
183 {
184 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
185
186 if (field.isStruct())
187 {
188 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
189
190 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
191 {
192 encoder->enterAggregateType();
193
194 const std::string uniformElementName =
195 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
196 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
197 blockInfoOut);
198
199 encoder->exitAggregateType();
200 }
201 }
202 else
203 {
204 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
205 (*blockInfoOut)[fieldName] =
206 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
207 }
208 }
209}
210
Jamie Madill334d6152015-10-22 14:00:28 -0400211template <typename T>
Jacek Caban2302e692015-12-01 12:44:43 +0100212static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
213{
214 ASSERT(dest != NULL);
215 ASSERT(dirtyFlag != NULL);
216
217 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
218 *dest = source;
219}
220
221template <typename T>
Jamie Madill334d6152015-10-22 14:00:28 -0400222bool TransposeMatrix(T *target,
223 const GLfloat *value,
224 int targetWidth,
225 int targetHeight,
226 int srcWidth,
227 int srcHeight)
228{
229 bool dirty = false;
230 int copyWidth = std::min(targetHeight, srcWidth);
231 int copyHeight = std::min(targetWidth, srcHeight);
232
233 for (int x = 0; x < copyWidth; x++)
234 {
235 for (int y = 0; y < copyHeight; y++)
236 {
237 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]),
238 &dirty);
239 }
240 }
241 // clear unfilled right side
242 for (int y = 0; y < copyWidth; y++)
243 {
244 for (int x = copyHeight; x < targetWidth; x++)
245 {
246 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
247 }
248 }
249 // clear unfilled bottom.
250 for (int y = copyWidth; y < targetHeight; y++)
251 {
252 for (int x = 0; x < targetWidth; x++)
253 {
254 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
255 }
256 }
257
258 return dirty;
259}
260
261template <typename T>
262bool ExpandMatrix(T *target,
263 const GLfloat *value,
264 int targetWidth,
265 int targetHeight,
266 int srcWidth,
267 int srcHeight)
268{
269 bool dirty = false;
270 int copyWidth = std::min(targetWidth, srcWidth);
271 int copyHeight = std::min(targetHeight, srcHeight);
272
273 for (int y = 0; y < copyHeight; y++)
274 {
275 for (int x = 0; x < copyWidth; x++)
276 {
277 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]),
278 &dirty);
279 }
280 }
281 // clear unfilled right side
282 for (int y = 0; y < copyHeight; y++)
283 {
284 for (int x = copyWidth; x < targetWidth; x++)
285 {
286 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
287 }
288 }
289 // clear unfilled bottom.
290 for (int y = copyHeight; y < targetHeight; y++)
291 {
292 for (int x = 0; x < targetWidth; x++)
293 {
294 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
295 }
296 }
297
298 return dirty;
299}
300
Jamie Madill4e31ad52015-10-29 10:32:57 -0400301gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
302{
303 switch (drawMode)
304 {
305 // Uses the point sprite geometry shader.
306 case GL_POINTS:
307 return gl::PRIMITIVE_POINTS;
308
309 // All line drawing uses the same geometry shader.
310 case GL_LINES:
311 case GL_LINE_STRIP:
312 case GL_LINE_LOOP:
313 return gl::PRIMITIVE_LINES;
314
315 // The triangle fan primitive is emulated with strips in D3D11.
316 case GL_TRIANGLES:
317 case GL_TRIANGLE_FAN:
318 return gl::PRIMITIVE_TRIANGLES;
319
320 // Special case for triangle strips.
321 case GL_TRIANGLE_STRIP:
322 return gl::PRIMITIVE_TRIANGLE_STRIP;
323
324 default:
325 UNREACHABLE();
326 return gl::PRIMITIVE_TYPE_MAX;
327 }
328}
329
Jamie Madillada9ecc2015-08-17 12:53:37 -0400330} // anonymous namespace
331
Jamie Madill28afae52015-11-09 15:07:57 -0500332// D3DUniform Implementation
333
Jamie Madill62d31cb2015-09-11 13:25:51 -0400334D3DUniform::D3DUniform(GLenum typeIn,
335 const std::string &nameIn,
336 unsigned int arraySizeIn,
337 bool defaultBlock)
338 : type(typeIn),
339 name(nameIn),
340 arraySize(arraySizeIn),
341 data(nullptr),
342 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400343 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400344 psRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400345 registerCount(0),
346 registerElement(0)
347{
348 // We use data storage for default block uniforms to cache values that are sent to D3D during
349 // rendering
350 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
351 if (defaultBlock)
352 {
353 size_t bytes = gl::VariableInternalSize(type) * elementCount();
354 data = new uint8_t[bytes];
355 memset(data, 0, bytes);
356
357 // TODO(jmadill): is this correct with non-square matrices?
358 registerCount = gl::VariableRowCount(type) * elementCount();
359 }
360}
361
362D3DUniform::~D3DUniform()
363{
364 SafeDeleteArray(data);
365}
366
367bool D3DUniform::isSampler() const
368{
369 return gl::IsSamplerType(type);
370}
371
372bool D3DUniform::isReferencedByVertexShader() const
373{
374 return vsRegisterIndex != GL_INVALID_INDEX;
375}
376
377bool D3DUniform::isReferencedByFragmentShader() const
378{
379 return psRegisterIndex != GL_INVALID_INDEX;
380}
381
Jamie Madill28afae52015-11-09 15:07:57 -0500382// D3DVarying Implementation
383
Jamie Madill9fc36822015-11-18 13:08:07 -0500384D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500385{
386}
387
Jamie Madill9fc36822015-11-18 13:08:07 -0500388D3DVarying::D3DVarying(const std::string &semanticNameIn,
389 unsigned int semanticIndexIn,
390 unsigned int componentCountIn,
391 unsigned int outputSlotIn)
392 : semanticName(semanticNameIn),
393 semanticIndex(semanticIndexIn),
394 componentCount(componentCountIn),
395 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500396{
397}
398
Jamie Madille39a3f02015-11-17 20:42:15 -0500399// ProgramD3DMetadata Implementation
400
401ProgramD3DMetadata::ProgramD3DMetadata(int rendererMajorShaderModel,
402 const std::string &shaderModelSuffix,
403 bool usesInstancedPointSpriteEmulation,
404 const ShaderD3D *vertexShader,
405 const ShaderD3D *fragmentShader)
406 : mRendererMajorShaderModel(rendererMajorShaderModel),
407 mShaderModelSuffix(shaderModelSuffix),
408 mUsesInstancedPointSpriteEmulation(usesInstancedPointSpriteEmulation),
409 mVertexShader(vertexShader),
410 mFragmentShader(fragmentShader)
411{
412}
413
414int ProgramD3DMetadata::getRendererMajorShaderModel() const
415{
416 return mRendererMajorShaderModel;
417}
418
419bool ProgramD3DMetadata::usesBroadcast(const gl::Data &data) const
420{
421 return (mFragmentShader->usesFragColor() && data.clientVersion < 3);
422}
423
424bool ProgramD3DMetadata::usesFragDepth(const gl::Program::Data &programData) const
425{
Jamie Madill63286672015-11-24 13:00:08 -0500426 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500427}
428
429bool ProgramD3DMetadata::usesPointCoord() const
430{
431 return mFragmentShader->usesPointCoord();
432}
433
434bool ProgramD3DMetadata::usesFragCoord() const
435{
436 return mFragmentShader->usesFragCoord();
437}
438
439bool ProgramD3DMetadata::usesPointSize() const
440{
441 return mVertexShader->usesPointSize();
442}
443
444bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
445{
446 return !usesPointSize() && usesPointCoord() && mRendererMajorShaderModel >= 4;
447}
448
449bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
450{
451 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader
452 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
453 // GeometryShader PointSprite emulation does not require this additional entry because the
454 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the
455 // PS_INPUT of the generated pixel shader. The Geometry Shader point sprite implementation needs
456 // gl_PointSize to be in VS_OUTPUT and GS_INPUT. Instanced point sprites doesn't need
457 // gl_PointSize in VS_OUTPUT.
458 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
459 usesInsertedPointCoordValue();
460}
461
462bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
463{
464 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
465 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
466 // the vertex shader in this case. This saves us 1 output vector.
467 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
468}
469
470bool ProgramD3DMetadata::usesSystemValuePointSize() const
471{
472 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
473}
474
475bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
476{
477 return mFragmentShader->usesMultipleRenderTargets();
478}
479
480GLint ProgramD3DMetadata::getMajorShaderVersion() const
481{
482 return mVertexShader->getData().getShaderVersion();
483}
484
485const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
486{
487 return mFragmentShader;
488}
489
Jamie Madill28afae52015-11-09 15:07:57 -0500490// ProgramD3D Implementation
491
Jamie Madilld3dfda22015-07-06 08:28:49 -0400492ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
493 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500494 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400495 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700496{
Brandon Joneseb994362014-09-24 10:27:28 -0700497}
498
499ProgramD3D::VertexExecutable::~VertexExecutable()
500{
501 SafeDelete(mShaderExecutable);
502}
503
Jamie Madilld3dfda22015-07-06 08:28:49 -0400504// static
505void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
506 const gl::InputLayout &inputLayout,
507 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700508{
Jamie Madillbd136f92015-08-10 14:51:37 -0400509 signatureOut->resize(inputLayout.size());
Jamie Madilld3dfda22015-07-06 08:28:49 -0400510
511 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700512 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400513 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbd136f92015-08-10 14:51:37 -0400514 bool converted = false;
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400515 if (vertexFormatType != gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700516 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400517 VertexConversionType conversionType =
518 renderer->getVertexConversionType(vertexFormatType);
Jamie Madillbd136f92015-08-10 14:51:37 -0400519 converted = ((conversionType & VERTEX_CONVERT_GPU) != 0);
Brandon Joneseb994362014-09-24 10:27:28 -0700520 }
Jamie Madillbd136f92015-08-10 14:51:37 -0400521
522 (*signatureOut)[index] = converted;
Brandon Joneseb994362014-09-24 10:27:28 -0700523 }
Brandon Joneseb994362014-09-24 10:27:28 -0700524}
525
Jamie Madilld3dfda22015-07-06 08:28:49 -0400526bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
527{
Jamie Madillbd136f92015-08-10 14:51:37 -0400528 size_t limit = std::max(mSignature.size(), signature.size());
529 for (size_t index = 0; index < limit; ++index)
530 {
531 // treat undefined indexes as 'not converted'
532 bool a = index < signature.size() ? signature[index] : false;
533 bool b = index < mSignature.size() ? mSignature[index] : false;
534 if (a != b)
535 return false;
536 }
537
538 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400539}
540
541ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
542 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400543 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700544{
545}
546
547ProgramD3D::PixelExecutable::~PixelExecutable()
548{
549 SafeDelete(mShaderExecutable);
550}
551
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700552ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
553{
554}
555
Geoff Lang7dd2e102014-11-10 15:19:26 -0500556unsigned int ProgramD3D::mCurrentSerial = 1;
557
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400558ProgramD3D::ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer)
559 : ProgramImpl(data),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700560 mRenderer(renderer),
561 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400562 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700563 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400564 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700565 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700566 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700567 mUsedVertexSamplerRange(0),
568 mUsedPixelSamplerRange(0),
569 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500570 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700571{
Brandon Joneseb994362014-09-24 10:27:28 -0700572 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700573}
574
575ProgramD3D::~ProgramD3D()
576{
577 reset();
578 SafeDelete(mDynamicHLSL);
579}
580
Brandon Jones44151a92014-09-10 11:32:25 -0700581bool ProgramD3D::usesPointSpriteEmulation() const
582{
583 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
584}
585
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400586bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700587{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400588 if (drawMode != GL_POINTS)
589 {
590 return mUsesFlatInterpolation;
591 }
592
Cooper Partine6664f02015-01-09 16:22:24 -0800593 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
594}
595
596bool ProgramD3D::usesInstancedPointSpriteEmulation() const
597{
598 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700599}
600
Jamie Madill334d6152015-10-22 14:00:28 -0400601GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
602 unsigned int samplerIndex,
603 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700604{
605 GLint logicalTextureUnit = -1;
606
607 switch (type)
608 {
Jamie Madill334d6152015-10-22 14:00:28 -0400609 case gl::SAMPLER_PIXEL:
610 ASSERT(samplerIndex < caps.maxTextureImageUnits);
611 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
612 {
613 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
614 }
615 break;
616 case gl::SAMPLER_VERTEX:
617 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
618 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
619 {
620 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
621 }
622 break;
623 default:
624 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700625 }
626
Jamie Madill334d6152015-10-22 14:00:28 -0400627 if (logicalTextureUnit >= 0 &&
628 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700629 {
630 return logicalTextureUnit;
631 }
632
633 return -1;
634}
635
636// Returns the texture type for a given Direct3D 9 sampler type and
637// index (0-15 for the pixel shader and 0-3 for the vertex shader).
638GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
639{
640 switch (type)
641 {
Jamie Madill334d6152015-10-22 14:00:28 -0400642 case gl::SAMPLER_PIXEL:
643 ASSERT(samplerIndex < mSamplersPS.size());
644 ASSERT(mSamplersPS[samplerIndex].active);
645 return mSamplersPS[samplerIndex].textureType;
646 case gl::SAMPLER_VERTEX:
647 ASSERT(samplerIndex < mSamplersVS.size());
648 ASSERT(mSamplersVS[samplerIndex].active);
649 return mSamplersVS[samplerIndex].textureType;
650 default:
651 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700652 }
653
654 return GL_TEXTURE_2D;
655}
656
657GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
658{
659 switch (type)
660 {
Jamie Madill334d6152015-10-22 14:00:28 -0400661 case gl::SAMPLER_PIXEL:
662 return mUsedPixelSamplerRange;
663 case gl::SAMPLER_VERTEX:
664 return mUsedVertexSamplerRange;
665 default:
666 UNREACHABLE();
667 return 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700668 }
669}
670
671void ProgramD3D::updateSamplerMapping()
672{
673 if (!mDirtySamplerMapping)
674 {
675 return;
676 }
677
678 mDirtySamplerMapping = false;
679
680 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400681 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700682 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400683 if (!d3dUniform->dirty)
684 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700685
Jamie Madill62d31cb2015-09-11 13:25:51 -0400686 if (!d3dUniform->isSampler())
687 continue;
688
689 int count = d3dUniform->elementCount();
690 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
691
692 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700693 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400694 unsigned int firstIndex = d3dUniform->psRegisterIndex;
695
696 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700697 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400698 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700699
Jamie Madill62d31cb2015-09-11 13:25:51 -0400700 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700701 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400702 ASSERT(mSamplersPS[samplerIndex].active);
703 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700704 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400705 }
706 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700707
Jamie Madill62d31cb2015-09-11 13:25:51 -0400708 if (d3dUniform->isReferencedByVertexShader())
709 {
710 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
711
712 for (int i = 0; i < count; i++)
713 {
714 unsigned int samplerIndex = firstIndex + i;
715
716 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700717 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400718 ASSERT(mSamplersVS[samplerIndex].active);
719 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700720 }
721 }
722 }
723 }
724}
725
Geoff Lang7dd2e102014-11-10 15:19:26 -0500726LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700727{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400728 reset();
729
Jamie Madill334d6152015-10-22 14:00:28 -0400730 DeviceIdentifier binaryDeviceIdentifier = {0};
731 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
732 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700733
734 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
735 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
736 {
737 infoLog << "Invalid program binary, device configuration has changed.";
738 return LinkResult(false, gl::Error(GL_NO_ERROR));
739 }
740
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500741 int compileFlags = stream->readInt<int>();
742 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
743 {
Jamie Madillf6113162015-05-07 11:49:21 -0400744 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500745 return LinkResult(false, gl::Error(GL_NO_ERROR));
746 }
747
Jamie Madill63805b42015-08-25 13:17:39 -0400748 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
749 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
750 {
751 stream->readInt(&mSemanticIndexes[i]);
752 }
753
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700754 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
755 for (unsigned int i = 0; i < psSamplerCount; ++i)
756 {
757 Sampler sampler;
758 stream->readBool(&sampler.active);
759 stream->readInt(&sampler.logicalTextureUnit);
760 stream->readInt(&sampler.textureType);
761 mSamplersPS.push_back(sampler);
762 }
763 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
764 for (unsigned int i = 0; i < vsSamplerCount; ++i)
765 {
766 Sampler sampler;
767 stream->readBool(&sampler.active);
768 stream->readInt(&sampler.logicalTextureUnit);
769 stream->readInt(&sampler.textureType);
770 mSamplersVS.push_back(sampler);
771 }
772
773 stream->readInt(&mUsedVertexSamplerRange);
774 stream->readInt(&mUsedPixelSamplerRange);
775
776 const unsigned int uniformCount = stream->readInt<unsigned int>();
777 if (stream->error())
778 {
Jamie Madillf6113162015-05-07 11:49:21 -0400779 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500780 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700781 }
782
Jamie Madill62d31cb2015-09-11 13:25:51 -0400783 const auto &linkedUniforms = mData.getUniforms();
784 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700785 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
786 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400787 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700788
Jamie Madill62d31cb2015-09-11 13:25:51 -0400789 D3DUniform *d3dUniform =
790 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
791 linkedUniform.isInDefaultBlock());
792 stream->readInt(&d3dUniform->psRegisterIndex);
793 stream->readInt(&d3dUniform->vsRegisterIndex);
794 stream->readInt(&d3dUniform->registerCount);
795 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700796
Jamie Madill62d31cb2015-09-11 13:25:51 -0400797 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700798 }
799
Jamie Madill4a3c2342015-10-08 12:58:45 -0400800 const unsigned int blockCount = stream->readInt<unsigned int>();
801 if (stream->error())
802 {
803 infoLog << "Invalid program binary.";
804 return LinkResult(false, gl::Error(GL_NO_ERROR));
805 }
806
807 ASSERT(mD3DUniformBlocks.empty());
808 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
809 {
810 D3DUniformBlock uniformBlock;
811 stream->readInt(&uniformBlock.psRegisterIndex);
812 stream->readInt(&uniformBlock.vsRegisterIndex);
813 mD3DUniformBlocks.push_back(uniformBlock);
814 }
815
Jamie Madill9fc36822015-11-18 13:08:07 -0500816 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
817 mStreamOutVaryings.resize(streamOutVaryingCount);
818 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700819 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500820 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700821
Jamie Madill28afae52015-11-09 15:07:57 -0500822 stream->readString(&varying->semanticName);
823 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500824 stream->readInt(&varying->componentCount);
825 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700826 }
827
Brandon Jones22502d52014-08-29 16:58:36 -0700828 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400829 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
830 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700831 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400832 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
833 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700834 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700835 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400836 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700837
838 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
839 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400840 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
841 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700842 {
843 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
844 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
845 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
846 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
847 }
848
Jamie Madill4e31ad52015-10-29 10:32:57 -0400849 stream->readString(&mGeometryShaderPreamble);
850
Jamie Madill334d6152015-10-22 14:00:28 -0400851 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700852
853 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400854 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
855 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700856 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400857 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400858 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700859
Jamie Madilld3dfda22015-07-06 08:28:49 -0400860 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700861 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400862 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700863 }
864
Jamie Madill334d6152015-10-22 14:00:28 -0400865 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700866 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400867
Jamie Madillada9ecc2015-08-17 12:53:37 -0400868 ShaderExecutableD3D *shaderExecutable = nullptr;
869
870 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500871 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400872 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400873 if (error.isError())
874 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500875 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400876 }
877
Brandon Joneseb994362014-09-24 10:27:28 -0700878 if (!shaderExecutable)
879 {
Jamie Madillf6113162015-05-07 11:49:21 -0400880 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500881 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700882 }
883
884 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400885 VertexExecutable::Signature signature;
886 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700887
888 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400889 mVertexExecutables.push_back(
890 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700891
892 stream->skip(vertexShaderSize);
893 }
894
895 const size_t pixelShaderCount = stream->readInt<unsigned int>();
896 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
897 {
898 const size_t outputCount = stream->readInt<unsigned int>();
899 std::vector<GLenum> outputs(outputCount);
900 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
901 {
902 stream->readInt(&outputs[outputIndex]);
903 }
904
Jamie Madill334d6152015-10-22 14:00:28 -0400905 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700906 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400907 ShaderExecutableD3D *shaderExecutable = nullptr;
908
909 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500910 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400911 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400912 if (error.isError())
913 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500914 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400915 }
Brandon Joneseb994362014-09-24 10:27:28 -0700916
917 if (!shaderExecutable)
918 {
Jamie Madillf6113162015-05-07 11:49:21 -0400919 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500920 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700921 }
922
923 // add new binary
924 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
925
926 stream->skip(pixelShaderSize);
927 }
928
Jamie Madill4e31ad52015-10-29 10:32:57 -0400929 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
930 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700931 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400932 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
933 if (geometryShaderSize == 0)
934 {
935 mGeometryExecutables[geometryExeIndex] = nullptr;
936 continue;
937 }
938
Brandon Joneseb994362014-09-24 10:27:28 -0700939 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400940 bool splitAttribs = (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
941
Jamie Madill28afae52015-11-09 15:07:57 -0500942 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500943 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, mStreamOutVaryings,
944 splitAttribs, &mGeometryExecutables[geometryExeIndex]);
Geoff Langb543aff2014-09-30 14:52:54 -0400945 if (error.isError())
946 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500947 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400948 }
Brandon Joneseb994362014-09-24 10:27:28 -0700949
Jamie Madill4e31ad52015-10-29 10:32:57 -0400950 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700951 {
Jamie Madillf6113162015-05-07 11:49:21 -0400952 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500953 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700954 }
955 stream->skip(geometryShaderSize);
956 }
957
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700958 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500959 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700960
Geoff Lang7dd2e102014-11-10 15:19:26 -0500961 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700962}
963
Geoff Langb543aff2014-09-30 14:52:54 -0400964gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700965{
Austin Kinross137b1512015-06-17 16:14:53 -0700966 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400967 // When we load the binary again later, we can validate the device identifier before trying to
968 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700969 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400970 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
971 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700972
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500973 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
974
Jamie Madill63805b42015-08-25 13:17:39 -0400975 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
976 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
977 {
978 stream->writeInt(mSemanticIndexes[i]);
979 }
980
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700981 stream->writeInt(mSamplersPS.size());
982 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
983 {
984 stream->writeInt(mSamplersPS[i].active);
985 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
986 stream->writeInt(mSamplersPS[i].textureType);
987 }
988
989 stream->writeInt(mSamplersVS.size());
990 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
991 {
992 stream->writeInt(mSamplersVS[i].active);
993 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
994 stream->writeInt(mSamplersVS[i].textureType);
995 }
996
997 stream->writeInt(mUsedVertexSamplerRange);
998 stream->writeInt(mUsedPixelSamplerRange);
999
Jamie Madill62d31cb2015-09-11 13:25:51 -04001000 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001001 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001002 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001003 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001004 stream->writeInt(uniform->psRegisterIndex);
1005 stream->writeInt(uniform->vsRegisterIndex);
1006 stream->writeInt(uniform->registerCount);
1007 stream->writeInt(uniform->registerElement);
1008 }
1009
1010 stream->writeInt(mD3DUniformBlocks.size());
1011 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1012 {
1013 stream->writeInt(uniformBlock.psRegisterIndex);
1014 stream->writeInt(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001015 }
1016
Jamie Madill9fc36822015-11-18 13:08:07 -05001017 stream->writeInt(mStreamOutVaryings.size());
1018 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001019 {
Brandon Joneseb994362014-09-24 10:27:28 -07001020 stream->writeString(varying.semanticName);
1021 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001022 stream->writeInt(varying.componentCount);
1023 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001024 }
1025
Brandon Jones22502d52014-08-29 16:58:36 -07001026 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001027 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1028 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001029 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001030 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1031 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001032 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001033 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001034 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001035
Brandon Joneseb994362014-09-24 10:27:28 -07001036 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001037 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001038 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1039 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001040 {
Brandon Joneseb994362014-09-24 10:27:28 -07001041 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001042 stream->writeInt(variable.type);
1043 stream->writeString(variable.name);
1044 stream->writeString(variable.source);
1045 stream->writeInt(variable.outputIndex);
1046 }
1047
Jamie Madill4e31ad52015-10-29 10:32:57 -04001048 stream->writeString(mGeometryShaderPreamble);
1049
Brandon Joneseb994362014-09-24 10:27:28 -07001050 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001051 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1052 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001053 {
1054 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1055
Jamie Madilld3dfda22015-07-06 08:28:49 -04001056 const auto &inputLayout = vertexExecutable->inputs();
1057 stream->writeInt(inputLayout.size());
1058
1059 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001060 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001061 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -07001062 }
1063
1064 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1065 stream->writeInt(vertexShaderSize);
1066
1067 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1068 stream->writeBytes(vertexBlob, vertexShaderSize);
1069 }
1070
1071 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001072 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1073 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001074 {
1075 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1076
1077 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1078 stream->writeInt(outputs.size());
1079 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1080 {
1081 stream->writeInt(outputs[outputIndex]);
1082 }
1083
1084 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1085 stream->writeInt(pixelShaderSize);
1086
1087 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1088 stream->writeBytes(pixelBlob, pixelShaderSize);
1089 }
1090
Jamie Madill4e31ad52015-10-29 10:32:57 -04001091 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001092 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001093 if (geometryExe == nullptr)
1094 {
1095 stream->writeInt(0);
1096 continue;
1097 }
1098
1099 size_t geometryShaderSize = geometryExe->getLength();
1100 stream->writeInt(geometryShaderSize);
1101 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001102 }
1103
Geoff Langb543aff2014-09-30 14:52:54 -04001104 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001105}
1106
Geoff Langc5629752015-12-07 16:29:04 -05001107void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1108{
1109}
1110
Jamie Madill334d6152015-10-22 14:00:28 -04001111gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1112 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001113{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001114 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001115
Jamie Madill85a18042015-03-05 15:41:41 -05001116 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001117 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001118
1119 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1120 {
1121 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1122
1123 if (colorbuffer)
1124 {
Jamie Madill334d6152015-10-22 14:00:28 -04001125 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1126 ? GL_COLOR_ATTACHMENT0
1127 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001128 }
1129 else
1130 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001131 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001132 }
1133 }
1134
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001135 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001136}
1137
Jamie Madill97399232014-12-23 12:31:15 -05001138gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001139 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001140 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001141{
1142 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1143 {
1144 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1145 {
Geoff Langb543aff2014-09-30 14:52:54 -04001146 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1147 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001148 }
1149 }
1150
Jamie Madill334d6152015-10-22 14:00:28 -04001151 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1152 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001153
1154 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001155 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001156
1157 gl::InfoLog tempInfoLog;
1158 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1159
Jamie Madillada9ecc2015-08-17 12:53:37 -04001160 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001161 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001162 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
1163 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001164 if (error.isError())
1165 {
1166 return error;
1167 }
Brandon Joneseb994362014-09-24 10:27:28 -07001168
Jamie Madill97399232014-12-23 12:31:15 -05001169 if (pixelExecutable)
1170 {
1171 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1172 }
1173 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001174 {
1175 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001176 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001177 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1178 }
Brandon Jones22502d52014-08-29 16:58:36 -07001179
Geoff Langb543aff2014-09-30 14:52:54 -04001180 *outExectuable = pixelExecutable;
1181 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001182}
1183
Jamie Madilld3dfda22015-07-06 08:28:49 -04001184gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001185 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001186 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001187{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001188 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001189
1190 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1191 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001192 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001193 {
Geoff Langb543aff2014-09-30 14:52:54 -04001194 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1195 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001196 }
1197 }
1198
Brandon Jones22502d52014-08-29 16:58:36 -07001199 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001200 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1201 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001202
1203 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001204 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001205
1206 gl::InfoLog tempInfoLog;
1207 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1208
Jamie Madillada9ecc2015-08-17 12:53:37 -04001209 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001210 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001211 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1212 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001213 if (error.isError())
1214 {
1215 return error;
1216 }
1217
Jamie Madill97399232014-12-23 12:31:15 -05001218 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001219 {
Jamie Madill334d6152015-10-22 14:00:28 -04001220 mVertexExecutables.push_back(
1221 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001222 }
Jamie Madill97399232014-12-23 12:31:15 -05001223 else if (!infoLog)
1224 {
1225 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001226 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001227 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1228 }
Brandon Jones22502d52014-08-29 16:58:36 -07001229
Geoff Langb543aff2014-09-30 14:52:54 -04001230 *outExectuable = vertexExecutable;
1231 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001232}
1233
Jamie Madill4e31ad52015-10-29 10:32:57 -04001234gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Data &data,
1235 GLenum drawMode,
1236 ShaderExecutableD3D **outExecutable,
1237 gl::InfoLog *infoLog)
1238{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001239 if (outExecutable)
1240 {
1241 *outExecutable = nullptr;
1242 }
1243
Austin Kinross88829e82016-01-12 13:04:41 -08001244 // Return a null shader if the current rendering doesn't use a geometry shader
1245 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001246 {
1247 return gl::Error(GL_NO_ERROR);
1248 }
1249
Jamie Madill4e31ad52015-10-29 10:32:57 -04001250 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1251
1252 if (mGeometryExecutables[geometryShaderType] != nullptr)
1253 {
1254 if (outExecutable)
1255 {
1256 *outExecutable = mGeometryExecutables[geometryShaderType];
1257 }
1258 return gl::Error(GL_NO_ERROR);
1259 }
1260
1261 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
1262 geometryShaderType, data, mData, mGeometryShaderPreamble);
1263
1264 gl::InfoLog tempInfoLog;
1265 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1266
1267 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001268 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001269 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
1270 &mGeometryExecutables[geometryShaderType]);
1271
1272 if (!infoLog && error.isError())
1273 {
1274 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1275 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1276 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1277 }
1278
1279 if (outExecutable)
1280 {
1281 *outExecutable = mGeometryExecutables[geometryShaderType];
1282 }
1283 return error;
1284}
1285
1286LinkResult ProgramD3D::compileProgramExecutables(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001287{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001288 const gl::InputLayout &defaultInputLayout =
1289 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001290 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001291 gl::Error error =
1292 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001293 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001294 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001295 return LinkResult(false, error);
1296 }
Austin Kinross434953e2015-02-20 10:49:51 -08001297
Jamie Madill334d6152015-10-22 14:00:28 -04001298 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001299 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001300 error =
1301 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001302 if (error.isError())
1303 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001304 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001305 }
Brandon Jones44151a92014-09-10 11:32:25 -07001306
Jamie Madill4e31ad52015-10-29 10:32:57 -04001307 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill847638a2015-11-20 13:01:41 -05001308 ShaderExecutableD3D *pointGS = nullptr;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001309 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001310 {
Jamie Madill847638a2015-11-20 13:01:41 -05001311 getGeometryExecutableForPrimitiveType(data, GL_POINTS, &pointGS, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001312 }
1313
Jamie Madillca03b352015-09-02 12:38:13 -04001314 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001315
1316 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001317 {
Jamie Madill334d6152015-10-22 14:00:28 -04001318 // Geometry shaders are currently only used internally, so there is no corresponding shader
1319 // object at the interface level. For now the geometry shader debug info is prepended to
1320 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001321 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001322 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001323 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1324 }
1325
1326 if (defaultVertexExecutable)
1327 {
1328 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1329 }
1330
1331 if (defaultPixelExecutable)
1332 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001333 const ShaderD3D *fragmentShaderD3D =
1334 GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001335 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1336 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001337
Jamie Madill847638a2015-11-20 13:01:41 -05001338 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable &&
1339 (!usesGeometryShader(GL_POINTS) || pointGS));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001340 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001341}
1342
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001343LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001344{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001345 reset();
1346
1347 // TODO(jmadill): structures containing samplers
1348 for (const gl::LinkedUniform &linkedUniform : mData.getUniforms())
1349 {
1350 if (linkedUniform.isSampler() && linkedUniform.isField())
1351 {
1352 infoLog << "Structures containing samplers not currently supported in D3D.";
1353 return LinkResult(false, gl::Error(GL_NO_ERROR));
1354 }
1355 }
1356
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001357 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1358 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001359
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001360 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1361 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1362
Jamie Madill63069df2015-09-01 17:26:41 +00001363 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001364 mSamplersPS.resize(data.caps->maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001365
Arun Patole44efa0b2015-03-04 17:11:05 +05301366 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001367 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1368
Austin Kinross02df7962015-07-01 10:03:42 -07001369 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1370 {
1371 if (fragmentShaderD3D->usesFrontFacing())
1372 {
1373 infoLog << "The current renderer doesn't support gl_FrontFacing";
1374 return LinkResult(false, gl::Error(GL_NO_ERROR));
1375 }
1376 }
1377
Jamie Madillca03b352015-09-02 12:38:13 -04001378 std::vector<PackedVarying> packedVaryings =
1379 MergeVaryings(*vertexShader, *fragmentShader, mData.getTransformFeedbackVaryingNames());
1380
Brandon Jones22502d52014-08-29 16:58:36 -07001381 // Map the varyings to the register file
Jamie Madill9fc36822015-11-18 13:08:07 -05001382 VaryingPacking varyingPacking(data.caps->maxVaryingVectors);
1383 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
1384 mData.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001385 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001386 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001387 }
1388
Jamie Madille39a3f02015-11-17 20:42:15 -05001389 ProgramD3DMetadata metadata(mRenderer->getMajorShaderModel(), mRenderer->getShaderModelSuffix(),
1390 usesInstancedPointSpriteEmulation(), vertexShaderD3D,
1391 fragmentShaderD3D);
1392
Jamie Madill9fc36822015-11-18 13:08:07 -05001393 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1394 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1395
1396 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.caps->maxVaryingVectors)
1397 {
1398 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1399 return LinkResult(false, gl::Error(GL_NO_ERROR));
1400 }
1401
1402 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1403 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1404 // intelligently, but D3D9 assumes one semantic per register.
1405 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1406 varyingPacking.getMaxSemanticIndex() > data.caps->maxVaryingVectors)
1407 {
1408 infoLog << "Cannot pack these varyings on D3D9.";
1409 return LinkResult(false, gl::Error(GL_NO_ERROR));
1410 }
1411
1412 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mData, metadata, varyingPacking, &mPixelHLSL,
1413 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001414 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001416 }
1417
Brandon Jones44151a92014-09-10 11:32:25 -07001418 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madille39a3f02015-11-17 20:42:15 -05001419 mDynamicHLSL->getPixelShaderOutputKey(data, mData, metadata, &mPixelShaderKey);
1420 mUsesFragDepth = metadata.usesFragDepth(mData);
Brandon Jones44151a92014-09-10 11:32:25 -07001421
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001422 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001423 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001424 for (const auto &varying : packedVaryings)
1425 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001426 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001427 {
1428 mUsesFlatInterpolation = true;
1429 break;
1430 }
1431 }
1432
Jamie Madill4e31ad52015-10-29 10:32:57 -04001433 if (mRenderer->getMajorShaderModel() >= 4)
1434 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001435 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1436 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001437 }
1438
Jamie Madill63805b42015-08-25 13:17:39 -04001439 initSemanticIndex();
Jamie Madill437d2662014-12-05 14:23:35 -05001440
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001441 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001442
Jamie Madill9fc36822015-11-18 13:08:07 -05001443 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001444
Jamie Madill4e31ad52015-10-29 10:32:57 -04001445 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001446 if (result.error.isError() || !result.linkSuccess)
1447 {
1448 infoLog << "Failed to create D3D shaders.";
1449 return result;
1450 }
1451
Jamie Madill4a3c2342015-10-08 12:58:45 -04001452 initUniformBlockInfo();
1453
Geoff Lang7dd2e102014-11-10 15:19:26 -05001454 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001455}
1456
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001457GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001458{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001459 // TODO(jmadill): Do something useful here?
1460 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001461}
1462
Jamie Madill4a3c2342015-10-08 12:58:45 -04001463void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001464{
1465 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1466
Jamie Madill62d31cb2015-09-11 13:25:51 -04001467 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1468 {
1469 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1470 continue;
1471
Jamie Madill4a3c2342015-10-08 12:58:45 -04001472 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001473 continue;
1474
Jamie Madill4a3c2342015-10-08 12:58:45 -04001475 size_t dataSize = getUniformBlockInfo(vertexBlock);
1476 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001477 }
1478
1479 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1480
1481 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1482 {
1483 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1484 continue;
1485
Jamie Madill4a3c2342015-10-08 12:58:45 -04001486 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001487 continue;
1488
Jamie Madill4a3c2342015-10-08 12:58:45 -04001489 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1490 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001491 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001492}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001493
Jamie Madill4a3c2342015-10-08 12:58:45 -04001494void ProgramD3D::assignUniformBlockRegisters()
1495{
1496 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001497
1498 // Assign registers and update sizes.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001499 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1500 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001501
Jamie Madill4a3c2342015-10-08 12:58:45 -04001502 for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001503 {
1504 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1505
Jamie Madill4a3c2342015-10-08 12:58:45 -04001506 D3DUniformBlock d3dUniformBlock;
1507
Jamie Madill62d31cb2015-09-11 13:25:51 -04001508 if (uniformBlock.vertexStaticUse)
1509 {
1510 unsigned int baseRegister =
1511 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001512 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001513 }
1514
1515 if (uniformBlock.fragmentStaticUse)
1516 {
1517 unsigned int baseRegister =
1518 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001519 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001520 }
1521
Jamie Madill4a3c2342015-10-08 12:58:45 -04001522 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001523 }
1524}
1525
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001526void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001527{
1528 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001529 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001530 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001531 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001532 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001533 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001534 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001535 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001536 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001537 vertexRegisters = std::max(vertexRegisters,
1538 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001539 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001540 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001541 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001542 fragmentRegisters = std::max(
1543 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001544 }
1545 }
1546 }
1547
Jamie Madill334d6152015-10-22 14:00:28 -04001548 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001549 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1550}
1551
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001552gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001553{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001554 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001555
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001556 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001557 if (error.isError())
1558 {
1559 return error;
1560 }
1561
Jamie Madill62d31cb2015-09-11 13:25:51 -04001562 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001563 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001564 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001565 }
1566
1567 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001568}
1569
Jamie Madilld1fe1642015-08-21 16:26:04 -04001570gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001571{
Jamie Madill4a3c2342015-10-08 12:58:45 -04001572 if (mData.getUniformBlocks().empty())
1573 {
1574 return gl::Error(GL_NO_ERROR);
1575 }
1576
1577 // Lazy init.
1578 if (mD3DUniformBlocks.empty())
1579 {
1580 assignUniformBlockRegisters();
1581 }
1582
Jamie Madill03260fa2015-06-22 13:57:22 -04001583 mVertexUBOCache.clear();
1584 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001585
1586 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1587 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1588
Jamie Madill4a3c2342015-10-08 12:58:45 -04001589 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001590 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001591 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001592 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
1593 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001594
Brandon Jones18bd4102014-09-22 14:21:44 -07001595 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001596 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001597 {
1598 continue;
1599 }
1600
Jamie Madill4a3c2342015-10-08 12:58:45 -04001601 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001602 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001603 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001604 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001605
Jamie Madill969194d2015-07-20 14:36:56 -04001606 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001607 {
1608 mVertexUBOCache.resize(registerIndex + 1, -1);
1609 }
1610
1611 ASSERT(mVertexUBOCache[registerIndex] == -1);
1612 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001613 }
1614
Jamie Madill4a3c2342015-10-08 12:58:45 -04001615 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001616 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001617 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001618 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001619
1620 if (mFragmentUBOCache.size() <= registerIndex)
1621 {
1622 mFragmentUBOCache.resize(registerIndex + 1, -1);
1623 }
1624
1625 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1626 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001627 }
1628 }
1629
Jamie Madill03260fa2015-06-22 13:57:22 -04001630 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001631}
1632
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001633void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001634{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001635 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001636 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001637 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001638 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001639}
1640
Jamie Madill334d6152015-10-22 14:00:28 -04001641void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001642{
1643 setUniform(location, count, v, GL_FLOAT);
1644}
1645
1646void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1647{
1648 setUniform(location, count, v, GL_FLOAT_VEC2);
1649}
1650
1651void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1652{
1653 setUniform(location, count, v, GL_FLOAT_VEC3);
1654}
1655
1656void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1657{
1658 setUniform(location, count, v, GL_FLOAT_VEC4);
1659}
1660
Jamie Madill334d6152015-10-22 14:00:28 -04001661void ProgramD3D::setUniformMatrix2fv(GLint location,
1662 GLsizei count,
1663 GLboolean transpose,
1664 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001665{
1666 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1667}
1668
Jamie Madill334d6152015-10-22 14:00:28 -04001669void ProgramD3D::setUniformMatrix3fv(GLint location,
1670 GLsizei count,
1671 GLboolean transpose,
1672 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001673{
1674 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1675}
1676
Jamie Madill334d6152015-10-22 14:00:28 -04001677void ProgramD3D::setUniformMatrix4fv(GLint location,
1678 GLsizei count,
1679 GLboolean transpose,
1680 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001681{
1682 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1683}
1684
Jamie Madill334d6152015-10-22 14:00:28 -04001685void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1686 GLsizei count,
1687 GLboolean transpose,
1688 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001689{
1690 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1691}
1692
Jamie Madill334d6152015-10-22 14:00:28 -04001693void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1694 GLsizei count,
1695 GLboolean transpose,
1696 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001697{
1698 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1699}
1700
Jamie Madill334d6152015-10-22 14:00:28 -04001701void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1702 GLsizei count,
1703 GLboolean transpose,
1704 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001705{
1706 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1707}
1708
Jamie Madill334d6152015-10-22 14:00:28 -04001709void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1710 GLsizei count,
1711 GLboolean transpose,
1712 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001713{
1714 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1715}
1716
Jamie Madill334d6152015-10-22 14:00:28 -04001717void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1718 GLsizei count,
1719 GLboolean transpose,
1720 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001721{
1722 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1723}
1724
Jamie Madill334d6152015-10-22 14:00:28 -04001725void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1726 GLsizei count,
1727 GLboolean transpose,
1728 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001729{
1730 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1731}
1732
1733void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1734{
1735 setUniform(location, count, v, GL_INT);
1736}
1737
1738void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1739{
1740 setUniform(location, count, v, GL_INT_VEC2);
1741}
1742
1743void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1744{
1745 setUniform(location, count, v, GL_INT_VEC3);
1746}
1747
1748void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1749{
1750 setUniform(location, count, v, GL_INT_VEC4);
1751}
1752
1753void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1754{
1755 setUniform(location, count, v, GL_UNSIGNED_INT);
1756}
1757
1758void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1759{
1760 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1761}
1762
1763void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1764{
1765 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1766}
1767
1768void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1769{
1770 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1771}
1772
Jamie Madill4a3c2342015-10-08 12:58:45 -04001773void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1774 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001775{
1776}
1777
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001778void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001779{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001780 D3DUniformMap uniformMap;
Jamie Madill334d6152015-10-22 14:00:28 -04001781 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001782 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001783
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001784 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001785 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001786 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001787 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001788 }
1789 }
1790
Jamie Madill334d6152015-10-22 14:00:28 -04001791 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001792 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001793 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001794 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001795 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001796 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001797 }
1798 }
1799
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001800 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
1801 for (const gl::LinkedUniform &glUniform : mData.getUniforms())
1802 {
1803 if (!glUniform.isInDefaultBlock())
1804 continue;
1805
1806 auto mapEntry = uniformMap.find(glUniform.name);
1807 ASSERT(mapEntry != uniformMap.end());
1808 mD3DUniforms.push_back(mapEntry->second);
1809 }
1810
Jamie Madill62d31cb2015-09-11 13:25:51 -04001811 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001812 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001813}
1814
Jamie Madill91445bc2015-09-23 16:47:53 -04001815void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001816 const sh::Uniform &uniform,
1817 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001818{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001819 if (uniform.isBuiltIn())
Jamie Madillfb536032015-09-11 13:19:49 -04001820 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001821 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001822 return;
1823 }
1824
Jamie Madill91445bc2015-09-23 16:47:53 -04001825 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1826
1827 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1828 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001829 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001830 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001831
Jamie Madill91445bc2015-09-23 16:47:53 -04001832 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001833}
1834
Jamie Madill62d31cb2015-09-11 13:25:51 -04001835D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1836{
1837 for (D3DUniform *d3dUniform : mD3DUniforms)
1838 {
1839 if (d3dUniform->name == name)
1840 {
1841 return d3dUniform;
1842 }
1843 }
1844
1845 return nullptr;
1846}
1847
Jamie Madill91445bc2015-09-23 16:47:53 -04001848void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001849 const sh::ShaderVariable &uniform,
1850 const std::string &fullName,
1851 sh::HLSLBlockEncoder *encoder,
1852 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001853{
1854 if (uniform.isStruct())
1855 {
1856 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1857 {
1858 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1859
Jamie Madill55def582015-05-04 11:24:57 -04001860 if (encoder)
1861 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001862
1863 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1864 {
Jamie Madill334d6152015-10-22 14:00:28 -04001865 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001866 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1867
Jamie Madill91445bc2015-09-23 16:47:53 -04001868 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001869 }
1870
Jamie Madill55def582015-05-04 11:24:57 -04001871 if (encoder)
1872 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001873 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001874 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001875 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001876
1877 // Not a struct. Arrays are treated as aggregate types.
1878 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001879 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001880 encoder->enterAggregateType();
1881 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001882
Jamie Madill62d31cb2015-09-11 13:25:51 -04001883 // Advance the uniform offset, to track registers allocation for structs
1884 sh::BlockMemberInfo blockInfo =
1885 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1886 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001887
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001888 auto uniformMapEntry = uniformMap->find(fullName);
1889 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001890
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001891 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001892 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001893 d3dUniform = uniformMapEntry->second;
1894 }
1895 else
1896 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001897 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001898 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001899 }
1900
1901 if (encoder)
1902 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001903 d3dUniform->registerElement =
1904 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001905 unsigned int reg =
1906 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001907 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001908 {
1909 d3dUniform->psRegisterIndex = reg;
1910 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001911 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001912 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001913 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001914 d3dUniform->vsRegisterIndex = reg;
1915 }
Jamie Madillfb536032015-09-11 13:19:49 -04001916
1917 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001918 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001919 {
1920 encoder->exitAggregateType();
1921 }
1922 }
1923}
1924
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001925template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001926void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001927{
Jamie Madill334d6152015-10-22 14:00:28 -04001928 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001929 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1930
Jamie Madill62d31cb2015-09-11 13:25:51 -04001931 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001932
Jamie Madill62d31cb2015-09-11 13:25:51 -04001933 unsigned int elementCount = targetUniform->elementCount();
1934 unsigned int arrayElement = mData.getUniformLocations()[location].element;
1935 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001936
1937 if (targetUniform->type == targetUniformType)
1938 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001939 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001940
Jamie Madill62d31cb2015-09-11 13:25:51 -04001941 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001942 {
Jamie Madill334d6152015-10-22 14:00:28 -04001943 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001944 const T *source = v + (i * components);
1945
1946 for (int c = 0; c < components; c++)
1947 {
1948 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1949 }
1950 for (int c = components; c < 4; c++)
1951 {
1952 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1953 }
1954 }
1955 }
1956 else if (targetUniform->type == targetBoolType)
1957 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001958 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001959
Jamie Madill62d31cb2015-09-11 13:25:51 -04001960 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001961 {
Jamie Madill334d6152015-10-22 14:00:28 -04001962 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001963 const T *source = v + (i * components);
1964
1965 for (int c = 0; c < components; c++)
1966 {
Jamie Madill334d6152015-10-22 14:00:28 -04001967 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1968 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001969 }
1970 for (int c = components; c < 4; c++)
1971 {
1972 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1973 }
1974 }
1975 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001976 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001977 {
1978 ASSERT(targetUniformType == GL_INT);
1979
Jamie Madill62d31cb2015-09-11 13:25:51 -04001980 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001981
1982 bool wasDirty = targetUniform->dirty;
1983
Jamie Madill62d31cb2015-09-11 13:25:51 -04001984 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001985 {
Jamie Madill334d6152015-10-22 14:00:28 -04001986 GLint *dest = target + (i * 4);
1987 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001988
1989 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1990 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1991 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1992 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1993 }
1994
1995 if (!wasDirty && targetUniform->dirty)
1996 {
1997 mDirtySamplerMapping = true;
1998 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001999 }
Jamie Madill334d6152015-10-22 14:00:28 -04002000 else
2001 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002002}
2003
2004template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002005void ProgramD3D::setUniformMatrixfv(GLint location,
2006 GLsizei countIn,
2007 GLboolean transpose,
2008 const GLfloat *value,
2009 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002010{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002011 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002012
Jamie Madill62d31cb2015-09-11 13:25:51 -04002013 unsigned int elementCount = targetUniform->elementCount();
2014 unsigned int arrayElement = mData.getUniformLocations()[location].element;
2015 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002016
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002018 GLfloat *target =
2019 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002020
Jamie Madill62d31cb2015-09-11 13:25:51 -04002021 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002022 {
2023 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2024 if (transpose == GL_FALSE)
2025 {
Jamie Madill334d6152015-10-22 14:00:28 -04002026 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2027 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002028 }
2029 else
2030 {
Jamie Madill334d6152015-10-22 14:00:28 -04002031 targetUniform->dirty =
2032 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002033 }
2034 target += targetMatrixStride;
2035 value += cols * rows;
2036 }
2037}
2038
Jamie Madill4a3c2342015-10-08 12:58:45 -04002039size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002040{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002041 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002042
Jamie Madill62d31cb2015-09-11 13:25:51 -04002043 // define member uniforms
2044 sh::Std140BlockEncoder std140Encoder;
2045 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2046 sh::BlockLayoutEncoder *encoder = nullptr;
2047
2048 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002049 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002050 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002051 }
2052 else
2053 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002054 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002055 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002056
2057 GetUniformBlockInfo(interfaceBlock.fields, "", encoder, interfaceBlock.isRowMajorLayout,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002058 &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002059
2060 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002061}
2062
Jamie Madill62d31cb2015-09-11 13:25:51 -04002063void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002064{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002065 for (const D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002066 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002067 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002068 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002069 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002070 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002071 }
2072}
2073
Jamie Madill62d31cb2015-09-11 13:25:51 -04002074void ProgramD3D::assignSamplerRegisters(const D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002075{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002076 ASSERT(d3dUniform->isSampler());
2077 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX ||
2078 d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madillfb536032015-09-11 13:19:49 -04002079
Jamie Madill62d31cb2015-09-11 13:25:51 -04002080 if (d3dUniform->vsRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002081 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002082 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2083 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002084 }
2085
Jamie Madill62d31cb2015-09-11 13:25:51 -04002086 if (d3dUniform->psRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002087 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002088 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2089 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002090 }
2091}
2092
Jamie Madill62d31cb2015-09-11 13:25:51 -04002093// static
2094void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002095 GLenum samplerType,
2096 unsigned int samplerCount,
2097 std::vector<Sampler> &outSamplers,
2098 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002099{
2100 unsigned int samplerIndex = startSamplerIndex;
2101
2102 do
2103 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002104 ASSERT(samplerIndex < outSamplers.size());
2105 Sampler *sampler = &outSamplers[samplerIndex];
2106 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002107 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002108 sampler->logicalTextureUnit = 0;
2109 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002110 samplerIndex++;
2111 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002112}
2113
Brandon Jonesc9610c52014-08-25 17:02:59 -07002114void ProgramD3D::reset()
2115{
Brandon Joneseb994362014-09-24 10:27:28 -07002116 SafeDeleteContainer(mVertexExecutables);
2117 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002118
2119 for (auto &element : mGeometryExecutables)
2120 {
2121 SafeDelete(element);
2122 }
Brandon Joneseb994362014-09-24 10:27:28 -07002123
Brandon Jones22502d52014-08-29 16:58:36 -07002124 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002125 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002126
2127 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002128 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002129 mUsesFragDepth = false;
2130 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002131 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002132 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002133
Jamie Madill62d31cb2015-09-11 13:25:51 -04002134 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002135 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002136
Brandon Jonesc9610c52014-08-25 17:02:59 -07002137 SafeDelete(mVertexUniformStorage);
2138 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002139
2140 mSamplersPS.clear();
2141 mSamplersVS.clear();
2142
2143 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002144 mUsedPixelSamplerRange = 0;
2145 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002146
Jamie Madill63805b42015-08-25 13:17:39 -04002147 std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1);
Jamie Madill437d2662014-12-05 14:23:35 -05002148 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002149
Jamie Madill9fc36822015-11-18 13:08:07 -05002150 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002151
2152 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002153}
2154
Geoff Lang7dd2e102014-11-10 15:19:26 -05002155unsigned int ProgramD3D::getSerial() const
2156{
2157 return mSerial;
2158}
2159
2160unsigned int ProgramD3D::issueSerial()
2161{
2162 return mCurrentSerial++;
2163}
2164
Jamie Madill63805b42015-08-25 13:17:39 -04002165void ProgramD3D::initSemanticIndex()
2166{
2167 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2168 ASSERT(vertexShader != nullptr);
2169
2170 // Init semantic index
2171 for (const sh::Attribute &attribute : mData.getAttributes())
2172 {
2173 int attributeIndex = attribute.location;
2174 int index = vertexShader->getSemanticIndex(attribute.name);
2175 int regs = gl::VariableRegisterCount(attribute.type);
2176
2177 for (int reg = 0; reg < regs; ++reg)
2178 {
2179 mSemanticIndexes[attributeIndex + reg] = index + reg;
2180 }
2181 }
2182
2183 initAttributesByLayout();
2184}
2185
Jamie Madill437d2662014-12-05 14:23:35 -05002186void ProgramD3D::initAttributesByLayout()
2187{
2188 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2189 {
2190 mAttributesByLayout[i] = i;
2191 }
2192
Jamie Madill63805b42015-08-25 13:17:39 -04002193 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS],
2194 AttributeSorter(mSemanticIndexes));
Jamie Madill437d2662014-12-05 14:23:35 -05002195}
2196
Jamie Madill334d6152015-10-22 14:00:28 -04002197void ProgramD3D::sortAttributesByLayout(
2198 const std::vector<TranslatedAttribute> &unsortedAttributes,
2199 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2200 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002201{
Jamie Madill476682e2015-06-30 10:04:29 -04002202 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002203 {
Jamie Madill334d6152015-10-22 14:00:28 -04002204 int oldIndex = mAttributesByLayout[attribIndex];
Jamie Madill63805b42015-08-25 13:17:39 -04002205 sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex];
Jamie Madill334d6152015-10-22 14:00:28 -04002206 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002207 }
2208}
2209
Jamie Madill63805b42015-08-25 13:17:39 -04002210void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002211{
Jamie Madillbd136f92015-08-10 14:51:37 -04002212 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002213 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002214
Dian Xianga4928832015-09-15 10:11:17 -07002215 for (unsigned int attributeIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002216 {
Jamie Madill63805b42015-08-25 13:17:39 -04002217 int semanticIndex = mSemanticIndexes[attributeIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002218
2219 if (semanticIndex != -1)
2220 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002221 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2222 {
2223 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2224 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002225 mCachedInputLayout[semanticIndex] =
2226 GetVertexFormatType(vertexAttributes[attributeIndex],
2227 state.getVertexAttribCurrentValue(attributeIndex).Type);
2228 }
2229 }
2230}
2231
Jamie Madill9fc36822015-11-18 13:08:07 -05002232void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002233{
Jamie Madill9fc36822015-11-18 13:08:07 -05002234 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2235
2236 const std::string &varyingSemantic =
2237 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2238
Jamie Madillccdf74b2015-08-18 10:46:12 -04002239 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002240 mStreamOutVaryings.clear();
2241
2242 const auto &tfVaryingNames = mData.getTransformFeedbackVaryingNames();
2243 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2244 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002245 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002246 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2247 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002248 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002249 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002250 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002251 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2252 builtins.glPosition.index, 4, outputSlot));
2253 }
2254 }
2255 else if (tfVaryingName == "gl_FragCoord")
2256 {
2257 if (builtins.glFragCoord.enabled)
2258 {
2259 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2260 builtins.glFragCoord.index, 4, outputSlot));
2261 }
2262 }
2263 else if (tfVaryingName == "gl_PointSize")
2264 {
2265 if (builtins.glPointSize.enabled)
2266 {
2267 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2268 }
2269 }
2270 else
2271 {
2272 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2273 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002274 const auto &varying = *registerInfo.packedVarying->varying;
2275 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002276 int componentCount = gl::VariableColumnCount(transposedType);
2277 ASSERT(!varying.isBuiltIn());
2278
Jamie Madill55c25d02015-11-18 13:08:08 -05002279 // Transform feedback for varying structs is underspecified.
2280 // See Khronos bug 9856.
2281 // TODO(jmadill): Figure out how to be spec-compliant here.
2282 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2283 continue;
2284
Jamie Madill9fc36822015-11-18 13:08:07 -05002285 // There can be more than one register assigned to a particular varying, and each
2286 // register needs its own stream out entry.
2287 if (tfVaryingName == varying.name)
2288 {
2289 mStreamOutVaryings.push_back(D3DVarying(
2290 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2291 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002292 }
2293 }
2294 }
2295}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002296
2297D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2298{
2299 return mD3DUniforms[mData.getUniformLocations()[location].index];
2300}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002301
2302bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2303{
2304 std::string baseName = blockName;
2305 gl::ParseAndStripArrayIndex(&baseName);
2306
2307 auto sizeIter = mBlockDataSizes.find(baseName);
2308 if (sizeIter == mBlockDataSizes.end())
2309 {
2310 *sizeOut = 0;
2311 return false;
2312 }
2313
2314 *sizeOut = sizeIter->second;
2315 return true;
2316}
2317
2318bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2319 sh::BlockMemberInfo *memberInfoOut) const
2320{
2321 auto infoIter = mBlockInfo.find(memberUniformName);
2322 if (infoIter == mBlockInfo.end())
2323 {
2324 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2325 return false;
2326 }
2327
2328 *memberInfoOut = infoIter->second;
2329 return true;
2330}
Brandon Jonesc9610c52014-08-25 17:02:59 -07002331}