blob: 37a288ea10853734481830a326a1f0aa22952cb1 [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
512void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
513 const gl::InputLayout &inputLayout,
514 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700515{
Jamie Madillbd136f92015-08-10 14:51:37 -0400516 signatureOut->resize(inputLayout.size());
Jamie Madilld3dfda22015-07-06 08:28:49 -0400517
518 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700519 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400520 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbd136f92015-08-10 14:51:37 -0400521 bool converted = false;
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400522 if (vertexFormatType != gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700523 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400524 VertexConversionType conversionType =
525 renderer->getVertexConversionType(vertexFormatType);
Jamie Madillbd136f92015-08-10 14:51:37 -0400526 converted = ((conversionType & VERTEX_CONVERT_GPU) != 0);
Brandon Joneseb994362014-09-24 10:27:28 -0700527 }
Jamie Madillbd136f92015-08-10 14:51:37 -0400528
529 (*signatureOut)[index] = converted;
Brandon Joneseb994362014-09-24 10:27:28 -0700530 }
Brandon Joneseb994362014-09-24 10:27:28 -0700531}
532
Jamie Madilld3dfda22015-07-06 08:28:49 -0400533bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
534{
Jamie Madillbd136f92015-08-10 14:51:37 -0400535 size_t limit = std::max(mSignature.size(), signature.size());
536 for (size_t index = 0; index < limit; ++index)
537 {
538 // treat undefined indexes as 'not converted'
539 bool a = index < signature.size() ? signature[index] : false;
540 bool b = index < mSignature.size() ? mSignature[index] : false;
541 if (a != b)
542 return false;
543 }
544
545 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400546}
547
548ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
549 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400550 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700551{
552}
553
554ProgramD3D::PixelExecutable::~PixelExecutable()
555{
556 SafeDelete(mShaderExecutable);
557}
558
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700559ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
560{
561}
562
Geoff Lang7dd2e102014-11-10 15:19:26 -0500563unsigned int ProgramD3D::mCurrentSerial = 1;
564
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400565ProgramD3D::ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer)
566 : ProgramImpl(data),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700567 mRenderer(renderer),
568 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400569 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700570 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400571 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700572 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700573 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700574 mUsedVertexSamplerRange(0),
575 mUsedPixelSamplerRange(0),
576 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500577 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700578{
Brandon Joneseb994362014-09-24 10:27:28 -0700579 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700580}
581
582ProgramD3D::~ProgramD3D()
583{
584 reset();
585 SafeDelete(mDynamicHLSL);
586}
587
Brandon Jones44151a92014-09-10 11:32:25 -0700588bool ProgramD3D::usesPointSpriteEmulation() const
589{
590 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
591}
592
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400593bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700594{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400595 if (drawMode != GL_POINTS)
596 {
597 return mUsesFlatInterpolation;
598 }
599
Cooper Partine6664f02015-01-09 16:22:24 -0800600 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
601}
602
603bool ProgramD3D::usesInstancedPointSpriteEmulation() const
604{
605 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700606}
607
Jamie Madill334d6152015-10-22 14:00:28 -0400608GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
609 unsigned int samplerIndex,
610 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700611{
612 GLint logicalTextureUnit = -1;
613
614 switch (type)
615 {
Jamie Madill334d6152015-10-22 14:00:28 -0400616 case gl::SAMPLER_PIXEL:
617 ASSERT(samplerIndex < caps.maxTextureImageUnits);
618 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
619 {
620 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
621 }
622 break;
623 case gl::SAMPLER_VERTEX:
624 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
625 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
626 {
627 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
628 }
629 break;
630 default:
631 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700632 }
633
Jamie Madill334d6152015-10-22 14:00:28 -0400634 if (logicalTextureUnit >= 0 &&
635 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700636 {
637 return logicalTextureUnit;
638 }
639
640 return -1;
641}
642
643// Returns the texture type for a given Direct3D 9 sampler type and
644// index (0-15 for the pixel shader and 0-3 for the vertex shader).
645GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
646{
647 switch (type)
648 {
Jamie Madill334d6152015-10-22 14:00:28 -0400649 case gl::SAMPLER_PIXEL:
650 ASSERT(samplerIndex < mSamplersPS.size());
651 ASSERT(mSamplersPS[samplerIndex].active);
652 return mSamplersPS[samplerIndex].textureType;
653 case gl::SAMPLER_VERTEX:
654 ASSERT(samplerIndex < mSamplersVS.size());
655 ASSERT(mSamplersVS[samplerIndex].active);
656 return mSamplersVS[samplerIndex].textureType;
657 default:
658 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700659 }
660
661 return GL_TEXTURE_2D;
662}
663
Geoff Lang00140f42016-02-03 18:47:33 +0000664GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700665{
666 switch (type)
667 {
Jamie Madill334d6152015-10-22 14:00:28 -0400668 case gl::SAMPLER_PIXEL:
669 return mUsedPixelSamplerRange;
670 case gl::SAMPLER_VERTEX:
671 return mUsedVertexSamplerRange;
672 default:
673 UNREACHABLE();
Geoff Lang00140f42016-02-03 18:47:33 +0000674 return 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700675 }
676}
677
678void ProgramD3D::updateSamplerMapping()
679{
680 if (!mDirtySamplerMapping)
681 {
682 return;
683 }
684
685 mDirtySamplerMapping = false;
686
687 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400688 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700689 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400690 if (!d3dUniform->dirty)
691 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700692
Jamie Madill62d31cb2015-09-11 13:25:51 -0400693 if (!d3dUniform->isSampler())
694 continue;
695
696 int count = d3dUniform->elementCount();
697 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
698
699 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700700 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400701 unsigned int firstIndex = d3dUniform->psRegisterIndex;
702
703 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700704 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400705 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700706
Jamie Madill62d31cb2015-09-11 13:25:51 -0400707 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700708 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400709 ASSERT(mSamplersPS[samplerIndex].active);
710 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700711 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400712 }
713 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700714
Jamie Madill62d31cb2015-09-11 13:25:51 -0400715 if (d3dUniform->isReferencedByVertexShader())
716 {
717 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
718
719 for (int i = 0; i < count; i++)
720 {
721 unsigned int samplerIndex = firstIndex + i;
722
723 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700724 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400725 ASSERT(mSamplersVS[samplerIndex].active);
726 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700727 }
728 }
729 }
730 }
731}
732
Geoff Lang7dd2e102014-11-10 15:19:26 -0500733LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700734{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400735 reset();
736
Jamie Madill334d6152015-10-22 14:00:28 -0400737 DeviceIdentifier binaryDeviceIdentifier = {0};
738 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
739 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700740
741 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
742 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
743 {
744 infoLog << "Invalid program binary, device configuration has changed.";
745 return LinkResult(false, gl::Error(GL_NO_ERROR));
746 }
747
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500748 int compileFlags = stream->readInt<int>();
749 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
750 {
Jamie Madillf6113162015-05-07 11:49:21 -0400751 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500752 return LinkResult(false, gl::Error(GL_NO_ERROR));
753 }
754
Jamie Madill63805b42015-08-25 13:17:39 -0400755 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
756 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
757 {
758 stream->readInt(&mSemanticIndexes[i]);
759 }
760
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700761 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
762 for (unsigned int i = 0; i < psSamplerCount; ++i)
763 {
764 Sampler sampler;
765 stream->readBool(&sampler.active);
766 stream->readInt(&sampler.logicalTextureUnit);
767 stream->readInt(&sampler.textureType);
768 mSamplersPS.push_back(sampler);
769 }
770 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
771 for (unsigned int i = 0; i < vsSamplerCount; ++i)
772 {
773 Sampler sampler;
774 stream->readBool(&sampler.active);
775 stream->readInt(&sampler.logicalTextureUnit);
776 stream->readInt(&sampler.textureType);
777 mSamplersVS.push_back(sampler);
778 }
779
780 stream->readInt(&mUsedVertexSamplerRange);
781 stream->readInt(&mUsedPixelSamplerRange);
782
783 const unsigned int uniformCount = stream->readInt<unsigned int>();
784 if (stream->error())
785 {
Jamie Madillf6113162015-05-07 11:49:21 -0400786 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500787 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700788 }
789
Jamie Madill62d31cb2015-09-11 13:25:51 -0400790 const auto &linkedUniforms = mData.getUniforms();
791 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700792 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
793 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400794 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700795
Jamie Madill62d31cb2015-09-11 13:25:51 -0400796 D3DUniform *d3dUniform =
797 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
798 linkedUniform.isInDefaultBlock());
799 stream->readInt(&d3dUniform->psRegisterIndex);
800 stream->readInt(&d3dUniform->vsRegisterIndex);
801 stream->readInt(&d3dUniform->registerCount);
802 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700803
Jamie Madill62d31cb2015-09-11 13:25:51 -0400804 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700805 }
806
Jamie Madill4a3c2342015-10-08 12:58:45 -0400807 const unsigned int blockCount = stream->readInt<unsigned int>();
808 if (stream->error())
809 {
810 infoLog << "Invalid program binary.";
811 return LinkResult(false, gl::Error(GL_NO_ERROR));
812 }
813
814 ASSERT(mD3DUniformBlocks.empty());
815 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
816 {
817 D3DUniformBlock uniformBlock;
818 stream->readInt(&uniformBlock.psRegisterIndex);
819 stream->readInt(&uniformBlock.vsRegisterIndex);
820 mD3DUniformBlocks.push_back(uniformBlock);
821 }
822
Jamie Madill9fc36822015-11-18 13:08:07 -0500823 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
824 mStreamOutVaryings.resize(streamOutVaryingCount);
825 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700826 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500827 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700828
Jamie Madill28afae52015-11-09 15:07:57 -0500829 stream->readString(&varying->semanticName);
830 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500831 stream->readInt(&varying->componentCount);
832 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700833 }
834
Brandon Jones22502d52014-08-29 16:58:36 -0700835 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400836 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
837 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700838 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400839 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
840 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700841 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700842 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400843 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700844
845 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
846 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400847 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
848 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700849 {
850 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
851 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
852 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
853 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
854 }
855
Jamie Madill4e31ad52015-10-29 10:32:57 -0400856 stream->readString(&mGeometryShaderPreamble);
857
Jamie Madill334d6152015-10-22 14:00:28 -0400858 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700859
860 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400861 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
862 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700863 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400864 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400865 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700866
Jamie Madilld3dfda22015-07-06 08:28:49 -0400867 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700868 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400869 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700870 }
871
Jamie Madill334d6152015-10-22 14:00:28 -0400872 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700873 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400874
Jamie Madillada9ecc2015-08-17 12:53:37 -0400875 ShaderExecutableD3D *shaderExecutable = nullptr;
876
877 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500878 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400879 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400880 if (error.isError())
881 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500882 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400883 }
884
Brandon Joneseb994362014-09-24 10:27:28 -0700885 if (!shaderExecutable)
886 {
Jamie Madillf6113162015-05-07 11:49:21 -0400887 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500888 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700889 }
890
891 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400892 VertexExecutable::Signature signature;
893 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700894
895 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400896 mVertexExecutables.push_back(
897 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700898
899 stream->skip(vertexShaderSize);
900 }
901
902 const size_t pixelShaderCount = stream->readInt<unsigned int>();
903 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
904 {
905 const size_t outputCount = stream->readInt<unsigned int>();
906 std::vector<GLenum> outputs(outputCount);
907 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
908 {
909 stream->readInt(&outputs[outputIndex]);
910 }
911
Jamie Madill334d6152015-10-22 14:00:28 -0400912 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700913 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400914 ShaderExecutableD3D *shaderExecutable = nullptr;
915
916 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500917 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400918 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400919 if (error.isError())
920 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500921 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400922 }
Brandon Joneseb994362014-09-24 10:27:28 -0700923
924 if (!shaderExecutable)
925 {
Jamie Madillf6113162015-05-07 11:49:21 -0400926 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500927 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700928 }
929
930 // add new binary
931 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
932
933 stream->skip(pixelShaderSize);
934 }
935
Jamie Madill4e31ad52015-10-29 10:32:57 -0400936 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
937 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700938 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400939 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
940 if (geometryShaderSize == 0)
941 {
942 mGeometryExecutables[geometryExeIndex] = nullptr;
943 continue;
944 }
945
Brandon Joneseb994362014-09-24 10:27:28 -0700946 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400947 bool splitAttribs = (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
948
Jamie Madill28afae52015-11-09 15:07:57 -0500949 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500950 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, mStreamOutVaryings,
951 splitAttribs, &mGeometryExecutables[geometryExeIndex]);
Geoff Langb543aff2014-09-30 14:52:54 -0400952 if (error.isError())
953 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500954 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400955 }
Brandon Joneseb994362014-09-24 10:27:28 -0700956
Jamie Madill4e31ad52015-10-29 10:32:57 -0400957 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700958 {
Jamie Madillf6113162015-05-07 11:49:21 -0400959 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500960 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700961 }
962 stream->skip(geometryShaderSize);
963 }
964
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700965 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500966 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700967
Geoff Lang7dd2e102014-11-10 15:19:26 -0500968 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700969}
970
Geoff Langb543aff2014-09-30 14:52:54 -0400971gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700972{
Austin Kinross137b1512015-06-17 16:14:53 -0700973 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400974 // When we load the binary again later, we can validate the device identifier before trying to
975 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700976 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400977 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
978 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700979
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500980 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
981
Jamie Madill63805b42015-08-25 13:17:39 -0400982 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
983 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
984 {
985 stream->writeInt(mSemanticIndexes[i]);
986 }
987
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700988 stream->writeInt(mSamplersPS.size());
989 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
990 {
991 stream->writeInt(mSamplersPS[i].active);
992 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
993 stream->writeInt(mSamplersPS[i].textureType);
994 }
995
996 stream->writeInt(mSamplersVS.size());
997 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
998 {
999 stream->writeInt(mSamplersVS[i].active);
1000 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
1001 stream->writeInt(mSamplersVS[i].textureType);
1002 }
1003
1004 stream->writeInt(mUsedVertexSamplerRange);
1005 stream->writeInt(mUsedPixelSamplerRange);
1006
Jamie Madill62d31cb2015-09-11 13:25:51 -04001007 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001008 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001009 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001010 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001011 stream->writeInt(uniform->psRegisterIndex);
1012 stream->writeInt(uniform->vsRegisterIndex);
1013 stream->writeInt(uniform->registerCount);
1014 stream->writeInt(uniform->registerElement);
1015 }
1016
1017 stream->writeInt(mD3DUniformBlocks.size());
1018 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1019 {
1020 stream->writeInt(uniformBlock.psRegisterIndex);
1021 stream->writeInt(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001022 }
1023
Jamie Madill9fc36822015-11-18 13:08:07 -05001024 stream->writeInt(mStreamOutVaryings.size());
1025 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001026 {
Brandon Joneseb994362014-09-24 10:27:28 -07001027 stream->writeString(varying.semanticName);
1028 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001029 stream->writeInt(varying.componentCount);
1030 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001031 }
1032
Brandon Jones22502d52014-08-29 16:58:36 -07001033 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001034 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1035 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001036 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001037 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1038 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001039 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001040 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001041 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001042
Brandon Joneseb994362014-09-24 10:27:28 -07001043 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001044 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001045 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1046 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001047 {
Brandon Joneseb994362014-09-24 10:27:28 -07001048 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001049 stream->writeInt(variable.type);
1050 stream->writeString(variable.name);
1051 stream->writeString(variable.source);
1052 stream->writeInt(variable.outputIndex);
1053 }
1054
Jamie Madill4e31ad52015-10-29 10:32:57 -04001055 stream->writeString(mGeometryShaderPreamble);
1056
Brandon Joneseb994362014-09-24 10:27:28 -07001057 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001058 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1059 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001060 {
1061 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1062
Jamie Madilld3dfda22015-07-06 08:28:49 -04001063 const auto &inputLayout = vertexExecutable->inputs();
1064 stream->writeInt(inputLayout.size());
1065
1066 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001067 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001068 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -07001069 }
1070
1071 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1072 stream->writeInt(vertexShaderSize);
1073
1074 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1075 stream->writeBytes(vertexBlob, vertexShaderSize);
1076 }
1077
1078 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001079 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1080 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001081 {
1082 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1083
1084 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1085 stream->writeInt(outputs.size());
1086 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1087 {
1088 stream->writeInt(outputs[outputIndex]);
1089 }
1090
1091 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1092 stream->writeInt(pixelShaderSize);
1093
1094 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1095 stream->writeBytes(pixelBlob, pixelShaderSize);
1096 }
1097
Jamie Madill4e31ad52015-10-29 10:32:57 -04001098 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001099 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001100 if (geometryExe == nullptr)
1101 {
1102 stream->writeInt(0);
1103 continue;
1104 }
1105
1106 size_t geometryShaderSize = geometryExe->getLength();
1107 stream->writeInt(geometryShaderSize);
1108 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001109 }
1110
Geoff Langb543aff2014-09-30 14:52:54 -04001111 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001112}
1113
Geoff Langc5629752015-12-07 16:29:04 -05001114void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1115{
1116}
1117
Jamie Madill334d6152015-10-22 14:00:28 -04001118gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1119 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001120{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001121 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001122
Jamie Madill85a18042015-03-05 15:41:41 -05001123 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001124 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001125
1126 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1127 {
1128 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1129
1130 if (colorbuffer)
1131 {
Jamie Madill334d6152015-10-22 14:00:28 -04001132 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1133 ? GL_COLOR_ATTACHMENT0
1134 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001135 }
1136 else
1137 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001138 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001139 }
1140 }
1141
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001142 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001143}
1144
Jamie Madill97399232014-12-23 12:31:15 -05001145gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001146 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001147 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001148{
1149 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1150 {
1151 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1152 {
Geoff Langb543aff2014-09-30 14:52:54 -04001153 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1154 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001155 }
1156 }
1157
Jamie Madill334d6152015-10-22 14:00:28 -04001158 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1159 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001160
1161 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001162 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001163
1164 gl::InfoLog tempInfoLog;
1165 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1166
Jamie Madillada9ecc2015-08-17 12:53:37 -04001167 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001168 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001169 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
1170 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001171 if (error.isError())
1172 {
1173 return error;
1174 }
Brandon Joneseb994362014-09-24 10:27:28 -07001175
Jamie Madill97399232014-12-23 12:31:15 -05001176 if (pixelExecutable)
1177 {
1178 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1179 }
1180 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001181 {
1182 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001183 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001184 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1185 }
Brandon Jones22502d52014-08-29 16:58:36 -07001186
Geoff Langb543aff2014-09-30 14:52:54 -04001187 *outExectuable = pixelExecutable;
1188 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001189}
1190
Jamie Madilld3dfda22015-07-06 08:28:49 -04001191gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001192 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001193 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001194{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001195 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001196
1197 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1198 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001199 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001200 {
Geoff Langb543aff2014-09-30 14:52:54 -04001201 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1202 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001203 }
1204 }
1205
Brandon Jones22502d52014-08-29 16:58:36 -07001206 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001207 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1208 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001209
1210 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001211 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001212
1213 gl::InfoLog tempInfoLog;
1214 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1215
Jamie Madillada9ecc2015-08-17 12:53:37 -04001216 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001217 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001218 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1219 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001220 if (error.isError())
1221 {
1222 return error;
1223 }
1224
Jamie Madill97399232014-12-23 12:31:15 -05001225 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001226 {
Jamie Madill334d6152015-10-22 14:00:28 -04001227 mVertexExecutables.push_back(
1228 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001229 }
Jamie Madill97399232014-12-23 12:31:15 -05001230 else if (!infoLog)
1231 {
1232 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001233 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001234 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1235 }
Brandon Jones22502d52014-08-29 16:58:36 -07001236
Geoff Langb543aff2014-09-30 14:52:54 -04001237 *outExectuable = vertexExecutable;
1238 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001239}
1240
Jamie Madill4e31ad52015-10-29 10:32:57 -04001241gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Data &data,
1242 GLenum drawMode,
1243 ShaderExecutableD3D **outExecutable,
1244 gl::InfoLog *infoLog)
1245{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001246 if (outExecutable)
1247 {
1248 *outExecutable = nullptr;
1249 }
1250
Austin Kinross88829e82016-01-12 13:04:41 -08001251 // Return a null shader if the current rendering doesn't use a geometry shader
1252 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001253 {
1254 return gl::Error(GL_NO_ERROR);
1255 }
1256
Jamie Madill4e31ad52015-10-29 10:32:57 -04001257 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1258
1259 if (mGeometryExecutables[geometryShaderType] != nullptr)
1260 {
1261 if (outExecutable)
1262 {
1263 *outExecutable = mGeometryExecutables[geometryShaderType];
1264 }
1265 return gl::Error(GL_NO_ERROR);
1266 }
1267
1268 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001269 geometryShaderType, data, mData, mRenderer->presentPathFastEnabled(),
1270 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001271
1272 gl::InfoLog tempInfoLog;
1273 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1274
1275 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001276 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001277 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
1278 &mGeometryExecutables[geometryShaderType]);
1279
1280 if (!infoLog && error.isError())
1281 {
1282 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1283 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1284 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1285 }
1286
1287 if (outExecutable)
1288 {
1289 *outExecutable = mGeometryExecutables[geometryShaderType];
1290 }
1291 return error;
1292}
1293
1294LinkResult ProgramD3D::compileProgramExecutables(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001295{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001296 const gl::InputLayout &defaultInputLayout =
1297 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001298 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001299 gl::Error error =
1300 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001301 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001302 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001303 return LinkResult(false, error);
1304 }
Austin Kinross434953e2015-02-20 10:49:51 -08001305
Jamie Madill334d6152015-10-22 14:00:28 -04001306 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001307 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001308 error =
1309 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001310 if (error.isError())
1311 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001312 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001313 }
Brandon Jones44151a92014-09-10 11:32:25 -07001314
Jamie Madill4e31ad52015-10-29 10:32:57 -04001315 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill847638a2015-11-20 13:01:41 -05001316 ShaderExecutableD3D *pointGS = nullptr;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001317 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001318 {
Jamie Madill847638a2015-11-20 13:01:41 -05001319 getGeometryExecutableForPrimitiveType(data, GL_POINTS, &pointGS, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001320 }
1321
Jamie Madillca03b352015-09-02 12:38:13 -04001322 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001323
1324 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001325 {
Jamie Madill334d6152015-10-22 14:00:28 -04001326 // Geometry shaders are currently only used internally, so there is no corresponding shader
1327 // object at the interface level. For now the geometry shader debug info is prepended to
1328 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001329 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001330 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001331 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1332 }
1333
1334 if (defaultVertexExecutable)
1335 {
1336 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1337 }
1338
1339 if (defaultPixelExecutable)
1340 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001341 const ShaderD3D *fragmentShaderD3D =
1342 GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001343 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1344 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001345
Jamie Madill847638a2015-11-20 13:01:41 -05001346 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable &&
1347 (!usesGeometryShader(GL_POINTS) || pointGS));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001348 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001349}
1350
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001351LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001352{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001353 reset();
1354
1355 // TODO(jmadill): structures containing samplers
1356 for (const gl::LinkedUniform &linkedUniform : mData.getUniforms())
1357 {
1358 if (linkedUniform.isSampler() && linkedUniform.isField())
1359 {
1360 infoLog << "Structures containing samplers not currently supported in D3D.";
1361 return LinkResult(false, gl::Error(GL_NO_ERROR));
1362 }
1363 }
1364
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001365 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1366 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001367
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001368 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1369 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1370
Jamie Madill63069df2015-09-01 17:26:41 +00001371 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001372 mSamplersPS.resize(data.caps->maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001373
Arun Patole44efa0b2015-03-04 17:11:05 +05301374 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001375 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1376
Austin Kinross02df7962015-07-01 10:03:42 -07001377 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1378 {
1379 if (fragmentShaderD3D->usesFrontFacing())
1380 {
1381 infoLog << "The current renderer doesn't support gl_FrontFacing";
1382 return LinkResult(false, gl::Error(GL_NO_ERROR));
1383 }
1384 }
1385
Jamie Madillca03b352015-09-02 12:38:13 -04001386 std::vector<PackedVarying> packedVaryings =
1387 MergeVaryings(*vertexShader, *fragmentShader, mData.getTransformFeedbackVaryingNames());
1388
Brandon Jones22502d52014-08-29 16:58:36 -07001389 // Map the varyings to the register file
Jamie Madill9fc36822015-11-18 13:08:07 -05001390 VaryingPacking varyingPacking(data.caps->maxVaryingVectors);
1391 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
1392 mData.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001393 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001394 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001395 }
1396
Jamie Madille39a3f02015-11-17 20:42:15 -05001397 ProgramD3DMetadata metadata(mRenderer->getMajorShaderModel(), mRenderer->getShaderModelSuffix(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001398 usesInstancedPointSpriteEmulation(),
1399 mRenderer->presentPathFastEnabled(), vertexShaderD3D,
Jamie Madille39a3f02015-11-17 20:42:15 -05001400 fragmentShaderD3D);
1401
Jamie Madill9fc36822015-11-18 13:08:07 -05001402 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1403 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1404
1405 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.caps->maxVaryingVectors)
1406 {
1407 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1408 return LinkResult(false, gl::Error(GL_NO_ERROR));
1409 }
1410
1411 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1412 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1413 // intelligently, but D3D9 assumes one semantic per register.
1414 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1415 varyingPacking.getMaxSemanticIndex() > data.caps->maxVaryingVectors)
1416 {
1417 infoLog << "Cannot pack these varyings on D3D9.";
1418 return LinkResult(false, gl::Error(GL_NO_ERROR));
1419 }
1420
1421 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mData, metadata, varyingPacking, &mPixelHLSL,
1422 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001423 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001424 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001425 }
1426
Brandon Jones44151a92014-09-10 11:32:25 -07001427 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madille39a3f02015-11-17 20:42:15 -05001428 mDynamicHLSL->getPixelShaderOutputKey(data, mData, metadata, &mPixelShaderKey);
1429 mUsesFragDepth = metadata.usesFragDepth(mData);
Brandon Jones44151a92014-09-10 11:32:25 -07001430
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001431 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001432 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001433 for (const auto &varying : packedVaryings)
1434 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001435 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001436 {
1437 mUsesFlatInterpolation = true;
1438 break;
1439 }
1440 }
1441
Jamie Madill4e31ad52015-10-29 10:32:57 -04001442 if (mRenderer->getMajorShaderModel() >= 4)
1443 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001444 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1445 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001446 }
1447
Jamie Madill63805b42015-08-25 13:17:39 -04001448 initSemanticIndex();
Jamie Madill437d2662014-12-05 14:23:35 -05001449
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001450 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001451
Jamie Madill9fc36822015-11-18 13:08:07 -05001452 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001453
Jamie Madill4e31ad52015-10-29 10:32:57 -04001454 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001455 if (result.error.isError() || !result.linkSuccess)
1456 {
1457 infoLog << "Failed to create D3D shaders.";
1458 return result;
1459 }
1460
Jamie Madill4a3c2342015-10-08 12:58:45 -04001461 initUniformBlockInfo();
1462
Geoff Lang7dd2e102014-11-10 15:19:26 -05001463 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001464}
1465
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001466GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001467{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001468 // TODO(jmadill): Do something useful here?
1469 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001470}
1471
Jamie Madill4a3c2342015-10-08 12:58:45 -04001472void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001473{
1474 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1475
Jamie Madill62d31cb2015-09-11 13:25:51 -04001476 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1477 {
1478 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1479 continue;
1480
Jamie Madill4a3c2342015-10-08 12:58:45 -04001481 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001482 continue;
1483
Jamie Madill4a3c2342015-10-08 12:58:45 -04001484 size_t dataSize = getUniformBlockInfo(vertexBlock);
1485 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001486 }
1487
1488 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1489
1490 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1491 {
1492 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1493 continue;
1494
Jamie Madill4a3c2342015-10-08 12:58:45 -04001495 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001496 continue;
1497
Jamie Madill4a3c2342015-10-08 12:58:45 -04001498 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1499 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001500 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001501}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001502
Jamie Madill4a3c2342015-10-08 12:58:45 -04001503void ProgramD3D::assignUniformBlockRegisters()
1504{
1505 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001506
1507 // Assign registers and update sizes.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001508 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1509 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001510
Jamie Madill4a3c2342015-10-08 12:58:45 -04001511 for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001512 {
1513 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1514
Jamie Madill4a3c2342015-10-08 12:58:45 -04001515 D3DUniformBlock d3dUniformBlock;
1516
Jamie Madill62d31cb2015-09-11 13:25:51 -04001517 if (uniformBlock.vertexStaticUse)
1518 {
1519 unsigned int baseRegister =
1520 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001521 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001522 }
1523
1524 if (uniformBlock.fragmentStaticUse)
1525 {
1526 unsigned int baseRegister =
1527 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001528 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001529 }
1530
Jamie Madill4a3c2342015-10-08 12:58:45 -04001531 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001532 }
1533}
1534
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001535void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001536{
1537 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001538 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001539 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001540 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001541 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001542 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001543 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001544 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001545 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001546 vertexRegisters = std::max(vertexRegisters,
1547 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001548 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001549 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001550 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001551 fragmentRegisters = std::max(
1552 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001553 }
1554 }
1555 }
1556
Jamie Madill334d6152015-10-22 14:00:28 -04001557 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001558 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1559}
1560
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001561gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001562{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001563 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001564
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001565 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001566 if (error.isError())
1567 {
1568 return error;
1569 }
1570
Jamie Madill62d31cb2015-09-11 13:25:51 -04001571 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001572 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001573 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001574 }
1575
1576 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001577}
1578
Jamie Madilld1fe1642015-08-21 16:26:04 -04001579gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001580{
Jamie Madill4a3c2342015-10-08 12:58:45 -04001581 if (mData.getUniformBlocks().empty())
1582 {
1583 return gl::Error(GL_NO_ERROR);
1584 }
1585
1586 // Lazy init.
1587 if (mD3DUniformBlocks.empty())
1588 {
1589 assignUniformBlockRegisters();
1590 }
1591
Jamie Madill03260fa2015-06-22 13:57:22 -04001592 mVertexUBOCache.clear();
1593 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001594
1595 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1596 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1597
Jamie Madill4a3c2342015-10-08 12:58:45 -04001598 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001599 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001600 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001601 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
1602 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001603
Brandon Jones18bd4102014-09-22 14:21:44 -07001604 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001605 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001606 {
1607 continue;
1608 }
1609
Jamie Madill4a3c2342015-10-08 12:58:45 -04001610 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001611 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001612 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001613 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001614
Jamie Madill969194d2015-07-20 14:36:56 -04001615 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001616 {
1617 mVertexUBOCache.resize(registerIndex + 1, -1);
1618 }
1619
1620 ASSERT(mVertexUBOCache[registerIndex] == -1);
1621 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001622 }
1623
Jamie Madill4a3c2342015-10-08 12:58:45 -04001624 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001625 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001626 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001627 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001628
1629 if (mFragmentUBOCache.size() <= registerIndex)
1630 {
1631 mFragmentUBOCache.resize(registerIndex + 1, -1);
1632 }
1633
1634 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1635 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001636 }
1637 }
1638
Jamie Madill03260fa2015-06-22 13:57:22 -04001639 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001640}
1641
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001642void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001643{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001644 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001645 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001646 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001647 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001648}
1649
Jamie Madill334d6152015-10-22 14:00:28 -04001650void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001651{
1652 setUniform(location, count, v, GL_FLOAT);
1653}
1654
1655void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1656{
1657 setUniform(location, count, v, GL_FLOAT_VEC2);
1658}
1659
1660void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1661{
1662 setUniform(location, count, v, GL_FLOAT_VEC3);
1663}
1664
1665void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1666{
1667 setUniform(location, count, v, GL_FLOAT_VEC4);
1668}
1669
Jamie Madill334d6152015-10-22 14:00:28 -04001670void ProgramD3D::setUniformMatrix2fv(GLint location,
1671 GLsizei count,
1672 GLboolean transpose,
1673 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001674{
1675 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1676}
1677
Jamie Madill334d6152015-10-22 14:00:28 -04001678void ProgramD3D::setUniformMatrix3fv(GLint location,
1679 GLsizei count,
1680 GLboolean transpose,
1681 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001682{
1683 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1684}
1685
Jamie Madill334d6152015-10-22 14:00:28 -04001686void ProgramD3D::setUniformMatrix4fv(GLint location,
1687 GLsizei count,
1688 GLboolean transpose,
1689 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001690{
1691 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1692}
1693
Jamie Madill334d6152015-10-22 14:00:28 -04001694void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1695 GLsizei count,
1696 GLboolean transpose,
1697 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001698{
1699 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1700}
1701
Jamie Madill334d6152015-10-22 14:00:28 -04001702void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1703 GLsizei count,
1704 GLboolean transpose,
1705 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001706{
1707 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1708}
1709
Jamie Madill334d6152015-10-22 14:00:28 -04001710void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1711 GLsizei count,
1712 GLboolean transpose,
1713 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001714{
1715 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1716}
1717
Jamie Madill334d6152015-10-22 14:00:28 -04001718void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1719 GLsizei count,
1720 GLboolean transpose,
1721 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001722{
1723 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1724}
1725
Jamie Madill334d6152015-10-22 14:00:28 -04001726void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1727 GLsizei count,
1728 GLboolean transpose,
1729 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001730{
1731 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1732}
1733
Jamie Madill334d6152015-10-22 14:00:28 -04001734void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1735 GLsizei count,
1736 GLboolean transpose,
1737 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001738{
1739 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1740}
1741
1742void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1743{
1744 setUniform(location, count, v, GL_INT);
1745}
1746
1747void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1748{
1749 setUniform(location, count, v, GL_INT_VEC2);
1750}
1751
1752void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1753{
1754 setUniform(location, count, v, GL_INT_VEC3);
1755}
1756
1757void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1758{
1759 setUniform(location, count, v, GL_INT_VEC4);
1760}
1761
1762void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1763{
1764 setUniform(location, count, v, GL_UNSIGNED_INT);
1765}
1766
1767void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1768{
1769 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1770}
1771
1772void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1773{
1774 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1775}
1776
1777void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1778{
1779 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1780}
1781
Jamie Madill4a3c2342015-10-08 12:58:45 -04001782void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1783 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001784{
1785}
1786
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001787void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001788{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001789 D3DUniformMap uniformMap;
Jamie Madill334d6152015-10-22 14:00:28 -04001790 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001791 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001792
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001793 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001794 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001795 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001796 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001797 }
1798 }
1799
Jamie Madill334d6152015-10-22 14:00:28 -04001800 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001801 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001802 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001803 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001804 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001805 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001806 }
1807 }
1808
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001809 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
1810 for (const gl::LinkedUniform &glUniform : mData.getUniforms())
1811 {
1812 if (!glUniform.isInDefaultBlock())
1813 continue;
1814
1815 auto mapEntry = uniformMap.find(glUniform.name);
1816 ASSERT(mapEntry != uniformMap.end());
1817 mD3DUniforms.push_back(mapEntry->second);
1818 }
1819
Jamie Madill62d31cb2015-09-11 13:25:51 -04001820 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001821 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001822}
1823
Jamie Madill91445bc2015-09-23 16:47:53 -04001824void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001825 const sh::Uniform &uniform,
1826 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001827{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001828 if (uniform.isBuiltIn())
Jamie Madillfb536032015-09-11 13:19:49 -04001829 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001830 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001831 return;
1832 }
1833
Jamie Madill91445bc2015-09-23 16:47:53 -04001834 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1835
1836 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1837 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001838 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001839 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001840
Jamie Madill91445bc2015-09-23 16:47:53 -04001841 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001842}
1843
Jamie Madill62d31cb2015-09-11 13:25:51 -04001844D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1845{
1846 for (D3DUniform *d3dUniform : mD3DUniforms)
1847 {
1848 if (d3dUniform->name == name)
1849 {
1850 return d3dUniform;
1851 }
1852 }
1853
1854 return nullptr;
1855}
1856
Jamie Madill91445bc2015-09-23 16:47:53 -04001857void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001858 const sh::ShaderVariable &uniform,
1859 const std::string &fullName,
1860 sh::HLSLBlockEncoder *encoder,
1861 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001862{
1863 if (uniform.isStruct())
1864 {
1865 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1866 {
1867 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1868
Jamie Madill55def582015-05-04 11:24:57 -04001869 if (encoder)
1870 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001871
1872 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1873 {
Jamie Madill334d6152015-10-22 14:00:28 -04001874 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001875 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1876
Jamie Madill91445bc2015-09-23 16:47:53 -04001877 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001878 }
1879
Jamie Madill55def582015-05-04 11:24:57 -04001880 if (encoder)
1881 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001882 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001883 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001884 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001885
1886 // Not a struct. Arrays are treated as aggregate types.
1887 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001888 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001889 encoder->enterAggregateType();
1890 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001891
Jamie Madill62d31cb2015-09-11 13:25:51 -04001892 // Advance the uniform offset, to track registers allocation for structs
1893 sh::BlockMemberInfo blockInfo =
1894 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1895 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001896
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001897 auto uniformMapEntry = uniformMap->find(fullName);
1898 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001899
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001900 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001901 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001902 d3dUniform = uniformMapEntry->second;
1903 }
1904 else
1905 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001906 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001907 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001908 }
1909
1910 if (encoder)
1911 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001912 d3dUniform->registerElement =
1913 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001914 unsigned int reg =
1915 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001916 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001917 {
1918 d3dUniform->psRegisterIndex = reg;
1919 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001920 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001921 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001922 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001923 d3dUniform->vsRegisterIndex = reg;
1924 }
Jamie Madillfb536032015-09-11 13:19:49 -04001925
1926 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001927 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001928 {
1929 encoder->exitAggregateType();
1930 }
1931 }
1932}
1933
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001934template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001935void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001936{
Jamie Madill334d6152015-10-22 14:00:28 -04001937 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001938 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1939
Jamie Madill62d31cb2015-09-11 13:25:51 -04001940 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001941
Jamie Madill62d31cb2015-09-11 13:25:51 -04001942 unsigned int elementCount = targetUniform->elementCount();
1943 unsigned int arrayElement = mData.getUniformLocations()[location].element;
1944 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001945
1946 if (targetUniform->type == targetUniformType)
1947 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001948 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001949
Jamie Madill62d31cb2015-09-11 13:25:51 -04001950 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001951 {
Jamie Madill334d6152015-10-22 14:00:28 -04001952 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001953 const T *source = v + (i * components);
1954
1955 for (int c = 0; c < components; c++)
1956 {
1957 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1958 }
1959 for (int c = components; c < 4; c++)
1960 {
1961 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1962 }
1963 }
1964 }
1965 else if (targetUniform->type == targetBoolType)
1966 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001967 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001968
Jamie Madill62d31cb2015-09-11 13:25:51 -04001969 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001970 {
Jamie Madill334d6152015-10-22 14:00:28 -04001971 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001972 const T *source = v + (i * components);
1973
1974 for (int c = 0; c < components; c++)
1975 {
Jamie Madill334d6152015-10-22 14:00:28 -04001976 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1977 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001978 }
1979 for (int c = components; c < 4; c++)
1980 {
1981 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1982 }
1983 }
1984 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001985 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986 {
1987 ASSERT(targetUniformType == GL_INT);
1988
Jamie Madill62d31cb2015-09-11 13:25:51 -04001989 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001990
1991 bool wasDirty = targetUniform->dirty;
1992
Jamie Madill62d31cb2015-09-11 13:25:51 -04001993 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001994 {
Jamie Madill334d6152015-10-22 14:00:28 -04001995 GLint *dest = target + (i * 4);
1996 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001997
1998 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1999 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2000 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2001 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2002 }
2003
2004 if (!wasDirty && targetUniform->dirty)
2005 {
2006 mDirtySamplerMapping = true;
2007 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002008 }
Jamie Madill334d6152015-10-22 14:00:28 -04002009 else
2010 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002011}
2012
2013template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002014void ProgramD3D::setUniformMatrixfv(GLint location,
2015 GLsizei countIn,
2016 GLboolean transpose,
2017 const GLfloat *value,
2018 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002019{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002020 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002021
Jamie Madill62d31cb2015-09-11 13:25:51 -04002022 unsigned int elementCount = targetUniform->elementCount();
2023 unsigned int arrayElement = mData.getUniformLocations()[location].element;
2024 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002025
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002026 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002027 GLfloat *target =
2028 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002029
Jamie Madill62d31cb2015-09-11 13:25:51 -04002030 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002031 {
2032 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2033 if (transpose == GL_FALSE)
2034 {
Jamie Madill334d6152015-10-22 14:00:28 -04002035 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2036 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002037 }
2038 else
2039 {
Jamie Madill334d6152015-10-22 14:00:28 -04002040 targetUniform->dirty =
2041 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002042 }
2043 target += targetMatrixStride;
2044 value += cols * rows;
2045 }
2046}
2047
Jamie Madill4a3c2342015-10-08 12:58:45 -04002048size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002049{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002050 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002051
Jamie Madill62d31cb2015-09-11 13:25:51 -04002052 // define member uniforms
2053 sh::Std140BlockEncoder std140Encoder;
2054 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2055 sh::BlockLayoutEncoder *encoder = nullptr;
2056
2057 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002058 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002059 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002060 }
2061 else
2062 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002063 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002064 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002065
2066 GetUniformBlockInfo(interfaceBlock.fields, "", encoder, interfaceBlock.isRowMajorLayout,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002067 &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002068
2069 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002070}
2071
Jamie Madill62d31cb2015-09-11 13:25:51 -04002072void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002073{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002074 for (const D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002075 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002076 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002077 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002078 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002079 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002080 }
2081}
2082
Jamie Madill62d31cb2015-09-11 13:25:51 -04002083void ProgramD3D::assignSamplerRegisters(const D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002084{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002085 ASSERT(d3dUniform->isSampler());
2086 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX ||
2087 d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madillfb536032015-09-11 13:19:49 -04002088
Jamie Madill62d31cb2015-09-11 13:25:51 -04002089 if (d3dUniform->vsRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002090 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002091 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2092 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002093 }
2094
Jamie Madill62d31cb2015-09-11 13:25:51 -04002095 if (d3dUniform->psRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002096 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002097 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2098 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002099 }
2100}
2101
Jamie Madill62d31cb2015-09-11 13:25:51 -04002102// static
2103void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002104 GLenum samplerType,
2105 unsigned int samplerCount,
2106 std::vector<Sampler> &outSamplers,
2107 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002108{
2109 unsigned int samplerIndex = startSamplerIndex;
2110
2111 do
2112 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002113 ASSERT(samplerIndex < outSamplers.size());
2114 Sampler *sampler = &outSamplers[samplerIndex];
2115 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002116 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002117 sampler->logicalTextureUnit = 0;
2118 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002119 samplerIndex++;
2120 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002121}
2122
Brandon Jonesc9610c52014-08-25 17:02:59 -07002123void ProgramD3D::reset()
2124{
Brandon Joneseb994362014-09-24 10:27:28 -07002125 SafeDeleteContainer(mVertexExecutables);
2126 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002127
2128 for (auto &element : mGeometryExecutables)
2129 {
2130 SafeDelete(element);
2131 }
Brandon Joneseb994362014-09-24 10:27:28 -07002132
Brandon Jones22502d52014-08-29 16:58:36 -07002133 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002134 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002135
2136 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002137 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002138 mUsesFragDepth = false;
2139 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002140 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002141 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002142
Jamie Madill62d31cb2015-09-11 13:25:51 -04002143 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002144 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002145
Brandon Jonesc9610c52014-08-25 17:02:59 -07002146 SafeDelete(mVertexUniformStorage);
2147 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002148
2149 mSamplersPS.clear();
2150 mSamplersVS.clear();
2151
2152 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002153 mUsedPixelSamplerRange = 0;
2154 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002155
Jamie Madill63805b42015-08-25 13:17:39 -04002156 std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1);
Jamie Madill437d2662014-12-05 14:23:35 -05002157 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002158
Jamie Madill9fc36822015-11-18 13:08:07 -05002159 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002160
2161 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002162}
2163
Geoff Lang7dd2e102014-11-10 15:19:26 -05002164unsigned int ProgramD3D::getSerial() const
2165{
2166 return mSerial;
2167}
2168
2169unsigned int ProgramD3D::issueSerial()
2170{
2171 return mCurrentSerial++;
2172}
2173
Jamie Madill63805b42015-08-25 13:17:39 -04002174void ProgramD3D::initSemanticIndex()
2175{
2176 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2177 ASSERT(vertexShader != nullptr);
2178
2179 // Init semantic index
2180 for (const sh::Attribute &attribute : mData.getAttributes())
2181 {
2182 int attributeIndex = attribute.location;
2183 int index = vertexShader->getSemanticIndex(attribute.name);
2184 int regs = gl::VariableRegisterCount(attribute.type);
2185
2186 for (int reg = 0; reg < regs; ++reg)
2187 {
2188 mSemanticIndexes[attributeIndex + reg] = index + reg;
2189 }
2190 }
2191
2192 initAttributesByLayout();
2193}
2194
Jamie Madill437d2662014-12-05 14:23:35 -05002195void ProgramD3D::initAttributesByLayout()
2196{
2197 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2198 {
2199 mAttributesByLayout[i] = i;
2200 }
2201
Jamie Madill63805b42015-08-25 13:17:39 -04002202 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS],
2203 AttributeSorter(mSemanticIndexes));
Jamie Madill437d2662014-12-05 14:23:35 -05002204}
2205
Jamie Madill334d6152015-10-22 14:00:28 -04002206void ProgramD3D::sortAttributesByLayout(
2207 const std::vector<TranslatedAttribute> &unsortedAttributes,
2208 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2209 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002210{
Jamie Madill476682e2015-06-30 10:04:29 -04002211 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002212 {
Jamie Madill334d6152015-10-22 14:00:28 -04002213 int oldIndex = mAttributesByLayout[attribIndex];
Jamie Madill63805b42015-08-25 13:17:39 -04002214 sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex];
Jamie Madill334d6152015-10-22 14:00:28 -04002215 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002216 }
2217}
2218
Jamie Madill63805b42015-08-25 13:17:39 -04002219void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002220{
Jamie Madillbd136f92015-08-10 14:51:37 -04002221 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002222 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002223
Dian Xianga4928832015-09-15 10:11:17 -07002224 for (unsigned int attributeIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002225 {
Jamie Madill63805b42015-08-25 13:17:39 -04002226 int semanticIndex = mSemanticIndexes[attributeIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002227
2228 if (semanticIndex != -1)
2229 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002230 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2231 {
2232 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2233 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002234 mCachedInputLayout[semanticIndex] =
2235 GetVertexFormatType(vertexAttributes[attributeIndex],
2236 state.getVertexAttribCurrentValue(attributeIndex).Type);
2237 }
2238 }
2239}
2240
Jamie Madill9fc36822015-11-18 13:08:07 -05002241void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002242{
Jamie Madill9fc36822015-11-18 13:08:07 -05002243 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2244
2245 const std::string &varyingSemantic =
2246 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2247
Jamie Madillccdf74b2015-08-18 10:46:12 -04002248 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002249 mStreamOutVaryings.clear();
2250
2251 const auto &tfVaryingNames = mData.getTransformFeedbackVaryingNames();
2252 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2253 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002254 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002255 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2256 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002257 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002258 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002259 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002260 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2261 builtins.glPosition.index, 4, outputSlot));
2262 }
2263 }
2264 else if (tfVaryingName == "gl_FragCoord")
2265 {
2266 if (builtins.glFragCoord.enabled)
2267 {
2268 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2269 builtins.glFragCoord.index, 4, outputSlot));
2270 }
2271 }
2272 else if (tfVaryingName == "gl_PointSize")
2273 {
2274 if (builtins.glPointSize.enabled)
2275 {
2276 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2277 }
2278 }
2279 else
2280 {
2281 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2282 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002283 const auto &varying = *registerInfo.packedVarying->varying;
2284 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002285 int componentCount = gl::VariableColumnCount(transposedType);
2286 ASSERT(!varying.isBuiltIn());
2287
Jamie Madill55c25d02015-11-18 13:08:08 -05002288 // Transform feedback for varying structs is underspecified.
2289 // See Khronos bug 9856.
2290 // TODO(jmadill): Figure out how to be spec-compliant here.
2291 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2292 continue;
2293
Jamie Madill9fc36822015-11-18 13:08:07 -05002294 // There can be more than one register assigned to a particular varying, and each
2295 // register needs its own stream out entry.
2296 if (tfVaryingName == varying.name)
2297 {
2298 mStreamOutVaryings.push_back(D3DVarying(
2299 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2300 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002301 }
2302 }
2303 }
2304}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002305
2306D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2307{
2308 return mD3DUniforms[mData.getUniformLocations()[location].index];
2309}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002310
2311bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2312{
2313 std::string baseName = blockName;
2314 gl::ParseAndStripArrayIndex(&baseName);
2315
2316 auto sizeIter = mBlockDataSizes.find(baseName);
2317 if (sizeIter == mBlockDataSizes.end())
2318 {
2319 *sizeOut = 0;
2320 return false;
2321 }
2322
2323 *sizeOut = sizeIter->second;
2324 return true;
2325}
2326
2327bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2328 sh::BlockMemberInfo *memberInfoOut) const
2329{
2330 auto infoIter = mBlockInfo.find(memberUniformName);
2331 if (infoIter == mBlockInfo.end())
2332 {
2333 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2334 return false;
2335 }
2336
2337 *memberInfoOut = infoIter->second;
2338 return true;
2339}
Brandon Jonesc9610c52014-08-25 17:02:59 -07002340}