blob: d00a8738ebef381701dd204bd14918d2281385eb [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 Madill53ea9cc2016-05-17 10:12:52 -040016#include "libANGLE/Uniform.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040017#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000018#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050019#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050020#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050021#include "libANGLE/renderer/d3d/RendererD3D.h"
22#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050023#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill65345da2015-11-13 11:25:23 -050024#include "libANGLE/renderer/d3d/VaryingPacking.h"
Jamie Madill437d2662014-12-05 14:23:35 -050025#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050026
Brandon Jonesc9610c52014-08-25 17:02:59 -070027namespace rx
28{
29
Brandon Joneseb994362014-09-24 10:27:28 -070030namespace
31{
32
Jamie Madillf8dd7b12015-08-05 13:50:08 -040033gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070034{
Jamie Madillbd136f92015-08-10 14:51:37 -040035 gl::InputLayout defaultLayout;
36 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070037 {
Brandon Joneseb994362014-09-24 10:27:28 -070038 if (shaderAttr.type != GL_NONE)
39 {
40 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
41
Jamie Madilld3dfda22015-07-06 08:28:49 -040042 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040043 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070044 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040045 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040046 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040047 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040048 gl::VertexFormatType defaultType =
49 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070050
Jamie Madillbd136f92015-08-10 14:51:37 -040051 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070052 }
53 }
54 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040055
56 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070057}
58
Jamie Madill334d6152015-10-22 14:00:28 -040059std::vector<GLenum> GetDefaultOutputLayoutFromShader(
60 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070061{
Jamie Madillb4463142014-12-19 14:56:54 -050062 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070063
Jamie Madillb4463142014-12-19 14:56:54 -050064 if (!shaderOutputVars.empty())
65 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070066 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
67 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050068 }
Brandon Joneseb994362014-09-24 10:27:28 -070069
70 return defaultPixelOutput;
71}
72
Brandon Jones1a8a7e32014-10-01 12:49:30 -070073bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
74{
75 return var.isRowMajorLayout;
76}
77
78bool IsRowMajorLayout(const sh::ShaderVariable &var)
79{
80 return false;
81}
82
Jamie Madill9fc36822015-11-18 13:08:07 -050083// true if varying x has a higher priority in packing than y
84bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
85{
Jamie Madill55c25d02015-11-18 13:08:08 -050086 return gl::CompareShaderVar(*x.varying, *y.varying);
Jamie Madill9fc36822015-11-18 13:08:07 -050087}
88
Jamie Madillca03b352015-09-02 12:38:13 -040089std::vector<PackedVarying> MergeVaryings(const gl::Shader &vertexShader,
90 const gl::Shader &fragmentShader,
91 const std::vector<std::string> &tfVaryings)
Jamie Madillada9ecc2015-08-17 12:53:37 -040092{
Jamie Madillca03b352015-09-02 12:38:13 -040093 std::vector<PackedVarying> packedVaryings;
94
95 for (const sh::Varying &output : vertexShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -040096 {
Jamie Madillca03b352015-09-02 12:38:13 -040097 bool packed = false;
Jamie Madillada9ecc2015-08-17 12:53:37 -040098
99 // Built-in varyings obey special rules
Jamie Madillca03b352015-09-02 12:38:13 -0400100 if (output.isBuiltIn())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400101 {
102 continue;
103 }
104
Jamie Madillca03b352015-09-02 12:38:13 -0400105 for (const sh::Varying &input : fragmentShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400106 {
Jamie Madillca03b352015-09-02 12:38:13 -0400107 if (output.name == input.name)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400108 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500109 if (output.isStruct())
110 {
111 ASSERT(!output.isArray());
112 for (const auto &field : output.fields)
113 {
114 ASSERT(!field.isStruct() && !field.isArray());
115 packedVaryings.push_back(
116 PackedVarying(field, input.interpolation, input.name));
117 }
118 }
119 else
120 {
121 packedVaryings.push_back(PackedVarying(input, input.interpolation));
122 }
Jamie Madillca03b352015-09-02 12:38:13 -0400123 packed = true;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400124 break;
125 }
126 }
127
Jamie Madillca03b352015-09-02 12:38:13 -0400128 // Keep Transform FB varyings in the merged list always.
129 if (!packed)
130 {
131 for (const std::string &tfVarying : tfVaryings)
132 {
133 if (tfVarying == output.name)
134 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500135 // Transform feedback for varying structs is underspecified.
136 // See Khronos bug 9856.
137 // TODO(jmadill): Figure out how to be spec-compliant here.
138 if (!output.isStruct())
139 {
140 packedVaryings.push_back(PackedVarying(output, output.interpolation));
141 packedVaryings.back().vertexOnly = true;
142 }
Jamie Madillca03b352015-09-02 12:38:13 -0400143 break;
144 }
145 }
146 }
Jamie Madillada9ecc2015-08-17 12:53:37 -0400147 }
148
Jamie Madill9fc36822015-11-18 13:08:07 -0500149 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
150
Jamie Madillca03b352015-09-02 12:38:13 -0400151 return packedVaryings;
Brandon Joneseb994362014-09-24 10:27:28 -0700152}
153
Jamie Madill62d31cb2015-09-11 13:25:51 -0400154template <typename VarT>
155void GetUniformBlockInfo(const std::vector<VarT> &fields,
156 const std::string &prefix,
157 sh::BlockLayoutEncoder *encoder,
158 bool inRowMajorLayout,
159 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
160{
161 for (const VarT &field : fields)
162 {
163 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
164
165 if (field.isStruct())
166 {
167 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
168
169 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
170 {
171 encoder->enterAggregateType();
172
173 const std::string uniformElementName =
174 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
175 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
176 blockInfoOut);
177
178 encoder->exitAggregateType();
179 }
180 }
181 else
182 {
183 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
184 (*blockInfoOut)[fieldName] =
185 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
186 }
187 }
188}
189
Jamie Madill334d6152015-10-22 14:00:28 -0400190template <typename T>
Jacek Caban2302e692015-12-01 12:44:43 +0100191static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
192{
193 ASSERT(dest != NULL);
194 ASSERT(dirtyFlag != NULL);
195
196 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
197 *dest = source;
198}
199
200template <typename T>
Jamie Madill334d6152015-10-22 14:00:28 -0400201bool TransposeMatrix(T *target,
202 const GLfloat *value,
203 int targetWidth,
204 int targetHeight,
205 int srcWidth,
206 int srcHeight)
207{
208 bool dirty = false;
209 int copyWidth = std::min(targetHeight, srcWidth);
210 int copyHeight = std::min(targetWidth, srcHeight);
211
212 for (int x = 0; x < copyWidth; x++)
213 {
214 for (int y = 0; y < copyHeight; y++)
215 {
216 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]),
217 &dirty);
218 }
219 }
220 // clear unfilled right side
221 for (int y = 0; y < copyWidth; y++)
222 {
223 for (int x = copyHeight; x < targetWidth; x++)
224 {
225 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
226 }
227 }
228 // clear unfilled bottom.
229 for (int y = copyWidth; y < targetHeight; y++)
230 {
231 for (int x = 0; x < targetWidth; x++)
232 {
233 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
234 }
235 }
236
237 return dirty;
238}
239
240template <typename T>
241bool ExpandMatrix(T *target,
242 const GLfloat *value,
243 int targetWidth,
244 int targetHeight,
245 int srcWidth,
246 int srcHeight)
247{
248 bool dirty = false;
249 int copyWidth = std::min(targetWidth, srcWidth);
250 int copyHeight = std::min(targetHeight, srcHeight);
251
252 for (int y = 0; y < copyHeight; y++)
253 {
254 for (int x = 0; x < copyWidth; x++)
255 {
256 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]),
257 &dirty);
258 }
259 }
260 // clear unfilled right side
261 for (int y = 0; y < copyHeight; y++)
262 {
263 for (int x = copyWidth; x < targetWidth; x++)
264 {
265 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
266 }
267 }
268 // clear unfilled bottom.
269 for (int y = copyHeight; y < targetHeight; y++)
270 {
271 for (int x = 0; x < targetWidth; x++)
272 {
273 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
274 }
275 }
276
277 return dirty;
278}
279
Jamie Madill4e31ad52015-10-29 10:32:57 -0400280gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
281{
282 switch (drawMode)
283 {
284 // Uses the point sprite geometry shader.
285 case GL_POINTS:
286 return gl::PRIMITIVE_POINTS;
287
288 // All line drawing uses the same geometry shader.
289 case GL_LINES:
290 case GL_LINE_STRIP:
291 case GL_LINE_LOOP:
292 return gl::PRIMITIVE_LINES;
293
294 // The triangle fan primitive is emulated with strips in D3D11.
295 case GL_TRIANGLES:
296 case GL_TRIANGLE_FAN:
297 return gl::PRIMITIVE_TRIANGLES;
298
299 // Special case for triangle strips.
300 case GL_TRIANGLE_STRIP:
301 return gl::PRIMITIVE_TRIANGLE_STRIP;
302
303 default:
304 UNREACHABLE();
305 return gl::PRIMITIVE_TYPE_MAX;
306 }
307}
308
Jamie Madillada9ecc2015-08-17 12:53:37 -0400309} // anonymous namespace
310
Jamie Madill28afae52015-11-09 15:07:57 -0500311// D3DUniform Implementation
312
Jamie Madill62d31cb2015-09-11 13:25:51 -0400313D3DUniform::D3DUniform(GLenum typeIn,
314 const std::string &nameIn,
315 unsigned int arraySizeIn,
316 bool defaultBlock)
317 : type(typeIn),
318 name(nameIn),
319 arraySize(arraySizeIn),
320 data(nullptr),
321 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400322 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400323 psRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400324 registerCount(0),
325 registerElement(0)
326{
327 // We use data storage for default block uniforms to cache values that are sent to D3D during
328 // rendering
329 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
330 if (defaultBlock)
331 {
332 size_t bytes = gl::VariableInternalSize(type) * elementCount();
333 data = new uint8_t[bytes];
334 memset(data, 0, bytes);
335
336 // TODO(jmadill): is this correct with non-square matrices?
337 registerCount = gl::VariableRowCount(type) * elementCount();
338 }
339}
340
341D3DUniform::~D3DUniform()
342{
343 SafeDeleteArray(data);
344}
345
346bool D3DUniform::isSampler() const
347{
348 return gl::IsSamplerType(type);
349}
350
351bool D3DUniform::isReferencedByVertexShader() const
352{
353 return vsRegisterIndex != GL_INVALID_INDEX;
354}
355
356bool D3DUniform::isReferencedByFragmentShader() const
357{
358 return psRegisterIndex != GL_INVALID_INDEX;
359}
360
Jamie Madill28afae52015-11-09 15:07:57 -0500361// D3DVarying Implementation
362
Jamie Madill9fc36822015-11-18 13:08:07 -0500363D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500364{
365}
366
Jamie Madill9fc36822015-11-18 13:08:07 -0500367D3DVarying::D3DVarying(const std::string &semanticNameIn,
368 unsigned int semanticIndexIn,
369 unsigned int componentCountIn,
370 unsigned int outputSlotIn)
371 : semanticName(semanticNameIn),
372 semanticIndex(semanticIndexIn),
373 componentCount(componentCountIn),
374 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500375{
376}
377
Jamie Madille39a3f02015-11-17 20:42:15 -0500378// ProgramD3DMetadata Implementation
379
Jamie Madillc9bde922016-07-24 17:58:50 -0400380ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500381 const ShaderD3D *vertexShader,
382 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400383 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
384 mShaderModelSuffix(renderer->getShaderModelSuffix()),
385 mUsesInstancedPointSpriteEmulation(
386 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
387 mUsesViewScale(renderer->presentPathFastEnabled()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500388 mVertexShader(vertexShader),
389 mFragmentShader(fragmentShader)
390{
391}
392
393int ProgramD3DMetadata::getRendererMajorShaderModel() const
394{
395 return mRendererMajorShaderModel;
396}
397
Jamie Madill9082b982016-04-27 15:21:51 -0400398bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500399{
Martin Radev1be913c2016-07-11 17:59:16 +0300400 return (mFragmentShader->usesFragColor() && data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500401}
402
Jamie Madill48ef11b2016-04-27 15:21:52 -0400403bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500404{
Jamie Madill63286672015-11-24 13:00:08 -0500405 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500406}
407
408bool ProgramD3DMetadata::usesPointCoord() const
409{
410 return mFragmentShader->usesPointCoord();
411}
412
413bool ProgramD3DMetadata::usesFragCoord() const
414{
415 return mFragmentShader->usesFragCoord();
416}
417
418bool ProgramD3DMetadata::usesPointSize() const
419{
420 return mVertexShader->usesPointSize();
421}
422
423bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
424{
Jamie Madillc9bde922016-07-24 17:58:50 -0400425 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
426 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500427}
428
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800429bool ProgramD3DMetadata::usesViewScale() const
430{
431 return mUsesViewScale;
432}
433
Jamie Madille39a3f02015-11-17 20:42:15 -0500434bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
435{
Jamie Madillc9bde922016-07-24 17:58:50 -0400436 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500437 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400438 // Even with a geometry shader, the app can render triangles or lines and reference
439 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
440 // simplicity, we always add this to the vertex shader when the fragment shader
441 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500442 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
443 usesInsertedPointCoordValue();
444}
445
446bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
447{
448 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
449 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
450 // the vertex shader in this case. This saves us 1 output vector.
451 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
452}
453
454bool ProgramD3DMetadata::usesSystemValuePointSize() const
455{
456 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
457}
458
459bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
460{
461 return mFragmentShader->usesMultipleRenderTargets();
462}
463
464GLint ProgramD3DMetadata::getMajorShaderVersion() const
465{
466 return mVertexShader->getData().getShaderVersion();
467}
468
469const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
470{
471 return mFragmentShader;
472}
473
Jamie Madill28afae52015-11-09 15:07:57 -0500474// ProgramD3D Implementation
475
Jamie Madilld3dfda22015-07-06 08:28:49 -0400476ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
477 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500478 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400479 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700480{
Brandon Joneseb994362014-09-24 10:27:28 -0700481}
482
483ProgramD3D::VertexExecutable::~VertexExecutable()
484{
485 SafeDelete(mShaderExecutable);
486}
487
Jamie Madilld3dfda22015-07-06 08:28:49 -0400488// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500489ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
490 GLenum type)
491{
492 switch (type)
493 {
494 case GL_INT:
495 return HLSLAttribType::SIGNED_INT;
496 case GL_UNSIGNED_INT:
497 return HLSLAttribType::UNSIGNED_INT;
498 case GL_SIGNED_NORMALIZED:
499 case GL_UNSIGNED_NORMALIZED:
500 case GL_FLOAT:
501 return HLSLAttribType::FLOAT;
502 default:
503 UNREACHABLE();
504 return HLSLAttribType::FLOAT;
505 }
506}
507
508// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400509void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
510 const gl::InputLayout &inputLayout,
511 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700512{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500513 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400514
515 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700516 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400517 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500518 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
519 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400520
Jamie Madillbdec2f42016-03-02 16:35:32 -0500521 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
522 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
523 continue;
524
525 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
526 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700527 }
Brandon Joneseb994362014-09-24 10:27:28 -0700528}
529
Jamie Madilld3dfda22015-07-06 08:28:49 -0400530bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
531{
Jamie Madillbd136f92015-08-10 14:51:37 -0400532 size_t limit = std::max(mSignature.size(), signature.size());
533 for (size_t index = 0; index < limit; ++index)
534 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500535 // treat undefined indexes as FLOAT
536 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
537 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400538 if (a != b)
539 return false;
540 }
541
542 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400543}
544
545ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
546 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400547 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700548{
549}
550
551ProgramD3D::PixelExecutable::~PixelExecutable()
552{
553 SafeDelete(mShaderExecutable);
554}
555
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700556ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
557{
558}
559
Geoff Lang7dd2e102014-11-10 15:19:26 -0500560unsigned int ProgramD3D::mCurrentSerial = 1;
561
Jamie Madill48ef11b2016-04-27 15:21:52 -0400562ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
563 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700564 mRenderer(renderer),
565 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400566 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700567 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400568 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700569 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700570 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700571 mUsedVertexSamplerRange(0),
572 mUsedPixelSamplerRange(0),
573 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500574 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700575{
Brandon Joneseb994362014-09-24 10:27:28 -0700576 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700577}
578
579ProgramD3D::~ProgramD3D()
580{
581 reset();
582 SafeDelete(mDynamicHLSL);
583}
584
Brandon Jones44151a92014-09-10 11:32:25 -0700585bool ProgramD3D::usesPointSpriteEmulation() const
586{
587 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
588}
589
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400590bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700591{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400592 if (drawMode != GL_POINTS)
593 {
594 return mUsesFlatInterpolation;
595 }
596
Cooper Partine6664f02015-01-09 16:22:24 -0800597 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
598}
599
600bool ProgramD3D::usesInstancedPointSpriteEmulation() const
601{
602 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700603}
604
Jamie Madill334d6152015-10-22 14:00:28 -0400605GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
606 unsigned int samplerIndex,
607 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700608{
609 GLint logicalTextureUnit = -1;
610
611 switch (type)
612 {
Jamie Madill334d6152015-10-22 14:00:28 -0400613 case gl::SAMPLER_PIXEL:
614 ASSERT(samplerIndex < caps.maxTextureImageUnits);
615 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
616 {
617 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
618 }
619 break;
620 case gl::SAMPLER_VERTEX:
621 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
622 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
623 {
624 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
625 }
626 break;
627 default:
628 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700629 }
630
Jamie Madill334d6152015-10-22 14:00:28 -0400631 if (logicalTextureUnit >= 0 &&
632 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700633 {
634 return logicalTextureUnit;
635 }
636
637 return -1;
638}
639
640// Returns the texture type for a given Direct3D 9 sampler type and
641// index (0-15 for the pixel shader and 0-3 for the vertex shader).
642GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
643{
644 switch (type)
645 {
Jamie Madill334d6152015-10-22 14:00:28 -0400646 case gl::SAMPLER_PIXEL:
647 ASSERT(samplerIndex < mSamplersPS.size());
648 ASSERT(mSamplersPS[samplerIndex].active);
649 return mSamplersPS[samplerIndex].textureType;
650 case gl::SAMPLER_VERTEX:
651 ASSERT(samplerIndex < mSamplersVS.size());
652 ASSERT(mSamplersVS[samplerIndex].active);
653 return mSamplersVS[samplerIndex].textureType;
654 default:
655 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700656 }
657
658 return GL_TEXTURE_2D;
659}
660
Olli Etuaho618bebc2016-01-15 16:40:00 +0200661GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700662{
663 switch (type)
664 {
Jamie Madill334d6152015-10-22 14:00:28 -0400665 case gl::SAMPLER_PIXEL:
666 return mUsedPixelSamplerRange;
667 case gl::SAMPLER_VERTEX:
668 return mUsedVertexSamplerRange;
669 default:
670 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200671 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700672 }
673}
674
675void ProgramD3D::updateSamplerMapping()
676{
677 if (!mDirtySamplerMapping)
678 {
679 return;
680 }
681
682 mDirtySamplerMapping = false;
683
684 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400685 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700686 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400687 if (!d3dUniform->dirty)
688 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700689
Jamie Madill62d31cb2015-09-11 13:25:51 -0400690 if (!d3dUniform->isSampler())
691 continue;
692
693 int count = d3dUniform->elementCount();
694 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
695
696 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700697 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400698 unsigned int firstIndex = d3dUniform->psRegisterIndex;
699
700 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700701 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400702 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700703
Jamie Madill62d31cb2015-09-11 13:25:51 -0400704 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700705 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400706 ASSERT(mSamplersPS[samplerIndex].active);
707 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700708 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400709 }
710 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700711
Jamie Madill62d31cb2015-09-11 13:25:51 -0400712 if (d3dUniform->isReferencedByVertexShader())
713 {
714 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
715
716 for (int i = 0; i < count; i++)
717 {
718 unsigned int samplerIndex = firstIndex + i;
719
720 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700721 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400722 ASSERT(mSamplersVS[samplerIndex].active);
723 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700724 }
725 }
726 }
727 }
728}
729
Geoff Lang7dd2e102014-11-10 15:19:26 -0500730LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700731{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400732 reset();
733
Jamie Madill334d6152015-10-22 14:00:28 -0400734 DeviceIdentifier binaryDeviceIdentifier = {0};
735 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
736 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700737
738 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
739 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
740 {
741 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500742 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700743 }
744
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500745 int compileFlags = stream->readInt<int>();
746 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
747 {
Jamie Madillf6113162015-05-07 11:49:21 -0400748 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500749 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500750 }
751
Jamie Madill8047c0d2016-03-07 13:02:12 -0500752 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400753 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500754 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400755 }
756
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700757 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
758 for (unsigned int i = 0; i < psSamplerCount; ++i)
759 {
760 Sampler sampler;
761 stream->readBool(&sampler.active);
762 stream->readInt(&sampler.logicalTextureUnit);
763 stream->readInt(&sampler.textureType);
764 mSamplersPS.push_back(sampler);
765 }
766 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
767 for (unsigned int i = 0; i < vsSamplerCount; ++i)
768 {
769 Sampler sampler;
770 stream->readBool(&sampler.active);
771 stream->readInt(&sampler.logicalTextureUnit);
772 stream->readInt(&sampler.textureType);
773 mSamplersVS.push_back(sampler);
774 }
775
776 stream->readInt(&mUsedVertexSamplerRange);
777 stream->readInt(&mUsedPixelSamplerRange);
778
779 const unsigned int uniformCount = stream->readInt<unsigned int>();
780 if (stream->error())
781 {
Jamie Madillf6113162015-05-07 11:49:21 -0400782 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500783 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700784 }
785
Jamie Madill48ef11b2016-04-27 15:21:52 -0400786 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400787 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700788 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
789 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400790 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700791
Jamie Madill62d31cb2015-09-11 13:25:51 -0400792 D3DUniform *d3dUniform =
793 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
794 linkedUniform.isInDefaultBlock());
795 stream->readInt(&d3dUniform->psRegisterIndex);
796 stream->readInt(&d3dUniform->vsRegisterIndex);
797 stream->readInt(&d3dUniform->registerCount);
798 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700799
Jamie Madill62d31cb2015-09-11 13:25:51 -0400800 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700801 }
802
Jamie Madill4a3c2342015-10-08 12:58:45 -0400803 const unsigned int blockCount = stream->readInt<unsigned int>();
804 if (stream->error())
805 {
806 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500807 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400808 }
809
810 ASSERT(mD3DUniformBlocks.empty());
811 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
812 {
813 D3DUniformBlock uniformBlock;
814 stream->readInt(&uniformBlock.psRegisterIndex);
815 stream->readInt(&uniformBlock.vsRegisterIndex);
816 mD3DUniformBlocks.push_back(uniformBlock);
817 }
818
Jamie Madill9fc36822015-11-18 13:08:07 -0500819 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
820 mStreamOutVaryings.resize(streamOutVaryingCount);
821 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700822 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500823 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700824
Jamie Madill28afae52015-11-09 15:07:57 -0500825 stream->readString(&varying->semanticName);
826 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500827 stream->readInt(&varying->componentCount);
828 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700829 }
830
Brandon Jones22502d52014-08-29 16:58:36 -0700831 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400832 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
833 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700834 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400835 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
836 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700837 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700838 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400839 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700840
841 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
842 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400843 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
844 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700845 {
846 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
847 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
848 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
849 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
850 }
851
Jamie Madill4e31ad52015-10-29 10:32:57 -0400852 stream->readString(&mGeometryShaderPreamble);
853
Jamie Madill334d6152015-10-22 14:00:28 -0400854 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700855
Jamie Madillb0a838b2016-11-13 20:02:12 -0500856 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
857
Brandon Joneseb994362014-09-24 10:27:28 -0700858 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400859 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
860 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700861 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400862 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400863 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700864
Jamie Madilld3dfda22015-07-06 08:28:49 -0400865 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700866 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400867 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700868 }
869
Jamie Madill334d6152015-10-22 14:00:28 -0400870 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700871 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400872
Jamie Madillada9ecc2015-08-17 12:53:37 -0400873 ShaderExecutableD3D *shaderExecutable = nullptr;
874
Jamie Madillb0a838b2016-11-13 20:02:12 -0500875 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
876 mStreamOutVaryings, separateAttribs,
877 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400878
Brandon Joneseb994362014-09-24 10:27:28 -0700879 if (!shaderExecutable)
880 {
Jamie Madillf6113162015-05-07 11:49:21 -0400881 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500882 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700883 }
884
885 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400886 VertexExecutable::Signature signature;
887 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700888
889 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400890 mVertexExecutables.push_back(
891 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700892
893 stream->skip(vertexShaderSize);
894 }
895
896 const size_t pixelShaderCount = stream->readInt<unsigned int>();
897 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
898 {
899 const size_t outputCount = stream->readInt<unsigned int>();
900 std::vector<GLenum> outputs(outputCount);
901 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
902 {
903 stream->readInt(&outputs[outputIndex]);
904 }
905
Jamie Madill334d6152015-10-22 14:00:28 -0400906 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700907 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400908 ShaderExecutableD3D *shaderExecutable = nullptr;
909
Jamie Madillb0a838b2016-11-13 20:02:12 -0500910 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
911 mStreamOutVaryings, separateAttribs,
912 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700913
914 if (!shaderExecutable)
915 {
Jamie Madillf6113162015-05-07 11:49:21 -0400916 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500917 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700918 }
919
920 // add new binary
921 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
922
923 stream->skip(pixelShaderSize);
924 }
925
Jamie Madill4e31ad52015-10-29 10:32:57 -0400926 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
927 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700928 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400929 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
930 if (geometryShaderSize == 0)
931 {
932 mGeometryExecutables[geometryExeIndex] = nullptr;
933 continue;
934 }
935
Brandon Joneseb994362014-09-24 10:27:28 -0700936 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400937
Jamie Madillb0a838b2016-11-13 20:02:12 -0500938 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
939 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
940 &mGeometryExecutables[geometryExeIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -0700941
Jamie Madill4e31ad52015-10-29 10:32:57 -0400942 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700943 {
Jamie Madillf6113162015-05-07 11:49:21 -0400944 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500945 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700946 }
947 stream->skip(geometryShaderSize);
948 }
949
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700950 initializeUniformStorage();
951
Jamie Madillb0a838b2016-11-13 20:02:12 -0500952 return true;
Brandon Jones22502d52014-08-29 16:58:36 -0700953}
954
Geoff Langb543aff2014-09-30 14:52:54 -0400955gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700956{
Austin Kinross137b1512015-06-17 16:14:53 -0700957 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400958 // When we load the binary again later, we can validate the device identifier before trying to
959 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700960 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400961 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
962 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700963
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500964 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
965
Jamie Madill8047c0d2016-03-07 13:02:12 -0500966 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400967 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500968 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -0400969 }
970
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700971 stream->writeInt(mSamplersPS.size());
972 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
973 {
974 stream->writeInt(mSamplersPS[i].active);
975 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
976 stream->writeInt(mSamplersPS[i].textureType);
977 }
978
979 stream->writeInt(mSamplersVS.size());
980 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
981 {
982 stream->writeInt(mSamplersVS[i].active);
983 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
984 stream->writeInt(mSamplersVS[i].textureType);
985 }
986
987 stream->writeInt(mUsedVertexSamplerRange);
988 stream->writeInt(mUsedPixelSamplerRange);
989
Jamie Madill62d31cb2015-09-11 13:25:51 -0400990 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -0400991 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700992 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400993 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -0400994 stream->writeIntOrNegOne(uniform->psRegisterIndex);
995 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400996 stream->writeInt(uniform->registerCount);
997 stream->writeInt(uniform->registerElement);
998 }
999
1000 stream->writeInt(mD3DUniformBlocks.size());
1001 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1002 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001003 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1004 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001005 }
1006
Jamie Madill9fc36822015-11-18 13:08:07 -05001007 stream->writeInt(mStreamOutVaryings.size());
1008 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001009 {
Brandon Joneseb994362014-09-24 10:27:28 -07001010 stream->writeString(varying.semanticName);
1011 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001012 stream->writeInt(varying.componentCount);
1013 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001014 }
1015
Brandon Jones22502d52014-08-29 16:58:36 -07001016 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001017 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1018 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001019 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001020 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1021 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001022 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001023 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001024 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001025
Brandon Joneseb994362014-09-24 10:27:28 -07001026 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001027 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001028 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1029 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001030 {
Brandon Joneseb994362014-09-24 10:27:28 -07001031 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001032 stream->writeInt(variable.type);
1033 stream->writeString(variable.name);
1034 stream->writeString(variable.source);
1035 stream->writeInt(variable.outputIndex);
1036 }
1037
Jamie Madill4e31ad52015-10-29 10:32:57 -04001038 stream->writeString(mGeometryShaderPreamble);
1039
Brandon Joneseb994362014-09-24 10:27:28 -07001040 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001041 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1042 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001043 {
1044 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1045
Jamie Madilld3dfda22015-07-06 08:28:49 -04001046 const auto &inputLayout = vertexExecutable->inputs();
1047 stream->writeInt(inputLayout.size());
1048
1049 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001050 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001051 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001052 }
1053
1054 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1055 stream->writeInt(vertexShaderSize);
1056
1057 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1058 stream->writeBytes(vertexBlob, vertexShaderSize);
1059 }
1060
1061 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001062 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1063 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001064 {
1065 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1066
1067 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1068 stream->writeInt(outputs.size());
1069 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1070 {
1071 stream->writeInt(outputs[outputIndex]);
1072 }
1073
1074 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1075 stream->writeInt(pixelShaderSize);
1076
1077 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1078 stream->writeBytes(pixelBlob, pixelShaderSize);
1079 }
1080
Jamie Madill4e31ad52015-10-29 10:32:57 -04001081 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001082 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001083 if (geometryExe == nullptr)
1084 {
1085 stream->writeInt(0);
1086 continue;
1087 }
1088
1089 size_t geometryShaderSize = geometryExe->getLength();
1090 stream->writeInt(geometryShaderSize);
1091 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001092 }
1093
Geoff Langb543aff2014-09-30 14:52:54 -04001094 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001095}
1096
Geoff Langc5629752015-12-07 16:29:04 -05001097void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1098{
1099}
1100
Jamie Madill334d6152015-10-22 14:00:28 -04001101gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1102 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001103{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001104 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001105
Jamie Madill85a18042015-03-05 15:41:41 -05001106 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001107 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001108
1109 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1110 {
1111 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1112
1113 if (colorbuffer)
1114 {
Jamie Madill334d6152015-10-22 14:00:28 -04001115 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1116 ? GL_COLOR_ATTACHMENT0
1117 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001118 }
1119 else
1120 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001121 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001122 }
1123 }
1124
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001125 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001126}
1127
Jamie Madill97399232014-12-23 12:31:15 -05001128gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001129 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001130 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001131{
1132 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1133 {
1134 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1135 {
Geoff Langb543aff2014-09-30 14:52:54 -04001136 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1137 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001138 }
1139 }
1140
Jamie Madill334d6152015-10-22 14:00:28 -04001141 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1142 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001143
1144 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001145 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001146
1147 gl::InfoLog tempInfoLog;
1148 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1149
Jamie Madillada9ecc2015-08-17 12:53:37 -04001150 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001151 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001152 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001153 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001154 if (error.isError())
1155 {
1156 return error;
1157 }
Brandon Joneseb994362014-09-24 10:27:28 -07001158
Jamie Madill97399232014-12-23 12:31:15 -05001159 if (pixelExecutable)
1160 {
1161 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1162 }
1163 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001164 {
1165 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001166 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001167 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1168 }
Brandon Jones22502d52014-08-29 16:58:36 -07001169
Geoff Langb543aff2014-09-30 14:52:54 -04001170 *outExectuable = pixelExecutable;
1171 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001172}
1173
Jamie Madilld3dfda22015-07-06 08:28:49 -04001174gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001175 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001176 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001177{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001178 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001179
1180 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1181 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001182 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001183 {
Geoff Langb543aff2014-09-30 14:52:54 -04001184 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1185 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001186 }
1187 }
1188
Brandon Jones22502d52014-08-29 16:58:36 -07001189 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001190 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001191 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001192
1193 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001194 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001195
1196 gl::InfoLog tempInfoLog;
1197 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1198
Jamie Madillada9ecc2015-08-17 12:53:37 -04001199 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001200 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001201 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001202 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001203 if (error.isError())
1204 {
1205 return error;
1206 }
1207
Jamie Madill97399232014-12-23 12:31:15 -05001208 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001209 {
Jamie Madill334d6152015-10-22 14:00:28 -04001210 mVertexExecutables.push_back(
1211 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001212 }
Jamie Madill97399232014-12-23 12:31:15 -05001213 else if (!infoLog)
1214 {
1215 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001216 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001217 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1218 }
Brandon Jones22502d52014-08-29 16:58:36 -07001219
Geoff Langb543aff2014-09-30 14:52:54 -04001220 *outExectuable = vertexExecutable;
1221 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001222}
1223
Jamie Madill9082b982016-04-27 15:21:51 -04001224gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001225 GLenum drawMode,
1226 ShaderExecutableD3D **outExecutable,
1227 gl::InfoLog *infoLog)
1228{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001229 if (outExecutable)
1230 {
1231 *outExecutable = nullptr;
1232 }
1233
Austin Kinross88829e82016-01-12 13:04:41 -08001234 // Return a null shader if the current rendering doesn't use a geometry shader
1235 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001236 {
1237 return gl::Error(GL_NO_ERROR);
1238 }
1239
Jamie Madill4e31ad52015-10-29 10:32:57 -04001240 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1241
1242 if (mGeometryExecutables[geometryShaderType] != nullptr)
1243 {
1244 if (outExecutable)
1245 {
1246 *outExecutable = mGeometryExecutables[geometryShaderType];
1247 }
1248 return gl::Error(GL_NO_ERROR);
1249 }
1250
1251 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001252 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001253 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001254
1255 gl::InfoLog tempInfoLog;
1256 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1257
1258 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001259 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001260 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
Jamie Madill4e31ad52015-10-29 10:32:57 -04001261 &mGeometryExecutables[geometryShaderType]);
1262
1263 if (!infoLog && error.isError())
1264 {
1265 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1266 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1267 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1268 }
1269
1270 if (outExecutable)
1271 {
1272 *outExecutable = mGeometryExecutables[geometryShaderType];
1273 }
1274 return error;
1275}
1276
Jamie Madill9082b982016-04-27 15:21:51 -04001277LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001278{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001279 const gl::InputLayout &defaultInputLayout =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001280 GetDefaultInputLayoutFromShader(mState.getAttachedVertexShader());
Jamie Madillb0a838b2016-11-13 20:02:12 -05001281 ShaderExecutableD3D *defaultVertexExecutable = nullptr;
1282 ANGLE_TRY(
1283 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog));
Austin Kinross434953e2015-02-20 10:49:51 -08001284
Jamie Madill334d6152015-10-22 14:00:28 -04001285 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Jamie Madillb0a838b2016-11-13 20:02:12 -05001286 ShaderExecutableD3D *defaultPixelExecutable = nullptr;
1287 ANGLE_TRY(
1288 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog));
Brandon Jones44151a92014-09-10 11:32:25 -07001289
Jamie Madill4e31ad52015-10-29 10:32:57 -04001290 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill847638a2015-11-20 13:01:41 -05001291 ShaderExecutableD3D *pointGS = nullptr;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001292 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001293 {
Jamie Madill847638a2015-11-20 13:01:41 -05001294 getGeometryExecutableForPrimitiveType(data, GL_POINTS, &pointGS, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001295 }
1296
Jamie Madill48ef11b2016-04-27 15:21:52 -04001297 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001298
1299 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001300 {
Jamie Madill334d6152015-10-22 14:00:28 -04001301 // Geometry shaders are currently only used internally, so there is no corresponding shader
1302 // object at the interface level. For now the geometry shader debug info is prepended to
1303 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001304 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001305 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001306 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1307 }
1308
1309 if (defaultVertexExecutable)
1310 {
1311 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1312 }
1313
1314 if (defaultPixelExecutable)
1315 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001316 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001317 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001318 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1319 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001320
Jamie Madillb0a838b2016-11-13 20:02:12 -05001321 return (defaultVertexExecutable && defaultPixelExecutable &&
1322 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001323}
1324
Jamie Madill9082b982016-04-27 15:21:51 -04001325LinkResult ProgramD3D::link(const gl::ContextState &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001326{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001327 reset();
1328
Jamie Madill48ef11b2016-04-27 15:21:52 -04001329 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1330 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001331
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001332 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1333 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1334
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001335 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1336 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001337
Arun Patole44efa0b2015-03-04 17:11:05 +05301338 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001339 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1340
Jamie Madill53ea9cc2016-05-17 10:12:52 -04001341 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
Austin Kinross02df7962015-07-01 10:03:42 -07001342 {
1343 if (fragmentShaderD3D->usesFrontFacing())
1344 {
1345 infoLog << "The current renderer doesn't support gl_FrontFacing";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001346 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001347 }
1348 }
1349
Jamie Madillca03b352015-09-02 12:38:13 -04001350 std::vector<PackedVarying> packedVaryings =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001351 MergeVaryings(*vertexShader, *fragmentShader, mState.getTransformFeedbackVaryingNames());
Jamie Madillca03b352015-09-02 12:38:13 -04001352
Brandon Jones22502d52014-08-29 16:58:36 -07001353 // Map the varyings to the register file
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001354 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors);
Jamie Madill9fc36822015-11-18 13:08:07 -05001355 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001356 mState.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001357 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001358 return false;
Brandon Jones22502d52014-08-29 16:58:36 -07001359 }
1360
Jamie Madillc9bde922016-07-24 17:58:50 -04001361 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madille39a3f02015-11-17 20:42:15 -05001362
Jamie Madill9fc36822015-11-18 13:08:07 -05001363 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1364 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1365
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001366 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.getCaps().maxVaryingVectors)
Jamie Madill9fc36822015-11-18 13:08:07 -05001367 {
1368 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001369 return false;
Jamie Madill9fc36822015-11-18 13:08:07 -05001370 }
1371
1372 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1373 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1374 // intelligently, but D3D9 assumes one semantic per register.
1375 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001376 varyingPacking.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
Jamie Madill9fc36822015-11-18 13:08:07 -05001377 {
1378 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001379 return false;
Jamie Madill9fc36822015-11-18 13:08:07 -05001380 }
1381
Jamie Madill48ef11b2016-04-27 15:21:52 -04001382 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, varyingPacking, &mPixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -05001383 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001384 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001385 return false;
Brandon Jones22502d52014-08-29 16:58:36 -07001386 }
1387
Brandon Jones44151a92014-09-10 11:32:25 -07001388 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001389 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1390 mUsesFragDepth = metadata.usesFragDepth();
Brandon Jones44151a92014-09-10 11:32:25 -07001391
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001392 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001393 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001394 for (const auto &varying : packedVaryings)
1395 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001396 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001397 {
1398 mUsesFlatInterpolation = true;
1399 break;
1400 }
1401 }
1402
Jamie Madill4e31ad52015-10-29 10:32:57 -04001403 if (mRenderer->getMajorShaderModel() >= 4)
1404 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001405 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1406 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001407 }
1408
Jamie Madill8047c0d2016-03-07 13:02:12 -05001409 initAttribLocationsToD3DSemantic();
Jamie Madill437d2662014-12-05 14:23:35 -05001410
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001411 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001412
Jamie Madill9fc36822015-11-18 13:08:07 -05001413 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001414
Jamie Madill4e31ad52015-10-29 10:32:57 -04001415 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001416 if (result.isError())
Geoff Lang65d17e52016-09-08 09:52:58 -04001417 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001418 infoLog << result.getError().getMessage();
Geoff Lang65d17e52016-09-08 09:52:58 -04001419 return result;
1420 }
Jamie Madillb0a838b2016-11-13 20:02:12 -05001421 else if (!result.getResult())
Jamie Madill31c8c562015-08-19 14:08:03 -04001422 {
1423 infoLog << "Failed to create D3D shaders.";
1424 return result;
1425 }
1426
Jamie Madill4a3c2342015-10-08 12:58:45 -04001427 initUniformBlockInfo();
1428
Jamie Madillb0a838b2016-11-13 20:02:12 -05001429 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001430}
1431
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001432GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001433{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001434 // TODO(jmadill): Do something useful here?
1435 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001436}
1437
Jamie Madill4a3c2342015-10-08 12:58:45 -04001438void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001439{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001440 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001441
Jamie Madill62d31cb2015-09-11 13:25:51 -04001442 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1443 {
1444 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1445 continue;
1446
Jamie Madill4a3c2342015-10-08 12:58:45 -04001447 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001448 continue;
1449
Jamie Madill4a3c2342015-10-08 12:58:45 -04001450 size_t dataSize = getUniformBlockInfo(vertexBlock);
1451 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001452 }
1453
Jamie Madill48ef11b2016-04-27 15:21:52 -04001454 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001455
1456 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1457 {
1458 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1459 continue;
1460
Jamie Madill4a3c2342015-10-08 12:58:45 -04001461 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001462 continue;
1463
Jamie Madill4a3c2342015-10-08 12:58:45 -04001464 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1465 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001466 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001467}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001468
Jamie Madill4a3c2342015-10-08 12:58:45 -04001469void ProgramD3D::assignUniformBlockRegisters()
1470{
1471 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001472
1473 // Assign registers and update sizes.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001474 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1475 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001476
Jamie Madill48ef11b2016-04-27 15:21:52 -04001477 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001478 {
1479 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1480
Jamie Madill4a3c2342015-10-08 12:58:45 -04001481 D3DUniformBlock d3dUniformBlock;
1482
Jamie Madill62d31cb2015-09-11 13:25:51 -04001483 if (uniformBlock.vertexStaticUse)
1484 {
1485 unsigned int baseRegister =
1486 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001487 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001488 }
1489
1490 if (uniformBlock.fragmentStaticUse)
1491 {
1492 unsigned int baseRegister =
1493 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001494 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001495 }
1496
Jamie Madill4a3c2342015-10-08 12:58:45 -04001497 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001498 }
1499}
1500
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001501void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001502{
1503 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001504 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001505 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001506 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001507 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001508 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001509 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001510 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001511 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001512 vertexRegisters = std::max(vertexRegisters,
1513 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001514 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001515 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001516 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001517 fragmentRegisters = std::max(
1518 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001519 }
1520 }
1521 }
1522
Jamie Madill334d6152015-10-22 14:00:28 -04001523 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001524 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1525}
1526
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001527gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001528{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001529 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001530
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001531 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001532 if (error.isError())
1533 {
1534 return error;
1535 }
1536
Jamie Madill62d31cb2015-09-11 13:25:51 -04001537 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001538 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001539 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001540 }
1541
1542 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001543}
1544
Jamie Madill9082b982016-04-27 15:21:51 -04001545gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001546{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001547 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001548 {
1549 return gl::Error(GL_NO_ERROR);
1550 }
1551
1552 // Lazy init.
1553 if (mD3DUniformBlocks.empty())
1554 {
1555 assignUniformBlockRegisters();
1556 }
1557
Jamie Madill03260fa2015-06-22 13:57:22 -04001558 mVertexUBOCache.clear();
1559 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001560
1561 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1562 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1563
Jamie Madill4a3c2342015-10-08 12:58:45 -04001564 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001565 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001566 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001567 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001568 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001569
Brandon Jones18bd4102014-09-22 14:21:44 -07001570 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001571 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001572 {
1573 continue;
1574 }
1575
Jamie Madill4a3c2342015-10-08 12:58:45 -04001576 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001577 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001578 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001579 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001580
Jamie Madill969194d2015-07-20 14:36:56 -04001581 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001582 {
1583 mVertexUBOCache.resize(registerIndex + 1, -1);
1584 }
1585
1586 ASSERT(mVertexUBOCache[registerIndex] == -1);
1587 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001588 }
1589
Jamie Madill4a3c2342015-10-08 12:58:45 -04001590 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001591 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001592 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001593 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001594
1595 if (mFragmentUBOCache.size() <= registerIndex)
1596 {
1597 mFragmentUBOCache.resize(registerIndex + 1, -1);
1598 }
1599
1600 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1601 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001602 }
1603 }
1604
Jamie Madill03260fa2015-06-22 13:57:22 -04001605 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001606}
1607
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001608void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001609{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001610 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001611 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001612 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001613 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001614}
1615
Jamie Madill334d6152015-10-22 14:00:28 -04001616void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001617{
1618 setUniform(location, count, v, GL_FLOAT);
1619}
1620
1621void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1622{
1623 setUniform(location, count, v, GL_FLOAT_VEC2);
1624}
1625
1626void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1627{
1628 setUniform(location, count, v, GL_FLOAT_VEC3);
1629}
1630
1631void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1632{
1633 setUniform(location, count, v, GL_FLOAT_VEC4);
1634}
1635
Jamie Madill334d6152015-10-22 14:00:28 -04001636void ProgramD3D::setUniformMatrix2fv(GLint location,
1637 GLsizei count,
1638 GLboolean transpose,
1639 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001640{
1641 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1642}
1643
Jamie Madill334d6152015-10-22 14:00:28 -04001644void ProgramD3D::setUniformMatrix3fv(GLint location,
1645 GLsizei count,
1646 GLboolean transpose,
1647 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001648{
1649 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1650}
1651
Jamie Madill334d6152015-10-22 14:00:28 -04001652void ProgramD3D::setUniformMatrix4fv(GLint location,
1653 GLsizei count,
1654 GLboolean transpose,
1655 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001656{
1657 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1658}
1659
Jamie Madill334d6152015-10-22 14:00:28 -04001660void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1661 GLsizei count,
1662 GLboolean transpose,
1663 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001664{
1665 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1666}
1667
Jamie Madill334d6152015-10-22 14:00:28 -04001668void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1669 GLsizei count,
1670 GLboolean transpose,
1671 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001672{
1673 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1674}
1675
Jamie Madill334d6152015-10-22 14:00:28 -04001676void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1677 GLsizei count,
1678 GLboolean transpose,
1679 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001680{
1681 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1682}
1683
Jamie Madill334d6152015-10-22 14:00:28 -04001684void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1685 GLsizei count,
1686 GLboolean transpose,
1687 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001688{
1689 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1690}
1691
Jamie Madill334d6152015-10-22 14:00:28 -04001692void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1693 GLsizei count,
1694 GLboolean transpose,
1695 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001696{
1697 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1698}
1699
Jamie Madill334d6152015-10-22 14:00:28 -04001700void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1701 GLsizei count,
1702 GLboolean transpose,
1703 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001704{
1705 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1706}
1707
1708void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1709{
1710 setUniform(location, count, v, GL_INT);
1711}
1712
1713void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1714{
1715 setUniform(location, count, v, GL_INT_VEC2);
1716}
1717
1718void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1719{
1720 setUniform(location, count, v, GL_INT_VEC3);
1721}
1722
1723void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1724{
1725 setUniform(location, count, v, GL_INT_VEC4);
1726}
1727
1728void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1729{
1730 setUniform(location, count, v, GL_UNSIGNED_INT);
1731}
1732
1733void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1734{
1735 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1736}
1737
1738void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1739{
1740 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1741}
1742
1743void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1744{
1745 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1746}
1747
Jamie Madill4a3c2342015-10-08 12:58:45 -04001748void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1749 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001750{
1751}
1752
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001753void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001754{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001755 D3DUniformMap uniformMap;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001756 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001757 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001758
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001759 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001760 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001761 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001762 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001763 }
1764 }
1765
Jamie Madill48ef11b2016-04-27 15:21:52 -04001766 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001767 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001768 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001769 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001770 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001771 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001772 }
1773 }
1774
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001775 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001776 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001777 {
1778 if (!glUniform.isInDefaultBlock())
1779 continue;
1780
1781 auto mapEntry = uniformMap.find(glUniform.name);
1782 ASSERT(mapEntry != uniformMap.end());
1783 mD3DUniforms.push_back(mapEntry->second);
1784 }
1785
Jamie Madill62d31cb2015-09-11 13:25:51 -04001786 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001787 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001788}
1789
Jamie Madill91445bc2015-09-23 16:47:53 -04001790void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001791 const sh::Uniform &uniform,
1792 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001793{
Olli Etuaho96963162016-03-21 11:54:33 +02001794 // Samplers get their registers assigned in assignAllSamplerRegisters.
1795 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001796 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001797 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001798 return;
1799 }
1800
Jamie Madill91445bc2015-09-23 16:47:53 -04001801 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1802
1803 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1804 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001805 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001806 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001807
Jamie Madill91445bc2015-09-23 16:47:53 -04001808 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001809}
1810
Jamie Madill62d31cb2015-09-11 13:25:51 -04001811D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1812{
1813 for (D3DUniform *d3dUniform : mD3DUniforms)
1814 {
1815 if (d3dUniform->name == name)
1816 {
1817 return d3dUniform;
1818 }
1819 }
1820
1821 return nullptr;
1822}
1823
Jamie Madill91445bc2015-09-23 16:47:53 -04001824void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001825 const sh::ShaderVariable &uniform,
1826 const std::string &fullName,
1827 sh::HLSLBlockEncoder *encoder,
1828 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001829{
1830 if (uniform.isStruct())
1831 {
1832 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1833 {
1834 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1835
Jamie Madill55def582015-05-04 11:24:57 -04001836 if (encoder)
1837 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001838
1839 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1840 {
Jamie Madill334d6152015-10-22 14:00:28 -04001841 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001842 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1843
Olli Etuaho96963162016-03-21 11:54:33 +02001844 // Samplers get their registers assigned in assignAllSamplerRegisters.
1845 // Also they couldn't use the same encoder as the rest of the struct, since they are
1846 // extracted out of the struct by the shader translator.
1847 if (gl::IsSamplerType(field.type))
1848 {
1849 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
1850 }
1851 else
1852 {
1853 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
1854 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001855 }
1856
Jamie Madill55def582015-05-04 11:24:57 -04001857 if (encoder)
1858 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001859 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001860 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001861 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001862
1863 // Not a struct. Arrays are treated as aggregate types.
1864 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001865 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001866 encoder->enterAggregateType();
1867 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001868
Jamie Madill62d31cb2015-09-11 13:25:51 -04001869 // Advance the uniform offset, to track registers allocation for structs
1870 sh::BlockMemberInfo blockInfo =
1871 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1872 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001873
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001874 auto uniformMapEntry = uniformMap->find(fullName);
1875 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001876
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001877 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001878 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001879 d3dUniform = uniformMapEntry->second;
1880 }
1881 else
1882 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001883 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001884 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001885 }
1886
1887 if (encoder)
1888 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001889 d3dUniform->registerElement =
1890 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001891 unsigned int reg =
1892 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001893 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001894 {
1895 d3dUniform->psRegisterIndex = reg;
1896 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001897 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001898 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001899 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001900 d3dUniform->vsRegisterIndex = reg;
1901 }
Jamie Madillfb536032015-09-11 13:19:49 -04001902
1903 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001904 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001905 {
1906 encoder->exitAggregateType();
1907 }
1908 }
1909}
1910
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001911template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001912void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001913{
Jamie Madill334d6152015-10-22 14:00:28 -04001914 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001915 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1916
Jamie Madill62d31cb2015-09-11 13:25:51 -04001917 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001918
Jamie Madill62d31cb2015-09-11 13:25:51 -04001919 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001920 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001921 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001922
1923 if (targetUniform->type == targetUniformType)
1924 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001925 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001926
Jamie Madill62d31cb2015-09-11 13:25:51 -04001927 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001928 {
Jamie Madill334d6152015-10-22 14:00:28 -04001929 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001930 const T *source = v + (i * components);
1931
1932 for (int c = 0; c < components; c++)
1933 {
1934 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1935 }
1936 for (int c = components; c < 4; c++)
1937 {
1938 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1939 }
1940 }
1941 }
1942 else if (targetUniform->type == targetBoolType)
1943 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001944 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001945
Jamie Madill62d31cb2015-09-11 13:25:51 -04001946 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001947 {
Jamie Madill334d6152015-10-22 14:00:28 -04001948 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001949 const T *source = v + (i * components);
1950
1951 for (int c = 0; c < components; c++)
1952 {
Jamie Madill334d6152015-10-22 14:00:28 -04001953 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1954 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001955 }
1956 for (int c = components; c < 4; c++)
1957 {
1958 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1959 }
1960 }
1961 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001962 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001963 {
1964 ASSERT(targetUniformType == GL_INT);
1965
Jamie Madill62d31cb2015-09-11 13:25:51 -04001966 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001967
1968 bool wasDirty = targetUniform->dirty;
1969
Jamie Madill62d31cb2015-09-11 13:25:51 -04001970 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001971 {
Jamie Madill334d6152015-10-22 14:00:28 -04001972 GLint *dest = target + (i * 4);
1973 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001974
1975 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1976 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1977 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1978 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1979 }
1980
1981 if (!wasDirty && targetUniform->dirty)
1982 {
1983 mDirtySamplerMapping = true;
1984 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001985 }
Jamie Madill334d6152015-10-22 14:00:28 -04001986 else
1987 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001988}
1989
1990template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001991void ProgramD3D::setUniformMatrixfv(GLint location,
1992 GLsizei countIn,
1993 GLboolean transpose,
1994 const GLfloat *value,
1995 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001996{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001997 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001998
Jamie Madill62d31cb2015-09-11 13:25:51 -04001999 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002000 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002001 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002002
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002003 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002004 GLfloat *target =
2005 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002006
Jamie Madill62d31cb2015-09-11 13:25:51 -04002007 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002008 {
2009 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2010 if (transpose == GL_FALSE)
2011 {
Jamie Madill334d6152015-10-22 14:00:28 -04002012 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2013 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002014 }
2015 else
2016 {
Jamie Madill334d6152015-10-22 14:00:28 -04002017 targetUniform->dirty =
2018 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002019 }
2020 target += targetMatrixStride;
2021 value += cols * rows;
2022 }
2023}
2024
Jamie Madill4a3c2342015-10-08 12:58:45 -04002025size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002026{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002027 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002028
Jamie Madill62d31cb2015-09-11 13:25:51 -04002029 // define member uniforms
2030 sh::Std140BlockEncoder std140Encoder;
2031 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2032 sh::BlockLayoutEncoder *encoder = nullptr;
2033
2034 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002035 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002036 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002037 }
2038 else
2039 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002040 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002041 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002042
Jamie Madill39046162016-02-08 15:05:17 -05002043 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2044 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002045
2046 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002047}
2048
Jamie Madill62d31cb2015-09-11 13:25:51 -04002049void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002050{
Olli Etuaho96963162016-03-21 11:54:33 +02002051 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002052 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002053 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002054 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002055 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002056 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002057 }
2058}
2059
Olli Etuaho96963162016-03-21 11:54:33 +02002060void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002061{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002062 ASSERT(d3dUniform->isSampler());
Jamie Madill48ef11b2016-04-27 15:21:52 -04002063 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2064 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Olli Etuaho96963162016-03-21 11:54:33 +02002065 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) || fragmentShaderD3D->hasUniform(d3dUniform));
2066 if (vertexShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002067 {
Olli Etuaho96963162016-03-21 11:54:33 +02002068 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2069 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002070 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2071 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002072 }
Olli Etuaho96963162016-03-21 11:54:33 +02002073 if (fragmentShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002074 {
Olli Etuaho96963162016-03-21 11:54:33 +02002075 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2076 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002077 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2078 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002079 }
2080}
2081
Jamie Madill62d31cb2015-09-11 13:25:51 -04002082// static
2083void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002084 GLenum samplerType,
2085 unsigned int samplerCount,
2086 std::vector<Sampler> &outSamplers,
2087 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002088{
2089 unsigned int samplerIndex = startSamplerIndex;
2090
2091 do
2092 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002093 ASSERT(samplerIndex < outSamplers.size());
2094 Sampler *sampler = &outSamplers[samplerIndex];
2095 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002096 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002097 sampler->logicalTextureUnit = 0;
2098 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002099 samplerIndex++;
2100 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002101}
2102
Brandon Jonesc9610c52014-08-25 17:02:59 -07002103void ProgramD3D::reset()
2104{
Brandon Joneseb994362014-09-24 10:27:28 -07002105 SafeDeleteContainer(mVertexExecutables);
2106 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002107
2108 for (auto &element : mGeometryExecutables)
2109 {
2110 SafeDelete(element);
2111 }
Brandon Joneseb994362014-09-24 10:27:28 -07002112
Brandon Jones22502d52014-08-29 16:58:36 -07002113 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002114 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002115
2116 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002117 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002118 mUsesFragDepth = false;
2119 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002120 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002121 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002122
Jamie Madill62d31cb2015-09-11 13:25:51 -04002123 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002124 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002125
Brandon Jonesc9610c52014-08-25 17:02:59 -07002126 SafeDelete(mVertexUniformStorage);
2127 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002128
2129 mSamplersPS.clear();
2130 mSamplersVS.clear();
2131
2132 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002133 mUsedPixelSamplerRange = 0;
2134 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002135
Jamie Madill8047c0d2016-03-07 13:02:12 -05002136 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002137
Jamie Madill9fc36822015-11-18 13:08:07 -05002138 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002139
2140 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002141}
2142
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143unsigned int ProgramD3D::getSerial() const
2144{
2145 return mSerial;
2146}
2147
2148unsigned int ProgramD3D::issueSerial()
2149{
2150 return mCurrentSerial++;
2151}
2152
Jamie Madill8047c0d2016-03-07 13:02:12 -05002153void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002154{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002155 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002156 ASSERT(vertexShader != nullptr);
2157
2158 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002159 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002160 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002161 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2162 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002163
Jamie Madill8047c0d2016-03-07 13:02:12 -05002164 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002165 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002166 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002167 }
2168 }
Jamie Madill437d2662014-12-05 14:23:35 -05002169}
2170
Jamie Madill63805b42015-08-25 13:17:39 -04002171void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002172{
Jamie Madillbd136f92015-08-10 14:51:37 -04002173 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002174 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002175
Jamie Madill48ef11b2016-04-27 15:21:52 -04002176 for (unsigned int locationIndex : angle::IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002177 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002178 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002179
Jamie Madill8047c0d2016-03-07 13:02:12 -05002180 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002181 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002182 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002183 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002184 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002185 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002186 mCachedInputLayout[d3dSemantic] =
2187 GetVertexFormatType(vertexAttributes[locationIndex],
2188 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002189 }
2190 }
2191}
2192
Jamie Madill9fc36822015-11-18 13:08:07 -05002193void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002194{
Jamie Madill9fc36822015-11-18 13:08:07 -05002195 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2196
2197 const std::string &varyingSemantic =
2198 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2199
Jamie Madillccdf74b2015-08-18 10:46:12 -04002200 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002201 mStreamOutVaryings.clear();
2202
Jamie Madill48ef11b2016-04-27 15:21:52 -04002203 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002204 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2205 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002206 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002207 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2208 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002209 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002210 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002211 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002212 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2213 builtins.glPosition.index, 4, outputSlot));
2214 }
2215 }
2216 else if (tfVaryingName == "gl_FragCoord")
2217 {
2218 if (builtins.glFragCoord.enabled)
2219 {
2220 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2221 builtins.glFragCoord.index, 4, outputSlot));
2222 }
2223 }
2224 else if (tfVaryingName == "gl_PointSize")
2225 {
2226 if (builtins.glPointSize.enabled)
2227 {
2228 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2229 }
2230 }
2231 else
2232 {
2233 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2234 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002235 const auto &varying = *registerInfo.packedVarying->varying;
2236 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002237 int componentCount = gl::VariableColumnCount(transposedType);
2238 ASSERT(!varying.isBuiltIn());
2239
Jamie Madill55c25d02015-11-18 13:08:08 -05002240 // Transform feedback for varying structs is underspecified.
2241 // See Khronos bug 9856.
2242 // TODO(jmadill): Figure out how to be spec-compliant here.
2243 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2244 continue;
2245
Jamie Madill9fc36822015-11-18 13:08:07 -05002246 // There can be more than one register assigned to a particular varying, and each
2247 // register needs its own stream out entry.
2248 if (tfVaryingName == varying.name)
2249 {
2250 mStreamOutVaryings.push_back(D3DVarying(
2251 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2252 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002253 }
2254 }
2255 }
2256}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002257
2258D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2259{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002260 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002261}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002262
2263bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2264{
2265 std::string baseName = blockName;
2266 gl::ParseAndStripArrayIndex(&baseName);
2267
2268 auto sizeIter = mBlockDataSizes.find(baseName);
2269 if (sizeIter == mBlockDataSizes.end())
2270 {
2271 *sizeOut = 0;
2272 return false;
2273 }
2274
2275 *sizeOut = sizeIter->second;
2276 return true;
2277}
2278
2279bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2280 sh::BlockMemberInfo *memberInfoOut) const
2281{
2282 auto infoIter = mBlockInfo.find(memberUniformName);
2283 if (infoIter == mBlockInfo.end())
2284 {
2285 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2286 return false;
2287 }
2288
2289 *memberInfoOut = infoIter->second;
2290 return true;
2291}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002292
2293void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2294 GLenum genMode,
2295 GLint components,
2296 const GLfloat *coeffs)
2297{
2298 UNREACHABLE();
2299}
2300
Jamie Madill8047c0d2016-03-07 13:02:12 -05002301} // namespace rx