blob: aff39b0b99e471586a0cfc91176993503550508a [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
Dian Xianga4928832015-09-15 10:11:17 -070011#include "common/BitSetIterator.h"
Jamie Madill437d2662014-12-05 14:23:35 -050012#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Framebuffer.h"
14#include "libANGLE/FramebufferAttachment.h"
15#include "libANGLE/Program.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040016#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000017#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050018#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050019#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/renderer/d3d/RendererD3D.h"
21#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050022#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill65345da2015-11-13 11:25:23 -050023#include "libANGLE/renderer/d3d/VaryingPacking.h"
Jamie Madill437d2662014-12-05 14:23:35 -050024#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050025
Brandon Jonesc9610c52014-08-25 17:02:59 -070026namespace rx
27{
28
Brandon Joneseb994362014-09-24 10:27:28 -070029namespace
30{
31
Jamie Madillf8dd7b12015-08-05 13:50:08 -040032gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070033{
Jamie Madillbd136f92015-08-10 14:51:37 -040034 gl::InputLayout defaultLayout;
35 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070036 {
Brandon Joneseb994362014-09-24 10:27:28 -070037 if (shaderAttr.type != GL_NONE)
38 {
39 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
40
Jamie Madilld3dfda22015-07-06 08:28:49 -040041 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040042 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070043 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040044 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040045 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040046 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040047 gl::VertexFormatType defaultType =
48 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070049
Jamie Madillbd136f92015-08-10 14:51:37 -040050 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070051 }
52 }
53 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040054
55 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070056}
57
Jamie Madill334d6152015-10-22 14:00:28 -040058std::vector<GLenum> GetDefaultOutputLayoutFromShader(
59 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070060{
Jamie Madillb4463142014-12-19 14:56:54 -050061 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070062
Jamie Madillb4463142014-12-19 14:56:54 -050063 if (!shaderOutputVars.empty())
64 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070065 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
66 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050067 }
Brandon Joneseb994362014-09-24 10:27:28 -070068
69 return defaultPixelOutput;
70}
71
Brandon Jones1a8a7e32014-10-01 12:49:30 -070072bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
73{
74 return var.isRowMajorLayout;
75}
76
77bool IsRowMajorLayout(const sh::ShaderVariable &var)
78{
79 return false;
80}
81
Jamie Madill437d2662014-12-05 14:23:35 -050082struct AttributeSorter
83{
Jamie Madill63805b42015-08-25 13:17:39 -040084 AttributeSorter(const ProgramD3D::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -050085 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -050086 {
87 }
88
89 bool operator()(int a, int b)
90 {
Jamie Madill80d934b2015-02-19 10:16:12 -050091 int indexA = (*originalIndices)[a];
92 int indexB = (*originalIndices)[b];
93
Jamie Madill334d6152015-10-22 14:00:28 -040094 if (indexA == -1)
95 return false;
96 if (indexB == -1)
97 return true;
Jamie Madill80d934b2015-02-19 10:16:12 -050098 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -050099 }
100
Jamie Madill63805b42015-08-25 13:17:39 -0400101 const ProgramD3D::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500102};
103
Jamie Madill9fc36822015-11-18 13:08:07 -0500104// true if varying x has a higher priority in packing than y
105bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
106{
Jamie Madill55c25d02015-11-18 13:08:08 -0500107 return gl::CompareShaderVar(*x.varying, *y.varying);
Jamie Madill9fc36822015-11-18 13:08:07 -0500108}
109
Jamie Madillca03b352015-09-02 12:38:13 -0400110std::vector<PackedVarying> MergeVaryings(const gl::Shader &vertexShader,
111 const gl::Shader &fragmentShader,
112 const std::vector<std::string> &tfVaryings)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400113{
Jamie Madillca03b352015-09-02 12:38:13 -0400114 std::vector<PackedVarying> packedVaryings;
115
116 for (const sh::Varying &output : vertexShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400117 {
Jamie Madillca03b352015-09-02 12:38:13 -0400118 bool packed = false;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400119
120 // Built-in varyings obey special rules
Jamie Madillca03b352015-09-02 12:38:13 -0400121 if (output.isBuiltIn())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400122 {
123 continue;
124 }
125
Jamie Madillca03b352015-09-02 12:38:13 -0400126 for (const sh::Varying &input : fragmentShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400127 {
Jamie Madillca03b352015-09-02 12:38:13 -0400128 if (output.name == input.name)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400129 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500130 if (output.isStruct())
131 {
132 ASSERT(!output.isArray());
133 for (const auto &field : output.fields)
134 {
135 ASSERT(!field.isStruct() && !field.isArray());
136 packedVaryings.push_back(
137 PackedVarying(field, input.interpolation, input.name));
138 }
139 }
140 else
141 {
142 packedVaryings.push_back(PackedVarying(input, input.interpolation));
143 }
Jamie Madillca03b352015-09-02 12:38:13 -0400144 packed = true;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400145 break;
146 }
147 }
148
Jamie Madillca03b352015-09-02 12:38:13 -0400149 // Keep Transform FB varyings in the merged list always.
150 if (!packed)
151 {
152 for (const std::string &tfVarying : tfVaryings)
153 {
154 if (tfVarying == output.name)
155 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500156 // Transform feedback for varying structs is underspecified.
157 // See Khronos bug 9856.
158 // TODO(jmadill): Figure out how to be spec-compliant here.
159 if (!output.isStruct())
160 {
161 packedVaryings.push_back(PackedVarying(output, output.interpolation));
162 packedVaryings.back().vertexOnly = true;
163 }
Jamie Madillca03b352015-09-02 12:38:13 -0400164 break;
165 }
166 }
167 }
Jamie Madillada9ecc2015-08-17 12:53:37 -0400168 }
169
Jamie Madill9fc36822015-11-18 13:08:07 -0500170 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
171
Jamie Madillca03b352015-09-02 12:38:13 -0400172 return packedVaryings;
Brandon Joneseb994362014-09-24 10:27:28 -0700173}
174
Jamie Madill62d31cb2015-09-11 13:25:51 -0400175template <typename VarT>
176void GetUniformBlockInfo(const std::vector<VarT> &fields,
177 const std::string &prefix,
178 sh::BlockLayoutEncoder *encoder,
179 bool inRowMajorLayout,
180 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
181{
182 for (const VarT &field : fields)
183 {
184 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
185
186 if (field.isStruct())
187 {
188 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
189
190 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
191 {
192 encoder->enterAggregateType();
193
194 const std::string uniformElementName =
195 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
196 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
197 blockInfoOut);
198
199 encoder->exitAggregateType();
200 }
201 }
202 else
203 {
204 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
205 (*blockInfoOut)[fieldName] =
206 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
207 }
208 }
209}
210
Jamie Madill334d6152015-10-22 14:00:28 -0400211template <typename T>
Jacek Caban2302e692015-12-01 12:44:43 +0100212static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
213{
214 ASSERT(dest != NULL);
215 ASSERT(dirtyFlag != NULL);
216
217 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
218 *dest = source;
219}
220
221template <typename T>
Jamie Madill334d6152015-10-22 14:00:28 -0400222bool TransposeMatrix(T *target,
223 const GLfloat *value,
224 int targetWidth,
225 int targetHeight,
226 int srcWidth,
227 int srcHeight)
228{
229 bool dirty = false;
230 int copyWidth = std::min(targetHeight, srcWidth);
231 int copyHeight = std::min(targetWidth, srcHeight);
232
233 for (int x = 0; x < copyWidth; x++)
234 {
235 for (int y = 0; y < copyHeight; y++)
236 {
237 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]),
238 &dirty);
239 }
240 }
241 // clear unfilled right side
242 for (int y = 0; y < copyWidth; y++)
243 {
244 for (int x = copyHeight; x < targetWidth; x++)
245 {
246 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
247 }
248 }
249 // clear unfilled bottom.
250 for (int y = copyWidth; y < targetHeight; y++)
251 {
252 for (int x = 0; x < targetWidth; x++)
253 {
254 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
255 }
256 }
257
258 return dirty;
259}
260
261template <typename T>
262bool ExpandMatrix(T *target,
263 const GLfloat *value,
264 int targetWidth,
265 int targetHeight,
266 int srcWidth,
267 int srcHeight)
268{
269 bool dirty = false;
270 int copyWidth = std::min(targetWidth, srcWidth);
271 int copyHeight = std::min(targetHeight, srcHeight);
272
273 for (int y = 0; y < copyHeight; y++)
274 {
275 for (int x = 0; x < copyWidth; x++)
276 {
277 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]),
278 &dirty);
279 }
280 }
281 // clear unfilled right side
282 for (int y = 0; y < copyHeight; y++)
283 {
284 for (int x = copyWidth; x < targetWidth; x++)
285 {
286 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
287 }
288 }
289 // clear unfilled bottom.
290 for (int y = copyHeight; y < targetHeight; y++)
291 {
292 for (int x = 0; x < targetWidth; x++)
293 {
294 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
295 }
296 }
297
298 return dirty;
299}
300
Jamie Madill4e31ad52015-10-29 10:32:57 -0400301gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
302{
303 switch (drawMode)
304 {
305 // Uses the point sprite geometry shader.
306 case GL_POINTS:
307 return gl::PRIMITIVE_POINTS;
308
309 // All line drawing uses the same geometry shader.
310 case GL_LINES:
311 case GL_LINE_STRIP:
312 case GL_LINE_LOOP:
313 return gl::PRIMITIVE_LINES;
314
315 // The triangle fan primitive is emulated with strips in D3D11.
316 case GL_TRIANGLES:
317 case GL_TRIANGLE_FAN:
318 return gl::PRIMITIVE_TRIANGLES;
319
320 // Special case for triangle strips.
321 case GL_TRIANGLE_STRIP:
322 return gl::PRIMITIVE_TRIANGLE_STRIP;
323
324 default:
325 UNREACHABLE();
326 return gl::PRIMITIVE_TYPE_MAX;
327 }
328}
329
Jamie Madillada9ecc2015-08-17 12:53:37 -0400330} // anonymous namespace
331
Jamie Madill28afae52015-11-09 15:07:57 -0500332// D3DUniform Implementation
333
Jamie Madill62d31cb2015-09-11 13:25:51 -0400334D3DUniform::D3DUniform(GLenum typeIn,
335 const std::string &nameIn,
336 unsigned int arraySizeIn,
337 bool defaultBlock)
338 : type(typeIn),
339 name(nameIn),
340 arraySize(arraySizeIn),
341 data(nullptr),
342 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400343 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400344 psRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400345 registerCount(0),
346 registerElement(0)
347{
348 // We use data storage for default block uniforms to cache values that are sent to D3D during
349 // rendering
350 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
351 if (defaultBlock)
352 {
353 size_t bytes = gl::VariableInternalSize(type) * elementCount();
354 data = new uint8_t[bytes];
355 memset(data, 0, bytes);
356
357 // TODO(jmadill): is this correct with non-square matrices?
358 registerCount = gl::VariableRowCount(type) * elementCount();
359 }
360}
361
362D3DUniform::~D3DUniform()
363{
364 SafeDeleteArray(data);
365}
366
367bool D3DUniform::isSampler() const
368{
369 return gl::IsSamplerType(type);
370}
371
372bool D3DUniform::isReferencedByVertexShader() const
373{
374 return vsRegisterIndex != GL_INVALID_INDEX;
375}
376
377bool D3DUniform::isReferencedByFragmentShader() const
378{
379 return psRegisterIndex != GL_INVALID_INDEX;
380}
381
Jamie Madill28afae52015-11-09 15:07:57 -0500382// D3DVarying Implementation
383
Jamie Madill9fc36822015-11-18 13:08:07 -0500384D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500385{
386}
387
Jamie Madill9fc36822015-11-18 13:08:07 -0500388D3DVarying::D3DVarying(const std::string &semanticNameIn,
389 unsigned int semanticIndexIn,
390 unsigned int componentCountIn,
391 unsigned int outputSlotIn)
392 : semanticName(semanticNameIn),
393 semanticIndex(semanticIndexIn),
394 componentCount(componentCountIn),
395 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500396{
397}
398
Jamie Madille39a3f02015-11-17 20:42:15 -0500399// ProgramD3DMetadata Implementation
400
401ProgramD3DMetadata::ProgramD3DMetadata(int rendererMajorShaderModel,
402 const std::string &shaderModelSuffix,
403 bool usesInstancedPointSpriteEmulation,
404 const ShaderD3D *vertexShader,
405 const ShaderD3D *fragmentShader)
406 : mRendererMajorShaderModel(rendererMajorShaderModel),
407 mShaderModelSuffix(shaderModelSuffix),
408 mUsesInstancedPointSpriteEmulation(usesInstancedPointSpriteEmulation),
409 mVertexShader(vertexShader),
410 mFragmentShader(fragmentShader)
411{
412}
413
414int ProgramD3DMetadata::getRendererMajorShaderModel() const
415{
416 return mRendererMajorShaderModel;
417}
418
419bool ProgramD3DMetadata::usesBroadcast(const gl::Data &data) const
420{
421 return (mFragmentShader->usesFragColor() && data.clientVersion < 3);
422}
423
424bool ProgramD3DMetadata::usesFragDepth(const gl::Program::Data &programData) const
425{
Jamie Madill63286672015-11-24 13:00:08 -0500426 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500427}
428
429bool ProgramD3DMetadata::usesPointCoord() const
430{
431 return mFragmentShader->usesPointCoord();
432}
433
434bool ProgramD3DMetadata::usesFragCoord() const
435{
436 return mFragmentShader->usesFragCoord();
437}
438
439bool ProgramD3DMetadata::usesPointSize() const
440{
441 return mVertexShader->usesPointSize();
442}
443
444bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
445{
446 return !usesPointSize() && usesPointCoord() && mRendererMajorShaderModel >= 4;
447}
448
449bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
450{
451 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader
452 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
453 // GeometryShader PointSprite emulation does not require this additional entry because the
454 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the
455 // PS_INPUT of the generated pixel shader. The Geometry Shader point sprite implementation needs
456 // gl_PointSize to be in VS_OUTPUT and GS_INPUT. Instanced point sprites doesn't need
457 // gl_PointSize in VS_OUTPUT.
458 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
459 usesInsertedPointCoordValue();
460}
461
462bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
463{
464 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
465 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
466 // the vertex shader in this case. This saves us 1 output vector.
467 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
468}
469
470bool ProgramD3DMetadata::usesSystemValuePointSize() const
471{
472 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
473}
474
475bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
476{
477 return mFragmentShader->usesMultipleRenderTargets();
478}
479
480GLint ProgramD3DMetadata::getMajorShaderVersion() const
481{
482 return mVertexShader->getData().getShaderVersion();
483}
484
485const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
486{
487 return mFragmentShader;
488}
489
Jamie Madill28afae52015-11-09 15:07:57 -0500490// ProgramD3D Implementation
491
Jamie Madilld3dfda22015-07-06 08:28:49 -0400492ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
493 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500494 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400495 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700496{
Brandon Joneseb994362014-09-24 10:27:28 -0700497}
498
499ProgramD3D::VertexExecutable::~VertexExecutable()
500{
501 SafeDelete(mShaderExecutable);
502}
503
Jamie Madilld3dfda22015-07-06 08:28:49 -0400504// static
505void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
506 const gl::InputLayout &inputLayout,
507 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700508{
Jamie Madillbd136f92015-08-10 14:51:37 -0400509 signatureOut->resize(inputLayout.size());
Jamie Madilld3dfda22015-07-06 08:28:49 -0400510
511 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700512 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400513 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbd136f92015-08-10 14:51:37 -0400514 bool converted = false;
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400515 if (vertexFormatType != gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700516 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400517 VertexConversionType conversionType =
518 renderer->getVertexConversionType(vertexFormatType);
Jamie Madillbd136f92015-08-10 14:51:37 -0400519 converted = ((conversionType & VERTEX_CONVERT_GPU) != 0);
Brandon Joneseb994362014-09-24 10:27:28 -0700520 }
Jamie Madillbd136f92015-08-10 14:51:37 -0400521
522 (*signatureOut)[index] = converted;
Brandon Joneseb994362014-09-24 10:27:28 -0700523 }
Brandon Joneseb994362014-09-24 10:27:28 -0700524}
525
Jamie Madilld3dfda22015-07-06 08:28:49 -0400526bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
527{
Jamie Madillbd136f92015-08-10 14:51:37 -0400528 size_t limit = std::max(mSignature.size(), signature.size());
529 for (size_t index = 0; index < limit; ++index)
530 {
531 // treat undefined indexes as 'not converted'
532 bool a = index < signature.size() ? signature[index] : false;
533 bool b = index < mSignature.size() ? mSignature[index] : false;
534 if (a != b)
535 return false;
536 }
537
538 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400539}
540
541ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
542 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400543 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700544{
545}
546
547ProgramD3D::PixelExecutable::~PixelExecutable()
548{
549 SafeDelete(mShaderExecutable);
550}
551
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700552ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
553{
554}
555
Geoff Lang7dd2e102014-11-10 15:19:26 -0500556unsigned int ProgramD3D::mCurrentSerial = 1;
557
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400558ProgramD3D::ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer)
559 : ProgramImpl(data),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700560 mRenderer(renderer),
561 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400562 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700563 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400564 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700565 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700566 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700567 mUsedVertexSamplerRange(0),
568 mUsedPixelSamplerRange(0),
569 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500570 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700571{
Brandon Joneseb994362014-09-24 10:27:28 -0700572 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700573}
574
575ProgramD3D::~ProgramD3D()
576{
577 reset();
578 SafeDelete(mDynamicHLSL);
579}
580
Brandon Jones44151a92014-09-10 11:32:25 -0700581bool ProgramD3D::usesPointSpriteEmulation() const
582{
583 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
584}
585
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400586bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700587{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400588 if (drawMode != GL_POINTS)
589 {
590 return mUsesFlatInterpolation;
591 }
592
Cooper Partine6664f02015-01-09 16:22:24 -0800593 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
594}
595
596bool ProgramD3D::usesInstancedPointSpriteEmulation() const
597{
598 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700599}
600
Jamie Madill334d6152015-10-22 14:00:28 -0400601GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
602 unsigned int samplerIndex,
603 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700604{
605 GLint logicalTextureUnit = -1;
606
607 switch (type)
608 {
Jamie Madill334d6152015-10-22 14:00:28 -0400609 case gl::SAMPLER_PIXEL:
610 ASSERT(samplerIndex < caps.maxTextureImageUnits);
611 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
612 {
613 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
614 }
615 break;
616 case gl::SAMPLER_VERTEX:
617 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
618 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
619 {
620 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
621 }
622 break;
623 default:
624 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700625 }
626
Jamie Madill334d6152015-10-22 14:00:28 -0400627 if (logicalTextureUnit >= 0 &&
628 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700629 {
630 return logicalTextureUnit;
631 }
632
633 return -1;
634}
635
636// Returns the texture type for a given Direct3D 9 sampler type and
637// index (0-15 for the pixel shader and 0-3 for the vertex shader).
638GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
639{
640 switch (type)
641 {
Jamie Madill334d6152015-10-22 14:00:28 -0400642 case gl::SAMPLER_PIXEL:
643 ASSERT(samplerIndex < mSamplersPS.size());
644 ASSERT(mSamplersPS[samplerIndex].active);
645 return mSamplersPS[samplerIndex].textureType;
646 case gl::SAMPLER_VERTEX:
647 ASSERT(samplerIndex < mSamplersVS.size());
648 ASSERT(mSamplersVS[samplerIndex].active);
649 return mSamplersVS[samplerIndex].textureType;
650 default:
651 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700652 }
653
654 return GL_TEXTURE_2D;
655}
656
657GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
658{
659 switch (type)
660 {
Jamie Madill334d6152015-10-22 14:00:28 -0400661 case gl::SAMPLER_PIXEL:
662 return mUsedPixelSamplerRange;
663 case gl::SAMPLER_VERTEX:
664 return mUsedVertexSamplerRange;
665 default:
666 UNREACHABLE();
667 return 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700668 }
669}
670
671void ProgramD3D::updateSamplerMapping()
672{
673 if (!mDirtySamplerMapping)
674 {
675 return;
676 }
677
678 mDirtySamplerMapping = false;
679
680 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400681 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700682 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400683 if (!d3dUniform->dirty)
684 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700685
Jamie Madill62d31cb2015-09-11 13:25:51 -0400686 if (!d3dUniform->isSampler())
687 continue;
688
689 int count = d3dUniform->elementCount();
690 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
691
692 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700693 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400694 unsigned int firstIndex = d3dUniform->psRegisterIndex;
695
696 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700697 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400698 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700699
Jamie Madill62d31cb2015-09-11 13:25:51 -0400700 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700701 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400702 ASSERT(mSamplersPS[samplerIndex].active);
703 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700704 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400705 }
706 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700707
Jamie Madill62d31cb2015-09-11 13:25:51 -0400708 if (d3dUniform->isReferencedByVertexShader())
709 {
710 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
711
712 for (int i = 0; i < count; i++)
713 {
714 unsigned int samplerIndex = firstIndex + i;
715
716 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700717 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400718 ASSERT(mSamplersVS[samplerIndex].active);
719 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700720 }
721 }
722 }
723 }
724}
725
Geoff Lang7dd2e102014-11-10 15:19:26 -0500726LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700727{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400728 reset();
729
Jamie Madill334d6152015-10-22 14:00:28 -0400730 DeviceIdentifier binaryDeviceIdentifier = {0};
731 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
732 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700733
734 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
735 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
736 {
737 infoLog << "Invalid program binary, device configuration has changed.";
738 return LinkResult(false, gl::Error(GL_NO_ERROR));
739 }
740
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500741 int compileFlags = stream->readInt<int>();
742 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
743 {
Jamie Madillf6113162015-05-07 11:49:21 -0400744 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500745 return LinkResult(false, gl::Error(GL_NO_ERROR));
746 }
747
Jamie Madill63805b42015-08-25 13:17:39 -0400748 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
749 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
750 {
751 stream->readInt(&mSemanticIndexes[i]);
752 }
753
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700754 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
755 for (unsigned int i = 0; i < psSamplerCount; ++i)
756 {
757 Sampler sampler;
758 stream->readBool(&sampler.active);
759 stream->readInt(&sampler.logicalTextureUnit);
760 stream->readInt(&sampler.textureType);
761 mSamplersPS.push_back(sampler);
762 }
763 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
764 for (unsigned int i = 0; i < vsSamplerCount; ++i)
765 {
766 Sampler sampler;
767 stream->readBool(&sampler.active);
768 stream->readInt(&sampler.logicalTextureUnit);
769 stream->readInt(&sampler.textureType);
770 mSamplersVS.push_back(sampler);
771 }
772
773 stream->readInt(&mUsedVertexSamplerRange);
774 stream->readInt(&mUsedPixelSamplerRange);
775
776 const unsigned int uniformCount = stream->readInt<unsigned int>();
777 if (stream->error())
778 {
Jamie Madillf6113162015-05-07 11:49:21 -0400779 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500780 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700781 }
782
Jamie Madill62d31cb2015-09-11 13:25:51 -0400783 const auto &linkedUniforms = mData.getUniforms();
784 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700785 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
786 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400787 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700788
Jamie Madill62d31cb2015-09-11 13:25:51 -0400789 D3DUniform *d3dUniform =
790 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
791 linkedUniform.isInDefaultBlock());
792 stream->readInt(&d3dUniform->psRegisterIndex);
793 stream->readInt(&d3dUniform->vsRegisterIndex);
794 stream->readInt(&d3dUniform->registerCount);
795 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700796
Jamie Madill62d31cb2015-09-11 13:25:51 -0400797 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700798 }
799
Jamie Madill4a3c2342015-10-08 12:58:45 -0400800 const unsigned int blockCount = stream->readInt<unsigned int>();
801 if (stream->error())
802 {
803 infoLog << "Invalid program binary.";
804 return LinkResult(false, gl::Error(GL_NO_ERROR));
805 }
806
807 ASSERT(mD3DUniformBlocks.empty());
808 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
809 {
810 D3DUniformBlock uniformBlock;
811 stream->readInt(&uniformBlock.psRegisterIndex);
812 stream->readInt(&uniformBlock.vsRegisterIndex);
813 mD3DUniformBlocks.push_back(uniformBlock);
814 }
815
Jamie Madill9fc36822015-11-18 13:08:07 -0500816 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
817 mStreamOutVaryings.resize(streamOutVaryingCount);
818 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700819 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500820 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700821
Jamie Madill28afae52015-11-09 15:07:57 -0500822 stream->readString(&varying->semanticName);
823 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500824 stream->readInt(&varying->componentCount);
825 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700826 }
827
Brandon Jones22502d52014-08-29 16:58:36 -0700828 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400829 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
830 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700831 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400832 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
833 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700834 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700835 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400836 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700837
838 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
839 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400840 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
841 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700842 {
843 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
844 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
845 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
846 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
847 }
848
Jamie Madill4e31ad52015-10-29 10:32:57 -0400849 stream->readString(&mGeometryShaderPreamble);
850
Jamie Madill334d6152015-10-22 14:00:28 -0400851 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700852
853 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400854 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
855 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700856 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400857 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400858 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700859
Jamie Madilld3dfda22015-07-06 08:28:49 -0400860 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700861 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400862 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700863 }
864
Jamie Madill334d6152015-10-22 14:00:28 -0400865 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700866 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400867
Jamie Madillada9ecc2015-08-17 12:53:37 -0400868 ShaderExecutableD3D *shaderExecutable = nullptr;
869
870 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500871 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400872 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400873 if (error.isError())
874 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500875 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400876 }
877
Brandon Joneseb994362014-09-24 10:27:28 -0700878 if (!shaderExecutable)
879 {
Jamie Madillf6113162015-05-07 11:49:21 -0400880 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500881 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700882 }
883
884 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400885 VertexExecutable::Signature signature;
886 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700887
888 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400889 mVertexExecutables.push_back(
890 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700891
892 stream->skip(vertexShaderSize);
893 }
894
895 const size_t pixelShaderCount = stream->readInt<unsigned int>();
896 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
897 {
898 const size_t outputCount = stream->readInt<unsigned int>();
899 std::vector<GLenum> outputs(outputCount);
900 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
901 {
902 stream->readInt(&outputs[outputIndex]);
903 }
904
Jamie Madill334d6152015-10-22 14:00:28 -0400905 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700906 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400907 ShaderExecutableD3D *shaderExecutable = nullptr;
908
909 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500910 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400911 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400912 if (error.isError())
913 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500914 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400915 }
Brandon Joneseb994362014-09-24 10:27:28 -0700916
917 if (!shaderExecutable)
918 {
Jamie Madillf6113162015-05-07 11:49:21 -0400919 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500920 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700921 }
922
923 // add new binary
924 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
925
926 stream->skip(pixelShaderSize);
927 }
928
Jamie Madill4e31ad52015-10-29 10:32:57 -0400929 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
930 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700931 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400932 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
933 if (geometryShaderSize == 0)
934 {
935 mGeometryExecutables[geometryExeIndex] = nullptr;
936 continue;
937 }
938
Brandon Joneseb994362014-09-24 10:27:28 -0700939 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400940 bool splitAttribs = (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
941
Jamie Madill28afae52015-11-09 15:07:57 -0500942 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500943 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, mStreamOutVaryings,
944 splitAttribs, &mGeometryExecutables[geometryExeIndex]);
Geoff Langb543aff2014-09-30 14:52:54 -0400945 if (error.isError())
946 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500947 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400948 }
Brandon Joneseb994362014-09-24 10:27:28 -0700949
Jamie Madill4e31ad52015-10-29 10:32:57 -0400950 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700951 {
Jamie Madillf6113162015-05-07 11:49:21 -0400952 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500953 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700954 }
955 stream->skip(geometryShaderSize);
956 }
957
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700958 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500959 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700960
Geoff Lang7dd2e102014-11-10 15:19:26 -0500961 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700962}
963
Geoff Langb543aff2014-09-30 14:52:54 -0400964gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700965{
Austin Kinross137b1512015-06-17 16:14:53 -0700966 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400967 // When we load the binary again later, we can validate the device identifier before trying to
968 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700969 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400970 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
971 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700972
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500973 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
974
Jamie Madill63805b42015-08-25 13:17:39 -0400975 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
976 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
977 {
978 stream->writeInt(mSemanticIndexes[i]);
979 }
980
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700981 stream->writeInt(mSamplersPS.size());
982 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
983 {
984 stream->writeInt(mSamplersPS[i].active);
985 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
986 stream->writeInt(mSamplersPS[i].textureType);
987 }
988
989 stream->writeInt(mSamplersVS.size());
990 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
991 {
992 stream->writeInt(mSamplersVS[i].active);
993 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
994 stream->writeInt(mSamplersVS[i].textureType);
995 }
996
997 stream->writeInt(mUsedVertexSamplerRange);
998 stream->writeInt(mUsedPixelSamplerRange);
999
Jamie Madill62d31cb2015-09-11 13:25:51 -04001000 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001001 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001002 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001003 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001004 stream->writeInt(uniform->psRegisterIndex);
1005 stream->writeInt(uniform->vsRegisterIndex);
1006 stream->writeInt(uniform->registerCount);
1007 stream->writeInt(uniform->registerElement);
1008 }
1009
1010 stream->writeInt(mD3DUniformBlocks.size());
1011 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1012 {
1013 stream->writeInt(uniformBlock.psRegisterIndex);
1014 stream->writeInt(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001015 }
1016
Jamie Madill9fc36822015-11-18 13:08:07 -05001017 stream->writeInt(mStreamOutVaryings.size());
1018 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001019 {
Brandon Joneseb994362014-09-24 10:27:28 -07001020 stream->writeString(varying.semanticName);
1021 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001022 stream->writeInt(varying.componentCount);
1023 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001024 }
1025
Brandon Jones22502d52014-08-29 16:58:36 -07001026 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001027 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1028 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001029 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001030 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1031 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001032 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001033 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001034 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001035
Brandon Joneseb994362014-09-24 10:27:28 -07001036 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001037 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001038 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1039 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001040 {
Brandon Joneseb994362014-09-24 10:27:28 -07001041 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001042 stream->writeInt(variable.type);
1043 stream->writeString(variable.name);
1044 stream->writeString(variable.source);
1045 stream->writeInt(variable.outputIndex);
1046 }
1047
Jamie Madill4e31ad52015-10-29 10:32:57 -04001048 stream->writeString(mGeometryShaderPreamble);
1049
Brandon Joneseb994362014-09-24 10:27:28 -07001050 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001051 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1052 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001053 {
1054 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1055
Jamie Madilld3dfda22015-07-06 08:28:49 -04001056 const auto &inputLayout = vertexExecutable->inputs();
1057 stream->writeInt(inputLayout.size());
1058
1059 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001060 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001061 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -07001062 }
1063
1064 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1065 stream->writeInt(vertexShaderSize);
1066
1067 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1068 stream->writeBytes(vertexBlob, vertexShaderSize);
1069 }
1070
1071 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001072 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1073 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001074 {
1075 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1076
1077 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1078 stream->writeInt(outputs.size());
1079 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1080 {
1081 stream->writeInt(outputs[outputIndex]);
1082 }
1083
1084 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1085 stream->writeInt(pixelShaderSize);
1086
1087 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1088 stream->writeBytes(pixelBlob, pixelShaderSize);
1089 }
1090
Jamie Madill4e31ad52015-10-29 10:32:57 -04001091 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001092 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001093 if (geometryExe == nullptr)
1094 {
1095 stream->writeInt(0);
1096 continue;
1097 }
1098
1099 size_t geometryShaderSize = geometryExe->getLength();
1100 stream->writeInt(geometryShaderSize);
1101 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001102 }
1103
Geoff Langb543aff2014-09-30 14:52:54 -04001104 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001105}
1106
Geoff Langc5629752015-12-07 16:29:04 -05001107void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1108{
1109}
1110
Jamie Madill334d6152015-10-22 14:00:28 -04001111gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1112 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001113{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001114 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001115
Jamie Madill85a18042015-03-05 15:41:41 -05001116 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill334d6152015-10-22 14:00:28 -04001117 const gl::AttachmentList &colorbuffers =
1118 fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -07001119
1120 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1121 {
1122 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1123
1124 if (colorbuffer)
1125 {
Jamie Madill334d6152015-10-22 14:00:28 -04001126 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1127 ? GL_COLOR_ATTACHMENT0
1128 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001129 }
1130 else
1131 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001132 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001133 }
1134 }
1135
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001136 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001137}
1138
Jamie Madill97399232014-12-23 12:31:15 -05001139gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001140 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001141 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001142{
1143 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1144 {
1145 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1146 {
Geoff Langb543aff2014-09-30 14:52:54 -04001147 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1148 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001149 }
1150 }
1151
Jamie Madill334d6152015-10-22 14:00:28 -04001152 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1153 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001154
1155 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001156 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001157
1158 gl::InfoLog tempInfoLog;
1159 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1160
Jamie Madillada9ecc2015-08-17 12:53:37 -04001161 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001162 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001163 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
1164 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001165 if (error.isError())
1166 {
1167 return error;
1168 }
Brandon Joneseb994362014-09-24 10:27:28 -07001169
Jamie Madill97399232014-12-23 12:31:15 -05001170 if (pixelExecutable)
1171 {
1172 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1173 }
1174 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001175 {
1176 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001177 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001178 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1179 }
Brandon Jones22502d52014-08-29 16:58:36 -07001180
Geoff Langb543aff2014-09-30 14:52:54 -04001181 *outExectuable = pixelExecutable;
1182 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001183}
1184
Jamie Madilld3dfda22015-07-06 08:28:49 -04001185gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001186 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001187 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001188{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001189 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001190
1191 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1192 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001193 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001194 {
Geoff Langb543aff2014-09-30 14:52:54 -04001195 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1196 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001197 }
1198 }
1199
Brandon Jones22502d52014-08-29 16:58:36 -07001200 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001201 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1202 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001203
1204 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001205 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001206
1207 gl::InfoLog tempInfoLog;
1208 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1209
Jamie Madillada9ecc2015-08-17 12:53:37 -04001210 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001211 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001212 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1213 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001214 if (error.isError())
1215 {
1216 return error;
1217 }
1218
Jamie Madill97399232014-12-23 12:31:15 -05001219 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001220 {
Jamie Madill334d6152015-10-22 14:00:28 -04001221 mVertexExecutables.push_back(
1222 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001223 }
Jamie Madill97399232014-12-23 12:31:15 -05001224 else if (!infoLog)
1225 {
1226 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001227 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001228 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1229 }
Brandon Jones22502d52014-08-29 16:58:36 -07001230
Geoff Langb543aff2014-09-30 14:52:54 -04001231 *outExectuable = vertexExecutable;
1232 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001233}
1234
Jamie Madill4e31ad52015-10-29 10:32:57 -04001235gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Data &data,
1236 GLenum drawMode,
1237 ShaderExecutableD3D **outExecutable,
1238 gl::InfoLog *infoLog)
1239{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001240 if (outExecutable)
1241 {
1242 *outExecutable = nullptr;
1243 }
1244
1245 // We only uses a geometry shader for point sprite emulation, or for fixing the provoking
1246 // vertex problem. Otherwise, return a null shader.
1247 if (drawMode != GL_POINTS && !mUsesFlatInterpolation)
1248 {
1249 return gl::Error(GL_NO_ERROR);
1250 }
1251
Jamie Madill4e31ad52015-10-29 10:32:57 -04001252 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1253
1254 if (mGeometryExecutables[geometryShaderType] != nullptr)
1255 {
1256 if (outExecutable)
1257 {
1258 *outExecutable = mGeometryExecutables[geometryShaderType];
1259 }
1260 return gl::Error(GL_NO_ERROR);
1261 }
1262
1263 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
1264 geometryShaderType, data, mData, mGeometryShaderPreamble);
1265
1266 gl::InfoLog tempInfoLog;
1267 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1268
1269 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001270 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001271 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
1272 &mGeometryExecutables[geometryShaderType]);
1273
1274 if (!infoLog && error.isError())
1275 {
1276 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1277 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1278 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1279 }
1280
1281 if (outExecutable)
1282 {
1283 *outExecutable = mGeometryExecutables[geometryShaderType];
1284 }
1285 return error;
1286}
1287
1288LinkResult ProgramD3D::compileProgramExecutables(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001289{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001290 const gl::InputLayout &defaultInputLayout =
1291 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001292 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001293 gl::Error error =
1294 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001295 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001296 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001297 return LinkResult(false, error);
1298 }
Austin Kinross434953e2015-02-20 10:49:51 -08001299
Jamie Madill334d6152015-10-22 14:00:28 -04001300 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001301 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001302 error =
1303 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001304 if (error.isError())
1305 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001306 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001307 }
Brandon Jones44151a92014-09-10 11:32:25 -07001308
Jamie Madill4e31ad52015-10-29 10:32:57 -04001309 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill847638a2015-11-20 13:01:41 -05001310 ShaderExecutableD3D *pointGS = nullptr;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001311 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001312 {
Jamie Madill847638a2015-11-20 13:01:41 -05001313 getGeometryExecutableForPrimitiveType(data, GL_POINTS, &pointGS, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001314 }
1315
Jamie Madillca03b352015-09-02 12:38:13 -04001316 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001317
1318 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001319 {
Jamie Madill334d6152015-10-22 14:00:28 -04001320 // Geometry shaders are currently only used internally, so there is no corresponding shader
1321 // object at the interface level. For now the geometry shader debug info is prepended to
1322 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001323 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001324 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001325 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1326 }
1327
1328 if (defaultVertexExecutable)
1329 {
1330 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1331 }
1332
1333 if (defaultPixelExecutable)
1334 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001335 const ShaderD3D *fragmentShaderD3D =
1336 GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001337 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1338 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001339
Jamie Madill847638a2015-11-20 13:01:41 -05001340 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable &&
1341 (!usesGeometryShader(GL_POINTS) || pointGS));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001342 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001343}
1344
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001345LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001346{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001347 reset();
1348
1349 // TODO(jmadill): structures containing samplers
1350 for (const gl::LinkedUniform &linkedUniform : mData.getUniforms())
1351 {
1352 if (linkedUniform.isSampler() && linkedUniform.isField())
1353 {
1354 infoLog << "Structures containing samplers not currently supported in D3D.";
1355 return LinkResult(false, gl::Error(GL_NO_ERROR));
1356 }
1357 }
1358
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001359 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1360 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001361
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001362 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1363 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1364
Jamie Madill63069df2015-09-01 17:26:41 +00001365 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001366 mSamplersPS.resize(data.caps->maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001367
Arun Patole44efa0b2015-03-04 17:11:05 +05301368 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001369 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1370
Austin Kinross02df7962015-07-01 10:03:42 -07001371 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1372 {
1373 if (fragmentShaderD3D->usesFrontFacing())
1374 {
1375 infoLog << "The current renderer doesn't support gl_FrontFacing";
1376 return LinkResult(false, gl::Error(GL_NO_ERROR));
1377 }
1378 }
1379
Jamie Madillca03b352015-09-02 12:38:13 -04001380 std::vector<PackedVarying> packedVaryings =
1381 MergeVaryings(*vertexShader, *fragmentShader, mData.getTransformFeedbackVaryingNames());
1382
Brandon Jones22502d52014-08-29 16:58:36 -07001383 // Map the varyings to the register file
Jamie Madill9fc36822015-11-18 13:08:07 -05001384 VaryingPacking varyingPacking(data.caps->maxVaryingVectors);
1385 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
1386 mData.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001387 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001388 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001389 }
1390
Jamie Madille39a3f02015-11-17 20:42:15 -05001391 ProgramD3DMetadata metadata(mRenderer->getMajorShaderModel(), mRenderer->getShaderModelSuffix(),
1392 usesInstancedPointSpriteEmulation(), vertexShaderD3D,
1393 fragmentShaderD3D);
1394
Jamie Madill9fc36822015-11-18 13:08:07 -05001395 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1396 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1397
1398 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.caps->maxVaryingVectors)
1399 {
1400 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1401 return LinkResult(false, gl::Error(GL_NO_ERROR));
1402 }
1403
1404 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1405 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1406 // intelligently, but D3D9 assumes one semantic per register.
1407 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1408 varyingPacking.getMaxSemanticIndex() > data.caps->maxVaryingVectors)
1409 {
1410 infoLog << "Cannot pack these varyings on D3D9.";
1411 return LinkResult(false, gl::Error(GL_NO_ERROR));
1412 }
1413
1414 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mData, metadata, varyingPacking, &mPixelHLSL,
1415 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001416 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001418 }
1419
Brandon Jones44151a92014-09-10 11:32:25 -07001420 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madille39a3f02015-11-17 20:42:15 -05001421 mDynamicHLSL->getPixelShaderOutputKey(data, mData, metadata, &mPixelShaderKey);
1422 mUsesFragDepth = metadata.usesFragDepth(mData);
Brandon Jones44151a92014-09-10 11:32:25 -07001423
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001424 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001425 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001426 for (const auto &varying : packedVaryings)
1427 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001428 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001429 {
1430 mUsesFlatInterpolation = true;
1431 break;
1432 }
1433 }
1434
Jamie Madill4e31ad52015-10-29 10:32:57 -04001435 if (mRenderer->getMajorShaderModel() >= 4)
1436 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001437 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1438 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001439 }
1440
Jamie Madill63805b42015-08-25 13:17:39 -04001441 initSemanticIndex();
Jamie Madill437d2662014-12-05 14:23:35 -05001442
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001443 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001444
Jamie Madill9fc36822015-11-18 13:08:07 -05001445 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001446
Jamie Madill4e31ad52015-10-29 10:32:57 -04001447 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001448 if (result.error.isError() || !result.linkSuccess)
1449 {
1450 infoLog << "Failed to create D3D shaders.";
1451 return result;
1452 }
1453
Jamie Madill4a3c2342015-10-08 12:58:45 -04001454 initUniformBlockInfo();
1455
Geoff Lang7dd2e102014-11-10 15:19:26 -05001456 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001457}
1458
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001459GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001460{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001461 // TODO(jmadill): Do something useful here?
1462 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001463}
1464
Jamie Madill4a3c2342015-10-08 12:58:45 -04001465void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001466{
1467 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1468
Jamie Madill62d31cb2015-09-11 13:25:51 -04001469 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1470 {
1471 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1472 continue;
1473
Jamie Madill4a3c2342015-10-08 12:58:45 -04001474 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001475 continue;
1476
Jamie Madill4a3c2342015-10-08 12:58:45 -04001477 size_t dataSize = getUniformBlockInfo(vertexBlock);
1478 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001479 }
1480
1481 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1482
1483 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1484 {
1485 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1486 continue;
1487
Jamie Madill4a3c2342015-10-08 12:58:45 -04001488 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001489 continue;
1490
Jamie Madill4a3c2342015-10-08 12:58:45 -04001491 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1492 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001493 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001494}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001495
Jamie Madill4a3c2342015-10-08 12:58:45 -04001496void ProgramD3D::assignUniformBlockRegisters()
1497{
1498 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001499
1500 // Assign registers and update sizes.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001501 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1502 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001503
Jamie Madill4a3c2342015-10-08 12:58:45 -04001504 for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001505 {
1506 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1507
Jamie Madill4a3c2342015-10-08 12:58:45 -04001508 D3DUniformBlock d3dUniformBlock;
1509
Jamie Madill62d31cb2015-09-11 13:25:51 -04001510 if (uniformBlock.vertexStaticUse)
1511 {
1512 unsigned int baseRegister =
1513 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001514 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001515 }
1516
1517 if (uniformBlock.fragmentStaticUse)
1518 {
1519 unsigned int baseRegister =
1520 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001521 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001522 }
1523
Jamie Madill4a3c2342015-10-08 12:58:45 -04001524 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001525 }
1526}
1527
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001528void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001529{
1530 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001531 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001532 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001533 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001534 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001535 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001536 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001537 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001538 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001539 vertexRegisters = std::max(vertexRegisters,
1540 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001541 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001542 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001543 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001544 fragmentRegisters = std::max(
1545 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001546 }
1547 }
1548 }
1549
Jamie Madill334d6152015-10-22 14:00:28 -04001550 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001551 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1552}
1553
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001554gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001555{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001556 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001557
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001558 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001559 if (error.isError())
1560 {
1561 return error;
1562 }
1563
Jamie Madill62d31cb2015-09-11 13:25:51 -04001564 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001565 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001566 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001567 }
1568
1569 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001570}
1571
Jamie Madilld1fe1642015-08-21 16:26:04 -04001572gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001573{
Jamie Madill4a3c2342015-10-08 12:58:45 -04001574 if (mData.getUniformBlocks().empty())
1575 {
1576 return gl::Error(GL_NO_ERROR);
1577 }
1578
1579 // Lazy init.
1580 if (mD3DUniformBlocks.empty())
1581 {
1582 assignUniformBlockRegisters();
1583 }
1584
Jamie Madill03260fa2015-06-22 13:57:22 -04001585 mVertexUBOCache.clear();
1586 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001587
1588 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1589 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1590
Jamie Madill4a3c2342015-10-08 12:58:45 -04001591 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001592 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001593 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001594 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
1595 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001596
Brandon Jones18bd4102014-09-22 14:21:44 -07001597 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001598 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001599 {
1600 continue;
1601 }
1602
Jamie Madill4a3c2342015-10-08 12:58:45 -04001603 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001604 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001605 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001606 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001607
Jamie Madill969194d2015-07-20 14:36:56 -04001608 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001609 {
1610 mVertexUBOCache.resize(registerIndex + 1, -1);
1611 }
1612
1613 ASSERT(mVertexUBOCache[registerIndex] == -1);
1614 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001615 }
1616
Jamie Madill4a3c2342015-10-08 12:58:45 -04001617 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001618 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001619 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001620 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001621
1622 if (mFragmentUBOCache.size() <= registerIndex)
1623 {
1624 mFragmentUBOCache.resize(registerIndex + 1, -1);
1625 }
1626
1627 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1628 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001629 }
1630 }
1631
Jamie Madill03260fa2015-06-22 13:57:22 -04001632 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001633}
1634
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001635void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001636{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001637 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001638 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001639 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001640 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001641}
1642
Jamie Madill334d6152015-10-22 14:00:28 -04001643void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001644{
1645 setUniform(location, count, v, GL_FLOAT);
1646}
1647
1648void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1649{
1650 setUniform(location, count, v, GL_FLOAT_VEC2);
1651}
1652
1653void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1654{
1655 setUniform(location, count, v, GL_FLOAT_VEC3);
1656}
1657
1658void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1659{
1660 setUniform(location, count, v, GL_FLOAT_VEC4);
1661}
1662
Jamie Madill334d6152015-10-22 14:00:28 -04001663void ProgramD3D::setUniformMatrix2fv(GLint location,
1664 GLsizei count,
1665 GLboolean transpose,
1666 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001667{
1668 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1669}
1670
Jamie Madill334d6152015-10-22 14:00:28 -04001671void ProgramD3D::setUniformMatrix3fv(GLint location,
1672 GLsizei count,
1673 GLboolean transpose,
1674 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001675{
1676 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1677}
1678
Jamie Madill334d6152015-10-22 14:00:28 -04001679void ProgramD3D::setUniformMatrix4fv(GLint location,
1680 GLsizei count,
1681 GLboolean transpose,
1682 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001683{
1684 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1685}
1686
Jamie Madill334d6152015-10-22 14:00:28 -04001687void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1688 GLsizei count,
1689 GLboolean transpose,
1690 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001691{
1692 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1693}
1694
Jamie Madill334d6152015-10-22 14:00:28 -04001695void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1696 GLsizei count,
1697 GLboolean transpose,
1698 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001699{
1700 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1701}
1702
Jamie Madill334d6152015-10-22 14:00:28 -04001703void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1704 GLsizei count,
1705 GLboolean transpose,
1706 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001707{
1708 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1709}
1710
Jamie Madill334d6152015-10-22 14:00:28 -04001711void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1712 GLsizei count,
1713 GLboolean transpose,
1714 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001715{
1716 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1717}
1718
Jamie Madill334d6152015-10-22 14:00:28 -04001719void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1720 GLsizei count,
1721 GLboolean transpose,
1722 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001723{
1724 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1725}
1726
Jamie Madill334d6152015-10-22 14:00:28 -04001727void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1728 GLsizei count,
1729 GLboolean transpose,
1730 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001731{
1732 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1733}
1734
1735void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1736{
1737 setUniform(location, count, v, GL_INT);
1738}
1739
1740void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1741{
1742 setUniform(location, count, v, GL_INT_VEC2);
1743}
1744
1745void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1746{
1747 setUniform(location, count, v, GL_INT_VEC3);
1748}
1749
1750void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1751{
1752 setUniform(location, count, v, GL_INT_VEC4);
1753}
1754
1755void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1756{
1757 setUniform(location, count, v, GL_UNSIGNED_INT);
1758}
1759
1760void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1761{
1762 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1763}
1764
1765void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1766{
1767 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1768}
1769
1770void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1771{
1772 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1773}
1774
Jamie Madill4a3c2342015-10-08 12:58:45 -04001775void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1776 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001777{
1778}
1779
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001780void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001781{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001782 D3DUniformMap uniformMap;
Jamie Madill334d6152015-10-22 14:00:28 -04001783 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001784 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001785
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001786 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001787 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001788 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001789 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001790 }
1791 }
1792
Jamie Madill334d6152015-10-22 14:00:28 -04001793 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001794 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001795 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001796 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001797 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001798 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001799 }
1800 }
1801
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001802 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
1803 for (const gl::LinkedUniform &glUniform : mData.getUniforms())
1804 {
1805 if (!glUniform.isInDefaultBlock())
1806 continue;
1807
1808 auto mapEntry = uniformMap.find(glUniform.name);
1809 ASSERT(mapEntry != uniformMap.end());
1810 mD3DUniforms.push_back(mapEntry->second);
1811 }
1812
Jamie Madill62d31cb2015-09-11 13:25:51 -04001813 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001814 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001815}
1816
Jamie Madill91445bc2015-09-23 16:47:53 -04001817void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001818 const sh::Uniform &uniform,
1819 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001820{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001821 if (uniform.isBuiltIn())
Jamie Madillfb536032015-09-11 13:19:49 -04001822 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001823 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001824 return;
1825 }
1826
Jamie Madill91445bc2015-09-23 16:47:53 -04001827 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1828
1829 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1830 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001831 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001832 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001833
Jamie Madill91445bc2015-09-23 16:47:53 -04001834 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001835}
1836
Jamie Madill62d31cb2015-09-11 13:25:51 -04001837D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1838{
1839 for (D3DUniform *d3dUniform : mD3DUniforms)
1840 {
1841 if (d3dUniform->name == name)
1842 {
1843 return d3dUniform;
1844 }
1845 }
1846
1847 return nullptr;
1848}
1849
Jamie Madill91445bc2015-09-23 16:47:53 -04001850void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001851 const sh::ShaderVariable &uniform,
1852 const std::string &fullName,
1853 sh::HLSLBlockEncoder *encoder,
1854 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001855{
1856 if (uniform.isStruct())
1857 {
1858 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1859 {
1860 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1861
Jamie Madill55def582015-05-04 11:24:57 -04001862 if (encoder)
1863 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001864
1865 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1866 {
Jamie Madill334d6152015-10-22 14:00:28 -04001867 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001868 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1869
Jamie Madill91445bc2015-09-23 16:47:53 -04001870 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001871 }
1872
Jamie Madill55def582015-05-04 11:24:57 -04001873 if (encoder)
1874 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001875 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001876 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001877 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001878
1879 // Not a struct. Arrays are treated as aggregate types.
1880 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001881 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001882 encoder->enterAggregateType();
1883 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001884
Jamie Madill62d31cb2015-09-11 13:25:51 -04001885 // Advance the uniform offset, to track registers allocation for structs
1886 sh::BlockMemberInfo blockInfo =
1887 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1888 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001889
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001890 auto uniformMapEntry = uniformMap->find(fullName);
1891 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001892
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001893 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001894 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001895 d3dUniform = uniformMapEntry->second;
1896 }
1897 else
1898 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001899 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001900 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001901 }
1902
1903 if (encoder)
1904 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001905 d3dUniform->registerElement =
1906 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001907 unsigned int reg =
1908 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001909 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001910 {
1911 d3dUniform->psRegisterIndex = reg;
1912 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001913 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001914 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001915 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001916 d3dUniform->vsRegisterIndex = reg;
1917 }
Jamie Madillfb536032015-09-11 13:19:49 -04001918
1919 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001920 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001921 {
1922 encoder->exitAggregateType();
1923 }
1924 }
1925}
1926
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001927template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001928void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001929{
Jamie Madill334d6152015-10-22 14:00:28 -04001930 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001931 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1932
Jamie Madill62d31cb2015-09-11 13:25:51 -04001933 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001934
Jamie Madill62d31cb2015-09-11 13:25:51 -04001935 unsigned int elementCount = targetUniform->elementCount();
1936 unsigned int arrayElement = mData.getUniformLocations()[location].element;
1937 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001938
1939 if (targetUniform->type == targetUniformType)
1940 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001941 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001942
Jamie Madill62d31cb2015-09-11 13:25:51 -04001943 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001944 {
Jamie Madill334d6152015-10-22 14:00:28 -04001945 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001946 const T *source = v + (i * components);
1947
1948 for (int c = 0; c < components; c++)
1949 {
1950 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1951 }
1952 for (int c = components; c < 4; c++)
1953 {
1954 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1955 }
1956 }
1957 }
1958 else if (targetUniform->type == targetBoolType)
1959 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001960 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001961
Jamie Madill62d31cb2015-09-11 13:25:51 -04001962 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001963 {
Jamie Madill334d6152015-10-22 14:00:28 -04001964 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001965 const T *source = v + (i * components);
1966
1967 for (int c = 0; c < components; c++)
1968 {
Jamie Madill334d6152015-10-22 14:00:28 -04001969 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1970 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001971 }
1972 for (int c = components; c < 4; c++)
1973 {
1974 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1975 }
1976 }
1977 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001978 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001979 {
1980 ASSERT(targetUniformType == GL_INT);
1981
Jamie Madill62d31cb2015-09-11 13:25:51 -04001982 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001983
1984 bool wasDirty = targetUniform->dirty;
1985
Jamie Madill62d31cb2015-09-11 13:25:51 -04001986 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001987 {
Jamie Madill334d6152015-10-22 14:00:28 -04001988 GLint *dest = target + (i * 4);
1989 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001990
1991 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1992 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1993 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1994 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1995 }
1996
1997 if (!wasDirty && targetUniform->dirty)
1998 {
1999 mDirtySamplerMapping = true;
2000 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002001 }
Jamie Madill334d6152015-10-22 14:00:28 -04002002 else
2003 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002004}
2005
2006template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002007void ProgramD3D::setUniformMatrixfv(GLint location,
2008 GLsizei countIn,
2009 GLboolean transpose,
2010 const GLfloat *value,
2011 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002012{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002013 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002014
Jamie Madill62d31cb2015-09-11 13:25:51 -04002015 unsigned int elementCount = targetUniform->elementCount();
2016 unsigned int arrayElement = mData.getUniformLocations()[location].element;
2017 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002019 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002020 GLfloat *target =
2021 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002022
Jamie Madill62d31cb2015-09-11 13:25:51 -04002023 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002024 {
2025 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2026 if (transpose == GL_FALSE)
2027 {
Jamie Madill334d6152015-10-22 14:00:28 -04002028 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2029 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002030 }
2031 else
2032 {
Jamie Madill334d6152015-10-22 14:00:28 -04002033 targetUniform->dirty =
2034 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002035 }
2036 target += targetMatrixStride;
2037 value += cols * rows;
2038 }
2039}
2040
Jamie Madill4a3c2342015-10-08 12:58:45 -04002041size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002042{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002043 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002044
Jamie Madill62d31cb2015-09-11 13:25:51 -04002045 // define member uniforms
2046 sh::Std140BlockEncoder std140Encoder;
2047 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2048 sh::BlockLayoutEncoder *encoder = nullptr;
2049
2050 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002051 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002052 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002053 }
2054 else
2055 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002056 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002057 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002058
2059 GetUniformBlockInfo(interfaceBlock.fields, "", encoder, interfaceBlock.isRowMajorLayout,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002060 &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002061
2062 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002063}
2064
Jamie Madill62d31cb2015-09-11 13:25:51 -04002065void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002066{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002067 for (const D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002068 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002069 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002070 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002071 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002072 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002073 }
2074}
2075
Jamie Madill62d31cb2015-09-11 13:25:51 -04002076void ProgramD3D::assignSamplerRegisters(const D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002077{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002078 ASSERT(d3dUniform->isSampler());
2079 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX ||
2080 d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madillfb536032015-09-11 13:19:49 -04002081
Jamie Madill62d31cb2015-09-11 13:25:51 -04002082 if (d3dUniform->vsRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002083 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002084 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2085 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002086 }
2087
Jamie Madill62d31cb2015-09-11 13:25:51 -04002088 if (d3dUniform->psRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002089 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002090 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2091 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002092 }
2093}
2094
Jamie Madill62d31cb2015-09-11 13:25:51 -04002095// static
2096void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002097 GLenum samplerType,
2098 unsigned int samplerCount,
2099 std::vector<Sampler> &outSamplers,
2100 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002101{
2102 unsigned int samplerIndex = startSamplerIndex;
2103
2104 do
2105 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002106 ASSERT(samplerIndex < outSamplers.size());
2107 Sampler *sampler = &outSamplers[samplerIndex];
2108 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002109 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002110 sampler->logicalTextureUnit = 0;
2111 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002112 samplerIndex++;
2113 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002114}
2115
Brandon Jonesc9610c52014-08-25 17:02:59 -07002116void ProgramD3D::reset()
2117{
Brandon Joneseb994362014-09-24 10:27:28 -07002118 SafeDeleteContainer(mVertexExecutables);
2119 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002120
2121 for (auto &element : mGeometryExecutables)
2122 {
2123 SafeDelete(element);
2124 }
Brandon Joneseb994362014-09-24 10:27:28 -07002125
Brandon Jones22502d52014-08-29 16:58:36 -07002126 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002127 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002128
2129 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002130 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002131 mUsesFragDepth = false;
2132 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002133 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002134 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002135
Jamie Madill62d31cb2015-09-11 13:25:51 -04002136 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002137 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002138
Brandon Jonesc9610c52014-08-25 17:02:59 -07002139 SafeDelete(mVertexUniformStorage);
2140 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002141
2142 mSamplersPS.clear();
2143 mSamplersVS.clear();
2144
2145 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002146 mUsedPixelSamplerRange = 0;
2147 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002148
Jamie Madill63805b42015-08-25 13:17:39 -04002149 std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1);
Jamie Madill437d2662014-12-05 14:23:35 -05002150 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002151
Jamie Madill9fc36822015-11-18 13:08:07 -05002152 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002153
2154 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002155}
2156
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157unsigned int ProgramD3D::getSerial() const
2158{
2159 return mSerial;
2160}
2161
2162unsigned int ProgramD3D::issueSerial()
2163{
2164 return mCurrentSerial++;
2165}
2166
Jamie Madill63805b42015-08-25 13:17:39 -04002167void ProgramD3D::initSemanticIndex()
2168{
2169 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2170 ASSERT(vertexShader != nullptr);
2171
2172 // Init semantic index
2173 for (const sh::Attribute &attribute : mData.getAttributes())
2174 {
2175 int attributeIndex = attribute.location;
2176 int index = vertexShader->getSemanticIndex(attribute.name);
2177 int regs = gl::VariableRegisterCount(attribute.type);
2178
2179 for (int reg = 0; reg < regs; ++reg)
2180 {
2181 mSemanticIndexes[attributeIndex + reg] = index + reg;
2182 }
2183 }
2184
2185 initAttributesByLayout();
2186}
2187
Jamie Madill437d2662014-12-05 14:23:35 -05002188void ProgramD3D::initAttributesByLayout()
2189{
2190 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2191 {
2192 mAttributesByLayout[i] = i;
2193 }
2194
Jamie Madill63805b42015-08-25 13:17:39 -04002195 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS],
2196 AttributeSorter(mSemanticIndexes));
Jamie Madill437d2662014-12-05 14:23:35 -05002197}
2198
Jamie Madill334d6152015-10-22 14:00:28 -04002199void ProgramD3D::sortAttributesByLayout(
2200 const std::vector<TranslatedAttribute> &unsortedAttributes,
2201 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2202 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002203{
Jamie Madill476682e2015-06-30 10:04:29 -04002204 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002205 {
Jamie Madill334d6152015-10-22 14:00:28 -04002206 int oldIndex = mAttributesByLayout[attribIndex];
Jamie Madill63805b42015-08-25 13:17:39 -04002207 sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex];
Jamie Madill334d6152015-10-22 14:00:28 -04002208 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002209 }
2210}
2211
Jamie Madill63805b42015-08-25 13:17:39 -04002212void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002213{
Jamie Madillbd136f92015-08-10 14:51:37 -04002214 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002215 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002216
Dian Xianga4928832015-09-15 10:11:17 -07002217 for (unsigned int attributeIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002218 {
Jamie Madill63805b42015-08-25 13:17:39 -04002219 int semanticIndex = mSemanticIndexes[attributeIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002220
2221 if (semanticIndex != -1)
2222 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002223 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2224 {
2225 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2226 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002227 mCachedInputLayout[semanticIndex] =
2228 GetVertexFormatType(vertexAttributes[attributeIndex],
2229 state.getVertexAttribCurrentValue(attributeIndex).Type);
2230 }
2231 }
2232}
2233
Jamie Madill9fc36822015-11-18 13:08:07 -05002234void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002235{
Jamie Madill9fc36822015-11-18 13:08:07 -05002236 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2237
2238 const std::string &varyingSemantic =
2239 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2240
Jamie Madillccdf74b2015-08-18 10:46:12 -04002241 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002242 mStreamOutVaryings.clear();
2243
2244 const auto &tfVaryingNames = mData.getTransformFeedbackVaryingNames();
2245 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2246 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002247 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002248 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2249 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002250 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002251 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002252 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002253 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2254 builtins.glPosition.index, 4, outputSlot));
2255 }
2256 }
2257 else if (tfVaryingName == "gl_FragCoord")
2258 {
2259 if (builtins.glFragCoord.enabled)
2260 {
2261 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2262 builtins.glFragCoord.index, 4, outputSlot));
2263 }
2264 }
2265 else if (tfVaryingName == "gl_PointSize")
2266 {
2267 if (builtins.glPointSize.enabled)
2268 {
2269 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2270 }
2271 }
2272 else
2273 {
2274 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2275 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002276 const auto &varying = *registerInfo.packedVarying->varying;
2277 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002278 int componentCount = gl::VariableColumnCount(transposedType);
2279 ASSERT(!varying.isBuiltIn());
2280
Jamie Madill55c25d02015-11-18 13:08:08 -05002281 // Transform feedback for varying structs is underspecified.
2282 // See Khronos bug 9856.
2283 // TODO(jmadill): Figure out how to be spec-compliant here.
2284 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2285 continue;
2286
Jamie Madill9fc36822015-11-18 13:08:07 -05002287 // There can be more than one register assigned to a particular varying, and each
2288 // register needs its own stream out entry.
2289 if (tfVaryingName == varying.name)
2290 {
2291 mStreamOutVaryings.push_back(D3DVarying(
2292 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2293 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002294 }
2295 }
2296 }
2297}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002298
2299D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2300{
2301 return mD3DUniforms[mData.getUniformLocations()[location].index];
2302}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002303
2304bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2305{
2306 std::string baseName = blockName;
2307 gl::ParseAndStripArrayIndex(&baseName);
2308
2309 auto sizeIter = mBlockDataSizes.find(baseName);
2310 if (sizeIter == mBlockDataSizes.end())
2311 {
2312 *sizeOut = 0;
2313 return false;
2314 }
2315
2316 *sizeOut = sizeIter->second;
2317 return true;
2318}
2319
2320bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2321 sh::BlockMemberInfo *memberInfoOut) const
2322{
2323 auto infoIter = mBlockInfo.find(memberUniformName);
2324 if (infoIter == mBlockInfo.end())
2325 {
2326 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2327 return false;
2328 }
2329
2330 *memberInfoOut = infoIter->second;
2331 return true;
2332}
Brandon Jonesc9610c52014-08-25 17:02:59 -07002333}