blob: 7586a7e2ad63eecaa510a1578415a0de89879ed3 [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,
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800404 bool usesViewScale,
Jamie Madille39a3f02015-11-17 20:42:15 -0500405 const ShaderD3D *vertexShader,
406 const ShaderD3D *fragmentShader)
407 : mRendererMajorShaderModel(rendererMajorShaderModel),
408 mShaderModelSuffix(shaderModelSuffix),
409 mUsesInstancedPointSpriteEmulation(usesInstancedPointSpriteEmulation),
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800410 mUsesViewScale(usesViewScale),
Jamie Madille39a3f02015-11-17 20:42:15 -0500411 mVertexShader(vertexShader),
412 mFragmentShader(fragmentShader)
413{
414}
415
416int ProgramD3DMetadata::getRendererMajorShaderModel() const
417{
418 return mRendererMajorShaderModel;
419}
420
421bool ProgramD3DMetadata::usesBroadcast(const gl::Data &data) const
422{
423 return (mFragmentShader->usesFragColor() && data.clientVersion < 3);
424}
425
426bool ProgramD3DMetadata::usesFragDepth(const gl::Program::Data &programData) const
427{
Jamie Madill63286672015-11-24 13:00:08 -0500428 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500429}
430
431bool ProgramD3DMetadata::usesPointCoord() const
432{
433 return mFragmentShader->usesPointCoord();
434}
435
436bool ProgramD3DMetadata::usesFragCoord() const
437{
438 return mFragmentShader->usesFragCoord();
439}
440
441bool ProgramD3DMetadata::usesPointSize() const
442{
443 return mVertexShader->usesPointSize();
444}
445
446bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
447{
448 return !usesPointSize() && usesPointCoord() && mRendererMajorShaderModel >= 4;
449}
450
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800451bool ProgramD3DMetadata::usesViewScale() const
452{
453 return mUsesViewScale;
454}
455
Jamie Madille39a3f02015-11-17 20:42:15 -0500456bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
457{
458 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader
459 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
460 // GeometryShader PointSprite emulation does not require this additional entry because the
461 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the
462 // PS_INPUT of the generated pixel shader. The Geometry Shader point sprite implementation needs
463 // gl_PointSize to be in VS_OUTPUT and GS_INPUT. Instanced point sprites doesn't need
464 // gl_PointSize in VS_OUTPUT.
465 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
466 usesInsertedPointCoordValue();
467}
468
469bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
470{
471 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
472 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
473 // the vertex shader in this case. This saves us 1 output vector.
474 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
475}
476
477bool ProgramD3DMetadata::usesSystemValuePointSize() const
478{
479 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
480}
481
482bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
483{
484 return mFragmentShader->usesMultipleRenderTargets();
485}
486
487GLint ProgramD3DMetadata::getMajorShaderVersion() const
488{
489 return mVertexShader->getData().getShaderVersion();
490}
491
492const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
493{
494 return mFragmentShader;
495}
496
Jamie Madill28afae52015-11-09 15:07:57 -0500497// ProgramD3D Implementation
498
Jamie Madilld3dfda22015-07-06 08:28:49 -0400499ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
500 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500501 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400502 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700503{
Brandon Joneseb994362014-09-24 10:27:28 -0700504}
505
506ProgramD3D::VertexExecutable::~VertexExecutable()
507{
508 SafeDelete(mShaderExecutable);
509}
510
Jamie Madilld3dfda22015-07-06 08:28:49 -0400511// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500512ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
513 GLenum type)
514{
515 switch (type)
516 {
517 case GL_INT:
518 return HLSLAttribType::SIGNED_INT;
519 case GL_UNSIGNED_INT:
520 return HLSLAttribType::UNSIGNED_INT;
521 case GL_SIGNED_NORMALIZED:
522 case GL_UNSIGNED_NORMALIZED:
523 case GL_FLOAT:
524 return HLSLAttribType::FLOAT;
525 default:
526 UNREACHABLE();
527 return HLSLAttribType::FLOAT;
528 }
529}
530
531// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400532void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
533 const gl::InputLayout &inputLayout,
534 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700535{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500536 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400537
538 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700539 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400540 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500541 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
542 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400543
Jamie Madillbdec2f42016-03-02 16:35:32 -0500544 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
545 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
546 continue;
547
548 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
549 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700550 }
Brandon Joneseb994362014-09-24 10:27:28 -0700551}
552
Jamie Madilld3dfda22015-07-06 08:28:49 -0400553bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
554{
Jamie Madillbd136f92015-08-10 14:51:37 -0400555 size_t limit = std::max(mSignature.size(), signature.size());
556 for (size_t index = 0; index < limit; ++index)
557 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500558 // treat undefined indexes as FLOAT
559 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
560 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400561 if (a != b)
562 return false;
563 }
564
565 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400566}
567
568ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
569 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400570 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700571{
572}
573
574ProgramD3D::PixelExecutable::~PixelExecutable()
575{
576 SafeDelete(mShaderExecutable);
577}
578
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700579ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
580{
581}
582
Geoff Lang7dd2e102014-11-10 15:19:26 -0500583unsigned int ProgramD3D::mCurrentSerial = 1;
584
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400585ProgramD3D::ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer)
586 : ProgramImpl(data),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700587 mRenderer(renderer),
588 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400589 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700590 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400591 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700592 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700593 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700594 mUsedVertexSamplerRange(0),
595 mUsedPixelSamplerRange(0),
596 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500597 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700598{
Brandon Joneseb994362014-09-24 10:27:28 -0700599 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700600}
601
602ProgramD3D::~ProgramD3D()
603{
604 reset();
605 SafeDelete(mDynamicHLSL);
606}
607
Brandon Jones44151a92014-09-10 11:32:25 -0700608bool ProgramD3D::usesPointSpriteEmulation() const
609{
610 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
611}
612
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400613bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700614{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400615 if (drawMode != GL_POINTS)
616 {
617 return mUsesFlatInterpolation;
618 }
619
Cooper Partine6664f02015-01-09 16:22:24 -0800620 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
621}
622
623bool ProgramD3D::usesInstancedPointSpriteEmulation() const
624{
625 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700626}
627
Jamie Madill334d6152015-10-22 14:00:28 -0400628GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
629 unsigned int samplerIndex,
630 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700631{
632 GLint logicalTextureUnit = -1;
633
634 switch (type)
635 {
Jamie Madill334d6152015-10-22 14:00:28 -0400636 case gl::SAMPLER_PIXEL:
637 ASSERT(samplerIndex < caps.maxTextureImageUnits);
638 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
639 {
640 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
641 }
642 break;
643 case gl::SAMPLER_VERTEX:
644 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
645 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
646 {
647 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
648 }
649 break;
650 default:
651 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700652 }
653
Jamie Madill334d6152015-10-22 14:00:28 -0400654 if (logicalTextureUnit >= 0 &&
655 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700656 {
657 return logicalTextureUnit;
658 }
659
660 return -1;
661}
662
663// Returns the texture type for a given Direct3D 9 sampler type and
664// index (0-15 for the pixel shader and 0-3 for the vertex shader).
665GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
666{
667 switch (type)
668 {
Jamie Madill334d6152015-10-22 14:00:28 -0400669 case gl::SAMPLER_PIXEL:
670 ASSERT(samplerIndex < mSamplersPS.size());
671 ASSERT(mSamplersPS[samplerIndex].active);
672 return mSamplersPS[samplerIndex].textureType;
673 case gl::SAMPLER_VERTEX:
674 ASSERT(samplerIndex < mSamplersVS.size());
675 ASSERT(mSamplersVS[samplerIndex].active);
676 return mSamplersVS[samplerIndex].textureType;
677 default:
678 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700679 }
680
681 return GL_TEXTURE_2D;
682}
683
Olli Etuaho618bebc2016-01-15 16:40:00 +0200684GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700685{
686 switch (type)
687 {
Jamie Madill334d6152015-10-22 14:00:28 -0400688 case gl::SAMPLER_PIXEL:
689 return mUsedPixelSamplerRange;
690 case gl::SAMPLER_VERTEX:
691 return mUsedVertexSamplerRange;
692 default:
693 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200694 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700695 }
696}
697
698void ProgramD3D::updateSamplerMapping()
699{
700 if (!mDirtySamplerMapping)
701 {
702 return;
703 }
704
705 mDirtySamplerMapping = false;
706
707 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400708 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700709 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400710 if (!d3dUniform->dirty)
711 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700712
Jamie Madill62d31cb2015-09-11 13:25:51 -0400713 if (!d3dUniform->isSampler())
714 continue;
715
716 int count = d3dUniform->elementCount();
717 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
718
719 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700720 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400721 unsigned int firstIndex = d3dUniform->psRegisterIndex;
722
723 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700724 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400725 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700726
Jamie Madill62d31cb2015-09-11 13:25:51 -0400727 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700728 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400729 ASSERT(mSamplersPS[samplerIndex].active);
730 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700731 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400732 }
733 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700734
Jamie Madill62d31cb2015-09-11 13:25:51 -0400735 if (d3dUniform->isReferencedByVertexShader())
736 {
737 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
738
739 for (int i = 0; i < count; i++)
740 {
741 unsigned int samplerIndex = firstIndex + i;
742
743 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700744 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400745 ASSERT(mSamplersVS[samplerIndex].active);
746 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700747 }
748 }
749 }
750 }
751}
752
Geoff Lang7dd2e102014-11-10 15:19:26 -0500753LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700754{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400755 reset();
756
Jamie Madill334d6152015-10-22 14:00:28 -0400757 DeviceIdentifier binaryDeviceIdentifier = {0};
758 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
759 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700760
761 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
762 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
763 {
764 infoLog << "Invalid program binary, device configuration has changed.";
765 return LinkResult(false, gl::Error(GL_NO_ERROR));
766 }
767
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500768 int compileFlags = stream->readInt<int>();
769 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
770 {
Jamie Madillf6113162015-05-07 11:49:21 -0400771 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500772 return LinkResult(false, gl::Error(GL_NO_ERROR));
773 }
774
Jamie Madill63805b42015-08-25 13:17:39 -0400775 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
776 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
777 {
778 stream->readInt(&mSemanticIndexes[i]);
779 }
780
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700781 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
782 for (unsigned int i = 0; i < psSamplerCount; ++i)
783 {
784 Sampler sampler;
785 stream->readBool(&sampler.active);
786 stream->readInt(&sampler.logicalTextureUnit);
787 stream->readInt(&sampler.textureType);
788 mSamplersPS.push_back(sampler);
789 }
790 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
791 for (unsigned int i = 0; i < vsSamplerCount; ++i)
792 {
793 Sampler sampler;
794 stream->readBool(&sampler.active);
795 stream->readInt(&sampler.logicalTextureUnit);
796 stream->readInt(&sampler.textureType);
797 mSamplersVS.push_back(sampler);
798 }
799
800 stream->readInt(&mUsedVertexSamplerRange);
801 stream->readInt(&mUsedPixelSamplerRange);
802
803 const unsigned int uniformCount = stream->readInt<unsigned int>();
804 if (stream->error())
805 {
Jamie Madillf6113162015-05-07 11:49:21 -0400806 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500807 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700808 }
809
Jamie Madill62d31cb2015-09-11 13:25:51 -0400810 const auto &linkedUniforms = mData.getUniforms();
811 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700812 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
813 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400814 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700815
Jamie Madill62d31cb2015-09-11 13:25:51 -0400816 D3DUniform *d3dUniform =
817 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
818 linkedUniform.isInDefaultBlock());
819 stream->readInt(&d3dUniform->psRegisterIndex);
820 stream->readInt(&d3dUniform->vsRegisterIndex);
821 stream->readInt(&d3dUniform->registerCount);
822 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700823
Jamie Madill62d31cb2015-09-11 13:25:51 -0400824 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700825 }
826
Jamie Madill4a3c2342015-10-08 12:58:45 -0400827 const unsigned int blockCount = stream->readInt<unsigned int>();
828 if (stream->error())
829 {
830 infoLog << "Invalid program binary.";
831 return LinkResult(false, gl::Error(GL_NO_ERROR));
832 }
833
834 ASSERT(mD3DUniformBlocks.empty());
835 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
836 {
837 D3DUniformBlock uniformBlock;
838 stream->readInt(&uniformBlock.psRegisterIndex);
839 stream->readInt(&uniformBlock.vsRegisterIndex);
840 mD3DUniformBlocks.push_back(uniformBlock);
841 }
842
Jamie Madill9fc36822015-11-18 13:08:07 -0500843 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
844 mStreamOutVaryings.resize(streamOutVaryingCount);
845 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700846 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500847 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700848
Jamie Madill28afae52015-11-09 15:07:57 -0500849 stream->readString(&varying->semanticName);
850 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500851 stream->readInt(&varying->componentCount);
852 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700853 }
854
Brandon Jones22502d52014-08-29 16:58:36 -0700855 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400856 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
857 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700858 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400859 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
860 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700861 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700862 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400863 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700864
865 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
866 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400867 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
868 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700869 {
870 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
871 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
872 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
873 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
874 }
875
Jamie Madill4e31ad52015-10-29 10:32:57 -0400876 stream->readString(&mGeometryShaderPreamble);
877
Jamie Madill334d6152015-10-22 14:00:28 -0400878 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700879
880 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400881 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
882 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700883 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400884 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400885 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700886
Jamie Madilld3dfda22015-07-06 08:28:49 -0400887 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700888 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400889 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700890 }
891
Jamie Madill334d6152015-10-22 14:00:28 -0400892 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700893 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400894
Jamie Madillada9ecc2015-08-17 12:53:37 -0400895 ShaderExecutableD3D *shaderExecutable = nullptr;
896
897 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500898 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400899 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400900 if (error.isError())
901 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500902 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400903 }
904
Brandon Joneseb994362014-09-24 10:27:28 -0700905 if (!shaderExecutable)
906 {
Jamie Madillf6113162015-05-07 11:49:21 -0400907 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500908 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700909 }
910
911 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400912 VertexExecutable::Signature signature;
913 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700914
915 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400916 mVertexExecutables.push_back(
917 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700918
919 stream->skip(vertexShaderSize);
920 }
921
922 const size_t pixelShaderCount = stream->readInt<unsigned int>();
923 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
924 {
925 const size_t outputCount = stream->readInt<unsigned int>();
926 std::vector<GLenum> outputs(outputCount);
927 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
928 {
929 stream->readInt(&outputs[outputIndex]);
930 }
931
Jamie Madill334d6152015-10-22 14:00:28 -0400932 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700933 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400934 ShaderExecutableD3D *shaderExecutable = nullptr;
935
936 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500937 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400938 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400939 if (error.isError())
940 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500941 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400942 }
Brandon Joneseb994362014-09-24 10:27:28 -0700943
944 if (!shaderExecutable)
945 {
Jamie Madillf6113162015-05-07 11:49:21 -0400946 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500947 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700948 }
949
950 // add new binary
951 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
952
953 stream->skip(pixelShaderSize);
954 }
955
Jamie Madill4e31ad52015-10-29 10:32:57 -0400956 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
957 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700958 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400959 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
960 if (geometryShaderSize == 0)
961 {
962 mGeometryExecutables[geometryExeIndex] = nullptr;
963 continue;
964 }
965
Brandon Joneseb994362014-09-24 10:27:28 -0700966 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400967 bool splitAttribs = (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
968
Jamie Madill28afae52015-11-09 15:07:57 -0500969 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500970 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, mStreamOutVaryings,
971 splitAttribs, &mGeometryExecutables[geometryExeIndex]);
Geoff Langb543aff2014-09-30 14:52:54 -0400972 if (error.isError())
973 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500974 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400975 }
Brandon Joneseb994362014-09-24 10:27:28 -0700976
Jamie Madill4e31ad52015-10-29 10:32:57 -0400977 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700978 {
Jamie Madillf6113162015-05-07 11:49:21 -0400979 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500980 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700981 }
982 stream->skip(geometryShaderSize);
983 }
984
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700985 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500986 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700987
Geoff Lang7dd2e102014-11-10 15:19:26 -0500988 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700989}
990
Geoff Langb543aff2014-09-30 14:52:54 -0400991gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700992{
Austin Kinross137b1512015-06-17 16:14:53 -0700993 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400994 // When we load the binary again later, we can validate the device identifier before trying to
995 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700996 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400997 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
998 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700999
Jamie Madill2db1fbb2014-12-03 10:58:55 -05001000 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1001
Jamie Madill63805b42015-08-25 13:17:39 -04001002 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
1003 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
1004 {
1005 stream->writeInt(mSemanticIndexes[i]);
1006 }
1007
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001008 stream->writeInt(mSamplersPS.size());
1009 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
1010 {
1011 stream->writeInt(mSamplersPS[i].active);
1012 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
1013 stream->writeInt(mSamplersPS[i].textureType);
1014 }
1015
1016 stream->writeInt(mSamplersVS.size());
1017 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
1018 {
1019 stream->writeInt(mSamplersVS[i].active);
1020 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
1021 stream->writeInt(mSamplersVS[i].textureType);
1022 }
1023
1024 stream->writeInt(mUsedVertexSamplerRange);
1025 stream->writeInt(mUsedPixelSamplerRange);
1026
Jamie Madill62d31cb2015-09-11 13:25:51 -04001027 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001028 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001029 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001030 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001031 stream->writeInt(uniform->psRegisterIndex);
1032 stream->writeInt(uniform->vsRegisterIndex);
1033 stream->writeInt(uniform->registerCount);
1034 stream->writeInt(uniform->registerElement);
1035 }
1036
1037 stream->writeInt(mD3DUniformBlocks.size());
1038 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1039 {
1040 stream->writeInt(uniformBlock.psRegisterIndex);
1041 stream->writeInt(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001042 }
1043
Jamie Madill9fc36822015-11-18 13:08:07 -05001044 stream->writeInt(mStreamOutVaryings.size());
1045 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001046 {
Brandon Joneseb994362014-09-24 10:27:28 -07001047 stream->writeString(varying.semanticName);
1048 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001049 stream->writeInt(varying.componentCount);
1050 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001051 }
1052
Brandon Jones22502d52014-08-29 16:58:36 -07001053 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001054 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1055 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001056 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001057 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1058 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001059 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001060 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001061 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001062
Brandon Joneseb994362014-09-24 10:27:28 -07001063 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001064 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001065 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1066 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001067 {
Brandon Joneseb994362014-09-24 10:27:28 -07001068 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001069 stream->writeInt(variable.type);
1070 stream->writeString(variable.name);
1071 stream->writeString(variable.source);
1072 stream->writeInt(variable.outputIndex);
1073 }
1074
Jamie Madill4e31ad52015-10-29 10:32:57 -04001075 stream->writeString(mGeometryShaderPreamble);
1076
Brandon Joneseb994362014-09-24 10:27:28 -07001077 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001078 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1079 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001080 {
1081 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1082
Jamie Madilld3dfda22015-07-06 08:28:49 -04001083 const auto &inputLayout = vertexExecutable->inputs();
1084 stream->writeInt(inputLayout.size());
1085
1086 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001087 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001088 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -07001089 }
1090
1091 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1092 stream->writeInt(vertexShaderSize);
1093
1094 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1095 stream->writeBytes(vertexBlob, vertexShaderSize);
1096 }
1097
1098 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001099 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1100 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001101 {
1102 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1103
1104 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1105 stream->writeInt(outputs.size());
1106 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1107 {
1108 stream->writeInt(outputs[outputIndex]);
1109 }
1110
1111 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1112 stream->writeInt(pixelShaderSize);
1113
1114 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1115 stream->writeBytes(pixelBlob, pixelShaderSize);
1116 }
1117
Jamie Madill4e31ad52015-10-29 10:32:57 -04001118 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001119 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001120 if (geometryExe == nullptr)
1121 {
1122 stream->writeInt(0);
1123 continue;
1124 }
1125
1126 size_t geometryShaderSize = geometryExe->getLength();
1127 stream->writeInt(geometryShaderSize);
1128 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001129 }
1130
Geoff Langb543aff2014-09-30 14:52:54 -04001131 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001132}
1133
Geoff Langc5629752015-12-07 16:29:04 -05001134void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1135{
1136}
1137
Jamie Madill334d6152015-10-22 14:00:28 -04001138gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1139 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001140{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001141 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001142
Jamie Madill85a18042015-03-05 15:41:41 -05001143 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001144 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001145
1146 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1147 {
1148 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1149
1150 if (colorbuffer)
1151 {
Jamie Madill334d6152015-10-22 14:00:28 -04001152 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1153 ? GL_COLOR_ATTACHMENT0
1154 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001155 }
1156 else
1157 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001158 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001159 }
1160 }
1161
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001162 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001163}
1164
Jamie Madill97399232014-12-23 12:31:15 -05001165gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001166 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001167 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001168{
1169 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1170 {
1171 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1172 {
Geoff Langb543aff2014-09-30 14:52:54 -04001173 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1174 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001175 }
1176 }
1177
Jamie Madill334d6152015-10-22 14:00:28 -04001178 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1179 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001180
1181 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001182 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001183
1184 gl::InfoLog tempInfoLog;
1185 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1186
Jamie Madillada9ecc2015-08-17 12:53:37 -04001187 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001188 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001189 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
1190 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001191 if (error.isError())
1192 {
1193 return error;
1194 }
Brandon Joneseb994362014-09-24 10:27:28 -07001195
Jamie Madill97399232014-12-23 12:31:15 -05001196 if (pixelExecutable)
1197 {
1198 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1199 }
1200 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001201 {
1202 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001203 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001204 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1205 }
Brandon Jones22502d52014-08-29 16:58:36 -07001206
Geoff Langb543aff2014-09-30 14:52:54 -04001207 *outExectuable = pixelExecutable;
1208 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001209}
1210
Jamie Madilld3dfda22015-07-06 08:28:49 -04001211gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001212 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001213 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001214{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001215 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001216
1217 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1218 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001219 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001220 {
Geoff Langb543aff2014-09-30 14:52:54 -04001221 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1222 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001223 }
1224 }
1225
Brandon Jones22502d52014-08-29 16:58:36 -07001226 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001227 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1228 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001229
1230 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001231 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001232
1233 gl::InfoLog tempInfoLog;
1234 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1235
Jamie Madillada9ecc2015-08-17 12:53:37 -04001236 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001237 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001238 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1239 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001240 if (error.isError())
1241 {
1242 return error;
1243 }
1244
Jamie Madill97399232014-12-23 12:31:15 -05001245 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001246 {
Jamie Madill334d6152015-10-22 14:00:28 -04001247 mVertexExecutables.push_back(
1248 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001249 }
Jamie Madill97399232014-12-23 12:31:15 -05001250 else if (!infoLog)
1251 {
1252 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001253 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001254 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1255 }
Brandon Jones22502d52014-08-29 16:58:36 -07001256
Geoff Langb543aff2014-09-30 14:52:54 -04001257 *outExectuable = vertexExecutable;
1258 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001259}
1260
Jamie Madill4e31ad52015-10-29 10:32:57 -04001261gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Data &data,
1262 GLenum drawMode,
1263 ShaderExecutableD3D **outExecutable,
1264 gl::InfoLog *infoLog)
1265{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001266 if (outExecutable)
1267 {
1268 *outExecutable = nullptr;
1269 }
1270
Austin Kinross88829e82016-01-12 13:04:41 -08001271 // Return a null shader if the current rendering doesn't use a geometry shader
1272 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001273 {
1274 return gl::Error(GL_NO_ERROR);
1275 }
1276
Jamie Madill4e31ad52015-10-29 10:32:57 -04001277 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1278
1279 if (mGeometryExecutables[geometryShaderType] != nullptr)
1280 {
1281 if (outExecutable)
1282 {
1283 *outExecutable = mGeometryExecutables[geometryShaderType];
1284 }
1285 return gl::Error(GL_NO_ERROR);
1286 }
1287
1288 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001289 geometryShaderType, data, mData, mRenderer->presentPathFastEnabled(),
1290 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001291
1292 gl::InfoLog tempInfoLog;
1293 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1294
1295 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001296 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001297 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
1298 &mGeometryExecutables[geometryShaderType]);
1299
1300 if (!infoLog && error.isError())
1301 {
1302 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1303 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1304 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1305 }
1306
1307 if (outExecutable)
1308 {
1309 *outExecutable = mGeometryExecutables[geometryShaderType];
1310 }
1311 return error;
1312}
1313
1314LinkResult ProgramD3D::compileProgramExecutables(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001315{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001316 const gl::InputLayout &defaultInputLayout =
1317 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001318 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001319 gl::Error error =
1320 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001321 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001322 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001323 return LinkResult(false, error);
1324 }
Austin Kinross434953e2015-02-20 10:49:51 -08001325
Jamie Madill334d6152015-10-22 14:00:28 -04001326 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001327 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001328 error =
1329 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001330 if (error.isError())
1331 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001332 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001333 }
Brandon Jones44151a92014-09-10 11:32:25 -07001334
Jamie Madill4e31ad52015-10-29 10:32:57 -04001335 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill847638a2015-11-20 13:01:41 -05001336 ShaderExecutableD3D *pointGS = nullptr;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001337 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001338 {
Jamie Madill847638a2015-11-20 13:01:41 -05001339 getGeometryExecutableForPrimitiveType(data, GL_POINTS, &pointGS, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001340 }
1341
Jamie Madillca03b352015-09-02 12:38:13 -04001342 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001343
1344 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001345 {
Jamie Madill334d6152015-10-22 14:00:28 -04001346 // Geometry shaders are currently only used internally, so there is no corresponding shader
1347 // object at the interface level. For now the geometry shader debug info is prepended to
1348 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001349 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001350 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001351 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1352 }
1353
1354 if (defaultVertexExecutable)
1355 {
1356 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1357 }
1358
1359 if (defaultPixelExecutable)
1360 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001361 const ShaderD3D *fragmentShaderD3D =
1362 GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001363 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1364 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001365
Jamie Madill847638a2015-11-20 13:01:41 -05001366 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable &&
1367 (!usesGeometryShader(GL_POINTS) || pointGS));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001369}
1370
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001371LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001372{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001373 reset();
1374
1375 // TODO(jmadill): structures containing samplers
1376 for (const gl::LinkedUniform &linkedUniform : mData.getUniforms())
1377 {
1378 if (linkedUniform.isSampler() && linkedUniform.isField())
1379 {
1380 infoLog << "Structures containing samplers not currently supported in D3D.";
1381 return LinkResult(false, gl::Error(GL_NO_ERROR));
1382 }
1383 }
1384
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001385 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1386 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001387
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001388 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1389 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1390
Jamie Madill63069df2015-09-01 17:26:41 +00001391 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001392 mSamplersPS.resize(data.caps->maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001393
Arun Patole44efa0b2015-03-04 17:11:05 +05301394 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001395 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1396
Austin Kinross02df7962015-07-01 10:03:42 -07001397 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1398 {
1399 if (fragmentShaderD3D->usesFrontFacing())
1400 {
1401 infoLog << "The current renderer doesn't support gl_FrontFacing";
1402 return LinkResult(false, gl::Error(GL_NO_ERROR));
1403 }
1404 }
1405
Jamie Madillca03b352015-09-02 12:38:13 -04001406 std::vector<PackedVarying> packedVaryings =
1407 MergeVaryings(*vertexShader, *fragmentShader, mData.getTransformFeedbackVaryingNames());
1408
Brandon Jones22502d52014-08-29 16:58:36 -07001409 // Map the varyings to the register file
Jamie Madill9fc36822015-11-18 13:08:07 -05001410 VaryingPacking varyingPacking(data.caps->maxVaryingVectors);
1411 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
1412 mData.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001413 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001415 }
1416
Jamie Madille39a3f02015-11-17 20:42:15 -05001417 ProgramD3DMetadata metadata(mRenderer->getMajorShaderModel(), mRenderer->getShaderModelSuffix(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001418 usesInstancedPointSpriteEmulation(),
1419 mRenderer->presentPathFastEnabled(), vertexShaderD3D,
Jamie Madille39a3f02015-11-17 20:42:15 -05001420 fragmentShaderD3D);
1421
Jamie Madill9fc36822015-11-18 13:08:07 -05001422 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1423 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1424
1425 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.caps->maxVaryingVectors)
1426 {
1427 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1428 return LinkResult(false, gl::Error(GL_NO_ERROR));
1429 }
1430
1431 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1432 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1433 // intelligently, but D3D9 assumes one semantic per register.
1434 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1435 varyingPacking.getMaxSemanticIndex() > data.caps->maxVaryingVectors)
1436 {
1437 infoLog << "Cannot pack these varyings on D3D9.";
1438 return LinkResult(false, gl::Error(GL_NO_ERROR));
1439 }
1440
1441 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mData, metadata, varyingPacking, &mPixelHLSL,
1442 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001443 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001445 }
1446
Brandon Jones44151a92014-09-10 11:32:25 -07001447 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madille39a3f02015-11-17 20:42:15 -05001448 mDynamicHLSL->getPixelShaderOutputKey(data, mData, metadata, &mPixelShaderKey);
1449 mUsesFragDepth = metadata.usesFragDepth(mData);
Brandon Jones44151a92014-09-10 11:32:25 -07001450
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001451 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001452 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001453 for (const auto &varying : packedVaryings)
1454 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001455 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001456 {
1457 mUsesFlatInterpolation = true;
1458 break;
1459 }
1460 }
1461
Jamie Madill4e31ad52015-10-29 10:32:57 -04001462 if (mRenderer->getMajorShaderModel() >= 4)
1463 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001464 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1465 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001466 }
1467
Jamie Madill63805b42015-08-25 13:17:39 -04001468 initSemanticIndex();
Jamie Madill437d2662014-12-05 14:23:35 -05001469
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001470 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001471
Jamie Madill9fc36822015-11-18 13:08:07 -05001472 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001473
Jamie Madill4e31ad52015-10-29 10:32:57 -04001474 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001475 if (result.error.isError() || !result.linkSuccess)
1476 {
1477 infoLog << "Failed to create D3D shaders.";
1478 return result;
1479 }
1480
Jamie Madill4a3c2342015-10-08 12:58:45 -04001481 initUniformBlockInfo();
1482
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001484}
1485
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001486GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001487{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001488 // TODO(jmadill): Do something useful here?
1489 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001490}
1491
Jamie Madill4a3c2342015-10-08 12:58:45 -04001492void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001493{
1494 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1495
Jamie Madill62d31cb2015-09-11 13:25:51 -04001496 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1497 {
1498 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1499 continue;
1500
Jamie Madill4a3c2342015-10-08 12:58:45 -04001501 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001502 continue;
1503
Jamie Madill4a3c2342015-10-08 12:58:45 -04001504 size_t dataSize = getUniformBlockInfo(vertexBlock);
1505 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001506 }
1507
1508 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1509
1510 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1511 {
1512 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1513 continue;
1514
Jamie Madill4a3c2342015-10-08 12:58:45 -04001515 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001516 continue;
1517
Jamie Madill4a3c2342015-10-08 12:58:45 -04001518 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1519 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001520 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001521}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001522
Jamie Madill4a3c2342015-10-08 12:58:45 -04001523void ProgramD3D::assignUniformBlockRegisters()
1524{
1525 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001526
1527 // Assign registers and update sizes.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001528 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1529 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001530
Jamie Madill4a3c2342015-10-08 12:58:45 -04001531 for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001532 {
1533 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1534
Jamie Madill4a3c2342015-10-08 12:58:45 -04001535 D3DUniformBlock d3dUniformBlock;
1536
Jamie Madill62d31cb2015-09-11 13:25:51 -04001537 if (uniformBlock.vertexStaticUse)
1538 {
1539 unsigned int baseRegister =
1540 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001541 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001542 }
1543
1544 if (uniformBlock.fragmentStaticUse)
1545 {
1546 unsigned int baseRegister =
1547 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001548 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001549 }
1550
Jamie Madill4a3c2342015-10-08 12:58:45 -04001551 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001552 }
1553}
1554
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001555void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001556{
1557 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001558 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001559 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001560 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001561 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001562 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001563 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001564 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001565 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001566 vertexRegisters = std::max(vertexRegisters,
1567 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001568 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001569 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001570 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001571 fragmentRegisters = std::max(
1572 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001573 }
1574 }
1575 }
1576
Jamie Madill334d6152015-10-22 14:00:28 -04001577 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001578 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1579}
1580
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001581gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001582{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001583 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001584
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001585 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001586 if (error.isError())
1587 {
1588 return error;
1589 }
1590
Jamie Madill62d31cb2015-09-11 13:25:51 -04001591 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001592 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001593 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001594 }
1595
1596 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001597}
1598
Jamie Madilld1fe1642015-08-21 16:26:04 -04001599gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001600{
Jamie Madill4a3c2342015-10-08 12:58:45 -04001601 if (mData.getUniformBlocks().empty())
1602 {
1603 return gl::Error(GL_NO_ERROR);
1604 }
1605
1606 // Lazy init.
1607 if (mD3DUniformBlocks.empty())
1608 {
1609 assignUniformBlockRegisters();
1610 }
1611
Jamie Madill03260fa2015-06-22 13:57:22 -04001612 mVertexUBOCache.clear();
1613 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001614
1615 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1616 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1617
Jamie Madill4a3c2342015-10-08 12:58:45 -04001618 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001619 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001620 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001621 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
1622 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001623
Brandon Jones18bd4102014-09-22 14:21:44 -07001624 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001625 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001626 {
1627 continue;
1628 }
1629
Jamie Madill4a3c2342015-10-08 12:58:45 -04001630 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001631 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001632 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001633 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001634
Jamie Madill969194d2015-07-20 14:36:56 -04001635 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001636 {
1637 mVertexUBOCache.resize(registerIndex + 1, -1);
1638 }
1639
1640 ASSERT(mVertexUBOCache[registerIndex] == -1);
1641 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001642 }
1643
Jamie Madill4a3c2342015-10-08 12:58:45 -04001644 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001645 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001646 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001647 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001648
1649 if (mFragmentUBOCache.size() <= registerIndex)
1650 {
1651 mFragmentUBOCache.resize(registerIndex + 1, -1);
1652 }
1653
1654 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1655 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001656 }
1657 }
1658
Jamie Madill03260fa2015-06-22 13:57:22 -04001659 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001660}
1661
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001662void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001663{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001664 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001665 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001666 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001667 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001668}
1669
Jamie Madill334d6152015-10-22 14:00:28 -04001670void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001671{
1672 setUniform(location, count, v, GL_FLOAT);
1673}
1674
1675void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1676{
1677 setUniform(location, count, v, GL_FLOAT_VEC2);
1678}
1679
1680void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1681{
1682 setUniform(location, count, v, GL_FLOAT_VEC3);
1683}
1684
1685void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1686{
1687 setUniform(location, count, v, GL_FLOAT_VEC4);
1688}
1689
Jamie Madill334d6152015-10-22 14:00:28 -04001690void ProgramD3D::setUniformMatrix2fv(GLint location,
1691 GLsizei count,
1692 GLboolean transpose,
1693 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001694{
1695 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1696}
1697
Jamie Madill334d6152015-10-22 14:00:28 -04001698void ProgramD3D::setUniformMatrix3fv(GLint location,
1699 GLsizei count,
1700 GLboolean transpose,
1701 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001702{
1703 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1704}
1705
Jamie Madill334d6152015-10-22 14:00:28 -04001706void ProgramD3D::setUniformMatrix4fv(GLint location,
1707 GLsizei count,
1708 GLboolean transpose,
1709 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001710{
1711 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1712}
1713
Jamie Madill334d6152015-10-22 14:00:28 -04001714void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1715 GLsizei count,
1716 GLboolean transpose,
1717 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001718{
1719 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1720}
1721
Jamie Madill334d6152015-10-22 14:00:28 -04001722void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1723 GLsizei count,
1724 GLboolean transpose,
1725 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001726{
1727 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1728}
1729
Jamie Madill334d6152015-10-22 14:00:28 -04001730void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1731 GLsizei count,
1732 GLboolean transpose,
1733 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001734{
1735 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1736}
1737
Jamie Madill334d6152015-10-22 14:00:28 -04001738void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1739 GLsizei count,
1740 GLboolean transpose,
1741 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001742{
1743 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1744}
1745
Jamie Madill334d6152015-10-22 14:00:28 -04001746void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1747 GLsizei count,
1748 GLboolean transpose,
1749 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001750{
1751 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1752}
1753
Jamie Madill334d6152015-10-22 14:00:28 -04001754void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1755 GLsizei count,
1756 GLboolean transpose,
1757 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001758{
1759 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1760}
1761
1762void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1763{
1764 setUniform(location, count, v, GL_INT);
1765}
1766
1767void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1768{
1769 setUniform(location, count, v, GL_INT_VEC2);
1770}
1771
1772void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1773{
1774 setUniform(location, count, v, GL_INT_VEC3);
1775}
1776
1777void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1778{
1779 setUniform(location, count, v, GL_INT_VEC4);
1780}
1781
1782void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1783{
1784 setUniform(location, count, v, GL_UNSIGNED_INT);
1785}
1786
1787void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1788{
1789 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1790}
1791
1792void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1793{
1794 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1795}
1796
1797void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1798{
1799 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1800}
1801
Jamie Madill4a3c2342015-10-08 12:58:45 -04001802void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1803 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001804{
1805}
1806
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001807void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001808{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001809 D3DUniformMap uniformMap;
Jamie Madill334d6152015-10-22 14:00:28 -04001810 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001811 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001812
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001813 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001814 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001815 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001816 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001817 }
1818 }
1819
Jamie Madill334d6152015-10-22 14:00:28 -04001820 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001821 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001822 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001823 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001824 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001825 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001826 }
1827 }
1828
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001829 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
1830 for (const gl::LinkedUniform &glUniform : mData.getUniforms())
1831 {
1832 if (!glUniform.isInDefaultBlock())
1833 continue;
1834
1835 auto mapEntry = uniformMap.find(glUniform.name);
1836 ASSERT(mapEntry != uniformMap.end());
1837 mD3DUniforms.push_back(mapEntry->second);
1838 }
1839
Jamie Madill62d31cb2015-09-11 13:25:51 -04001840 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001841 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001842}
1843
Jamie Madill91445bc2015-09-23 16:47:53 -04001844void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001845 const sh::Uniform &uniform,
1846 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001847{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001848 if (uniform.isBuiltIn())
Jamie Madillfb536032015-09-11 13:19:49 -04001849 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001850 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001851 return;
1852 }
1853
Jamie Madill91445bc2015-09-23 16:47:53 -04001854 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1855
1856 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1857 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001858 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001859 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001860
Jamie Madill91445bc2015-09-23 16:47:53 -04001861 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001862}
1863
Jamie Madill62d31cb2015-09-11 13:25:51 -04001864D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1865{
1866 for (D3DUniform *d3dUniform : mD3DUniforms)
1867 {
1868 if (d3dUniform->name == name)
1869 {
1870 return d3dUniform;
1871 }
1872 }
1873
1874 return nullptr;
1875}
1876
Jamie Madill91445bc2015-09-23 16:47:53 -04001877void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001878 const sh::ShaderVariable &uniform,
1879 const std::string &fullName,
1880 sh::HLSLBlockEncoder *encoder,
1881 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001882{
1883 if (uniform.isStruct())
1884 {
1885 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1886 {
1887 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1888
Jamie Madill55def582015-05-04 11:24:57 -04001889 if (encoder)
1890 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001891
1892 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1893 {
Jamie Madill334d6152015-10-22 14:00:28 -04001894 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001895 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1896
Jamie Madill91445bc2015-09-23 16:47:53 -04001897 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001898 }
1899
Jamie Madill55def582015-05-04 11:24:57 -04001900 if (encoder)
1901 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001902 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001903 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001904 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001905
1906 // Not a struct. Arrays are treated as aggregate types.
1907 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001908 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001909 encoder->enterAggregateType();
1910 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001911
Jamie Madill62d31cb2015-09-11 13:25:51 -04001912 // Advance the uniform offset, to track registers allocation for structs
1913 sh::BlockMemberInfo blockInfo =
1914 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1915 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001916
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001917 auto uniformMapEntry = uniformMap->find(fullName);
1918 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001919
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001920 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001921 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001922 d3dUniform = uniformMapEntry->second;
1923 }
1924 else
1925 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001926 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001927 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001928 }
1929
1930 if (encoder)
1931 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001932 d3dUniform->registerElement =
1933 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001934 unsigned int reg =
1935 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001936 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001937 {
1938 d3dUniform->psRegisterIndex = reg;
1939 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001940 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001941 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001942 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001943 d3dUniform->vsRegisterIndex = reg;
1944 }
Jamie Madillfb536032015-09-11 13:19:49 -04001945
1946 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001947 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001948 {
1949 encoder->exitAggregateType();
1950 }
1951 }
1952}
1953
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001954template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001955void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001956{
Jamie Madill334d6152015-10-22 14:00:28 -04001957 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001958 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1959
Jamie Madill62d31cb2015-09-11 13:25:51 -04001960 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001961
Jamie Madill62d31cb2015-09-11 13:25:51 -04001962 unsigned int elementCount = targetUniform->elementCount();
1963 unsigned int arrayElement = mData.getUniformLocations()[location].element;
1964 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001965
1966 if (targetUniform->type == targetUniformType)
1967 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001968 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001969
Jamie Madill62d31cb2015-09-11 13:25:51 -04001970 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001971 {
Jamie Madill334d6152015-10-22 14:00:28 -04001972 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001973 const T *source = v + (i * components);
1974
1975 for (int c = 0; c < components; c++)
1976 {
1977 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1978 }
1979 for (int c = components; c < 4; c++)
1980 {
1981 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1982 }
1983 }
1984 }
1985 else if (targetUniform->type == targetBoolType)
1986 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001987 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001988
Jamie Madill62d31cb2015-09-11 13:25:51 -04001989 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001990 {
Jamie Madill334d6152015-10-22 14:00:28 -04001991 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001992 const T *source = v + (i * components);
1993
1994 for (int c = 0; c < components; c++)
1995 {
Jamie Madill334d6152015-10-22 14:00:28 -04001996 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1997 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001998 }
1999 for (int c = components; c < 4; c++)
2000 {
2001 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2002 }
2003 }
2004 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002005 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002006 {
2007 ASSERT(targetUniformType == GL_INT);
2008
Jamie Madill62d31cb2015-09-11 13:25:51 -04002009 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002010
2011 bool wasDirty = targetUniform->dirty;
2012
Jamie Madill62d31cb2015-09-11 13:25:51 -04002013 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002014 {
Jamie Madill334d6152015-10-22 14:00:28 -04002015 GLint *dest = target + (i * 4);
2016 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017
2018 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2019 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2020 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2021 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2022 }
2023
2024 if (!wasDirty && targetUniform->dirty)
2025 {
2026 mDirtySamplerMapping = true;
2027 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002028 }
Jamie Madill334d6152015-10-22 14:00:28 -04002029 else
2030 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002031}
2032
2033template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002034void ProgramD3D::setUniformMatrixfv(GLint location,
2035 GLsizei countIn,
2036 GLboolean transpose,
2037 const GLfloat *value,
2038 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002039{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002040 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002041
Jamie Madill62d31cb2015-09-11 13:25:51 -04002042 unsigned int elementCount = targetUniform->elementCount();
2043 unsigned int arrayElement = mData.getUniformLocations()[location].element;
2044 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002045
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002046 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002047 GLfloat *target =
2048 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002049
Jamie Madill62d31cb2015-09-11 13:25:51 -04002050 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002051 {
2052 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2053 if (transpose == GL_FALSE)
2054 {
Jamie Madill334d6152015-10-22 14:00:28 -04002055 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2056 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002057 }
2058 else
2059 {
Jamie Madill334d6152015-10-22 14:00:28 -04002060 targetUniform->dirty =
2061 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002062 }
2063 target += targetMatrixStride;
2064 value += cols * rows;
2065 }
2066}
2067
Jamie Madill4a3c2342015-10-08 12:58:45 -04002068size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002069{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002070 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002071
Jamie Madill62d31cb2015-09-11 13:25:51 -04002072 // define member uniforms
2073 sh::Std140BlockEncoder std140Encoder;
2074 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2075 sh::BlockLayoutEncoder *encoder = nullptr;
2076
2077 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002078 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002079 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002080 }
2081 else
2082 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002083 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002084 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002085
Jamie Madill39046162016-02-08 15:05:17 -05002086 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2087 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002088
2089 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002090}
2091
Jamie Madill62d31cb2015-09-11 13:25:51 -04002092void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002093{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002094 for (const D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002095 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002096 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002097 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002098 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002099 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002100 }
2101}
2102
Jamie Madill62d31cb2015-09-11 13:25:51 -04002103void ProgramD3D::assignSamplerRegisters(const D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002104{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002105 ASSERT(d3dUniform->isSampler());
2106 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX ||
2107 d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madillfb536032015-09-11 13:19:49 -04002108
Jamie Madill62d31cb2015-09-11 13:25:51 -04002109 if (d3dUniform->vsRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002110 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002111 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2112 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002113 }
2114
Jamie Madill62d31cb2015-09-11 13:25:51 -04002115 if (d3dUniform->psRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002116 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002117 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2118 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002119 }
2120}
2121
Jamie Madill62d31cb2015-09-11 13:25:51 -04002122// static
2123void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002124 GLenum samplerType,
2125 unsigned int samplerCount,
2126 std::vector<Sampler> &outSamplers,
2127 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002128{
2129 unsigned int samplerIndex = startSamplerIndex;
2130
2131 do
2132 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002133 ASSERT(samplerIndex < outSamplers.size());
2134 Sampler *sampler = &outSamplers[samplerIndex];
2135 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002136 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002137 sampler->logicalTextureUnit = 0;
2138 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002139 samplerIndex++;
2140 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002141}
2142
Brandon Jonesc9610c52014-08-25 17:02:59 -07002143void ProgramD3D::reset()
2144{
Brandon Joneseb994362014-09-24 10:27:28 -07002145 SafeDeleteContainer(mVertexExecutables);
2146 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002147
2148 for (auto &element : mGeometryExecutables)
2149 {
2150 SafeDelete(element);
2151 }
Brandon Joneseb994362014-09-24 10:27:28 -07002152
Brandon Jones22502d52014-08-29 16:58:36 -07002153 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002154 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002155
2156 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002157 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002158 mUsesFragDepth = false;
2159 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002160 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002161 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002162
Jamie Madill62d31cb2015-09-11 13:25:51 -04002163 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002164 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002165
Brandon Jonesc9610c52014-08-25 17:02:59 -07002166 SafeDelete(mVertexUniformStorage);
2167 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002168
2169 mSamplersPS.clear();
2170 mSamplersVS.clear();
2171
2172 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002173 mUsedPixelSamplerRange = 0;
2174 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002175
Jamie Madill63805b42015-08-25 13:17:39 -04002176 std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1);
Jamie Madill437d2662014-12-05 14:23:35 -05002177 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002178
Jamie Madill9fc36822015-11-18 13:08:07 -05002179 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002180
2181 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002182}
2183
Geoff Lang7dd2e102014-11-10 15:19:26 -05002184unsigned int ProgramD3D::getSerial() const
2185{
2186 return mSerial;
2187}
2188
2189unsigned int ProgramD3D::issueSerial()
2190{
2191 return mCurrentSerial++;
2192}
2193
Jamie Madill63805b42015-08-25 13:17:39 -04002194void ProgramD3D::initSemanticIndex()
2195{
2196 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2197 ASSERT(vertexShader != nullptr);
2198
2199 // Init semantic index
2200 for (const sh::Attribute &attribute : mData.getAttributes())
2201 {
2202 int attributeIndex = attribute.location;
2203 int index = vertexShader->getSemanticIndex(attribute.name);
2204 int regs = gl::VariableRegisterCount(attribute.type);
2205
2206 for (int reg = 0; reg < regs; ++reg)
2207 {
2208 mSemanticIndexes[attributeIndex + reg] = index + reg;
2209 }
2210 }
2211
2212 initAttributesByLayout();
2213}
2214
Jamie Madill437d2662014-12-05 14:23:35 -05002215void ProgramD3D::initAttributesByLayout()
2216{
2217 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2218 {
2219 mAttributesByLayout[i] = i;
2220 }
2221
Jamie Madill63805b42015-08-25 13:17:39 -04002222 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS],
2223 AttributeSorter(mSemanticIndexes));
Jamie Madill437d2662014-12-05 14:23:35 -05002224}
2225
Jamie Madill334d6152015-10-22 14:00:28 -04002226void ProgramD3D::sortAttributesByLayout(
2227 const std::vector<TranslatedAttribute> &unsortedAttributes,
2228 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2229 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002230{
Jamie Madill476682e2015-06-30 10:04:29 -04002231 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002232 {
Jamie Madill334d6152015-10-22 14:00:28 -04002233 int oldIndex = mAttributesByLayout[attribIndex];
Jamie Madill63805b42015-08-25 13:17:39 -04002234 sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex];
Jamie Madill334d6152015-10-22 14:00:28 -04002235 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002236 }
2237}
2238
Jamie Madill63805b42015-08-25 13:17:39 -04002239void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002240{
Jamie Madillbd136f92015-08-10 14:51:37 -04002241 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002242 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002243
Dian Xianga4928832015-09-15 10:11:17 -07002244 for (unsigned int attributeIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002245 {
Jamie Madill63805b42015-08-25 13:17:39 -04002246 int semanticIndex = mSemanticIndexes[attributeIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002247
2248 if (semanticIndex != -1)
2249 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002250 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2251 {
2252 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2253 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002254 mCachedInputLayout[semanticIndex] =
2255 GetVertexFormatType(vertexAttributes[attributeIndex],
2256 state.getVertexAttribCurrentValue(attributeIndex).Type);
2257 }
2258 }
2259}
2260
Jamie Madill9fc36822015-11-18 13:08:07 -05002261void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002262{
Jamie Madill9fc36822015-11-18 13:08:07 -05002263 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2264
2265 const std::string &varyingSemantic =
2266 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2267
Jamie Madillccdf74b2015-08-18 10:46:12 -04002268 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002269 mStreamOutVaryings.clear();
2270
2271 const auto &tfVaryingNames = mData.getTransformFeedbackVaryingNames();
2272 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2273 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002274 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002275 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2276 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002277 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002278 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002279 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002280 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2281 builtins.glPosition.index, 4, outputSlot));
2282 }
2283 }
2284 else if (tfVaryingName == "gl_FragCoord")
2285 {
2286 if (builtins.glFragCoord.enabled)
2287 {
2288 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2289 builtins.glFragCoord.index, 4, outputSlot));
2290 }
2291 }
2292 else if (tfVaryingName == "gl_PointSize")
2293 {
2294 if (builtins.glPointSize.enabled)
2295 {
2296 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2297 }
2298 }
2299 else
2300 {
2301 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2302 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002303 const auto &varying = *registerInfo.packedVarying->varying;
2304 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002305 int componentCount = gl::VariableColumnCount(transposedType);
2306 ASSERT(!varying.isBuiltIn());
2307
Jamie Madill55c25d02015-11-18 13:08:08 -05002308 // Transform feedback for varying structs is underspecified.
2309 // See Khronos bug 9856.
2310 // TODO(jmadill): Figure out how to be spec-compliant here.
2311 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2312 continue;
2313
Jamie Madill9fc36822015-11-18 13:08:07 -05002314 // There can be more than one register assigned to a particular varying, and each
2315 // register needs its own stream out entry.
2316 if (tfVaryingName == varying.name)
2317 {
2318 mStreamOutVaryings.push_back(D3DVarying(
2319 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2320 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002321 }
2322 }
2323 }
2324}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002325
2326D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2327{
2328 return mD3DUniforms[mData.getUniformLocations()[location].index];
2329}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002330
2331bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2332{
2333 std::string baseName = blockName;
2334 gl::ParseAndStripArrayIndex(&baseName);
2335
2336 auto sizeIter = mBlockDataSizes.find(baseName);
2337 if (sizeIter == mBlockDataSizes.end())
2338 {
2339 *sizeOut = 0;
2340 return false;
2341 }
2342
2343 *sizeOut = sizeIter->second;
2344 return true;
2345}
2346
2347bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2348 sh::BlockMemberInfo *memberInfoOut) const
2349{
2350 auto infoIter = mBlockInfo.find(memberUniformName);
2351 if (infoIter == mBlockInfo.end())
2352 {
2353 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2354 return false;
2355 }
2356
2357 *memberInfoOut = infoIter->second;
2358 return true;
2359}
Brandon Jonesc9610c52014-08-25 17:02:59 -07002360}