blob: 3ac82d94198286574e38012f27d046a120f43bc9 [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.";
742 return LinkResult(false, gl::Error(GL_NO_ERROR));
743 }
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 Madill2db1fbb2014-12-03 10:58:55 -0500749 return LinkResult(false, gl::Error(GL_NO_ERROR));
750 }
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.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500783 return LinkResult(false, gl::Error(GL_NO_ERROR));
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.";
807 return LinkResult(false, gl::Error(GL_NO_ERROR));
808 }
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
856 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400857 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
858 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700859 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400860 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400861 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700862
Jamie Madilld3dfda22015-07-06 08:28:49 -0400863 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700864 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400865 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700866 }
867
Jamie Madill334d6152015-10-22 14:00:28 -0400868 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700869 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400870
Jamie Madillada9ecc2015-08-17 12:53:37 -0400871 ShaderExecutableD3D *shaderExecutable = nullptr;
872
873 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500874 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400875 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400876 if (error.isError())
877 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500878 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400879 }
880
Brandon Joneseb994362014-09-24 10:27:28 -0700881 if (!shaderExecutable)
882 {
Jamie Madillf6113162015-05-07 11:49:21 -0400883 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500884 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700885 }
886
887 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400888 VertexExecutable::Signature signature;
889 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700890
891 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400892 mVertexExecutables.push_back(
893 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700894
895 stream->skip(vertexShaderSize);
896 }
897
898 const size_t pixelShaderCount = stream->readInt<unsigned int>();
899 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
900 {
901 const size_t outputCount = stream->readInt<unsigned int>();
902 std::vector<GLenum> outputs(outputCount);
903 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
904 {
905 stream->readInt(&outputs[outputIndex]);
906 }
907
Jamie Madill334d6152015-10-22 14:00:28 -0400908 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700909 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400910 ShaderExecutableD3D *shaderExecutable = nullptr;
911
912 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500913 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400914 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400915 if (error.isError())
916 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500917 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400918 }
Brandon Joneseb994362014-09-24 10:27:28 -0700919
920 if (!shaderExecutable)
921 {
Jamie Madillf6113162015-05-07 11:49:21 -0400922 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500923 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700924 }
925
926 // add new binary
927 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
928
929 stream->skip(pixelShaderSize);
930 }
931
Jamie Madill4e31ad52015-10-29 10:32:57 -0400932 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
933 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700934 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400935 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
936 if (geometryShaderSize == 0)
937 {
938 mGeometryExecutables[geometryExeIndex] = nullptr;
939 continue;
940 }
941
Brandon Joneseb994362014-09-24 10:27:28 -0700942 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400943 bool splitAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400944
Jamie Madill28afae52015-11-09 15:07:57 -0500945 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500946 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, mStreamOutVaryings,
947 splitAttribs, &mGeometryExecutables[geometryExeIndex]);
Geoff Langb543aff2014-09-30 14:52:54 -0400948 if (error.isError())
949 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500950 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400951 }
Brandon Joneseb994362014-09-24 10:27:28 -0700952
Jamie Madill4e31ad52015-10-29 10:32:57 -0400953 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700954 {
Jamie Madillf6113162015-05-07 11:49:21 -0400955 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500956 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700957 }
958 stream->skip(geometryShaderSize);
959 }
960
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700961 initializeUniformStorage();
962
Geoff Lang7dd2e102014-11-10 15:19:26 -0500963 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700964}
965
Geoff Langb543aff2014-09-30 14:52:54 -0400966gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700967{
Austin Kinross137b1512015-06-17 16:14:53 -0700968 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400969 // When we load the binary again later, we can validate the device identifier before trying to
970 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700971 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400972 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
973 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700974
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500975 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
976
Jamie Madill8047c0d2016-03-07 13:02:12 -0500977 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400978 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500979 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -0400980 }
981
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700982 stream->writeInt(mSamplersPS.size());
983 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
984 {
985 stream->writeInt(mSamplersPS[i].active);
986 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
987 stream->writeInt(mSamplersPS[i].textureType);
988 }
989
990 stream->writeInt(mSamplersVS.size());
991 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
992 {
993 stream->writeInt(mSamplersVS[i].active);
994 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
995 stream->writeInt(mSamplersVS[i].textureType);
996 }
997
998 stream->writeInt(mUsedVertexSamplerRange);
999 stream->writeInt(mUsedPixelSamplerRange);
1000
Jamie Madill62d31cb2015-09-11 13:25:51 -04001001 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001002 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001003 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001004 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001005 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1006 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001007 stream->writeInt(uniform->registerCount);
1008 stream->writeInt(uniform->registerElement);
1009 }
1010
1011 stream->writeInt(mD3DUniformBlocks.size());
1012 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1013 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001014 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1015 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001016 }
1017
Jamie Madill9fc36822015-11-18 13:08:07 -05001018 stream->writeInt(mStreamOutVaryings.size());
1019 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001020 {
Brandon Joneseb994362014-09-24 10:27:28 -07001021 stream->writeString(varying.semanticName);
1022 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001023 stream->writeInt(varying.componentCount);
1024 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001025 }
1026
Brandon Jones22502d52014-08-29 16:58:36 -07001027 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001028 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1029 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001030 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001031 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1032 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001033 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001034 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001035 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001036
Brandon Joneseb994362014-09-24 10:27:28 -07001037 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001038 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001039 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1040 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001041 {
Brandon Joneseb994362014-09-24 10:27:28 -07001042 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001043 stream->writeInt(variable.type);
1044 stream->writeString(variable.name);
1045 stream->writeString(variable.source);
1046 stream->writeInt(variable.outputIndex);
1047 }
1048
Jamie Madill4e31ad52015-10-29 10:32:57 -04001049 stream->writeString(mGeometryShaderPreamble);
1050
Brandon Joneseb994362014-09-24 10:27:28 -07001051 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001052 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1053 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001054 {
1055 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1056
Jamie Madilld3dfda22015-07-06 08:28:49 -04001057 const auto &inputLayout = vertexExecutable->inputs();
1058 stream->writeInt(inputLayout.size());
1059
1060 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001061 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001062 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001063 }
1064
1065 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1066 stream->writeInt(vertexShaderSize);
1067
1068 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1069 stream->writeBytes(vertexBlob, vertexShaderSize);
1070 }
1071
1072 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001073 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1074 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001075 {
1076 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1077
1078 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1079 stream->writeInt(outputs.size());
1080 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1081 {
1082 stream->writeInt(outputs[outputIndex]);
1083 }
1084
1085 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1086 stream->writeInt(pixelShaderSize);
1087
1088 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1089 stream->writeBytes(pixelBlob, pixelShaderSize);
1090 }
1091
Jamie Madill4e31ad52015-10-29 10:32:57 -04001092 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001093 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001094 if (geometryExe == nullptr)
1095 {
1096 stream->writeInt(0);
1097 continue;
1098 }
1099
1100 size_t geometryShaderSize = geometryExe->getLength();
1101 stream->writeInt(geometryShaderSize);
1102 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001103 }
1104
Geoff Langb543aff2014-09-30 14:52:54 -04001105 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001106}
1107
Geoff Langc5629752015-12-07 16:29:04 -05001108void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1109{
1110}
1111
Jamie Madill334d6152015-10-22 14:00:28 -04001112gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1113 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001114{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001115 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001116
Jamie Madill85a18042015-03-05 15:41:41 -05001117 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001118 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001119
1120 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1121 {
1122 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1123
1124 if (colorbuffer)
1125 {
Jamie Madill334d6152015-10-22 14:00:28 -04001126 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1127 ? GL_COLOR_ATTACHMENT0
1128 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001129 }
1130 else
1131 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001132 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001133 }
1134 }
1135
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001136 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001137}
1138
Jamie Madill97399232014-12-23 12:31:15 -05001139gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001140 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001141 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001142{
1143 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1144 {
1145 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1146 {
Geoff Langb543aff2014-09-30 14:52:54 -04001147 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1148 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001149 }
1150 }
1151
Jamie Madill334d6152015-10-22 14:00:28 -04001152 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1153 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001154
1155 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001156 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001157
1158 gl::InfoLog tempInfoLog;
1159 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1160
Jamie Madillada9ecc2015-08-17 12:53:37 -04001161 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001162 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001163 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001164 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001165 if (error.isError())
1166 {
1167 return error;
1168 }
Brandon Joneseb994362014-09-24 10:27:28 -07001169
Jamie Madill97399232014-12-23 12:31:15 -05001170 if (pixelExecutable)
1171 {
1172 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1173 }
1174 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001175 {
1176 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001177 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001178 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1179 }
Brandon Jones22502d52014-08-29 16:58:36 -07001180
Geoff Langb543aff2014-09-30 14:52:54 -04001181 *outExectuable = pixelExecutable;
1182 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001183}
1184
Jamie Madilld3dfda22015-07-06 08:28:49 -04001185gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001186 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001187 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001188{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001189 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001190
1191 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1192 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001193 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001194 {
Geoff Langb543aff2014-09-30 14:52:54 -04001195 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1196 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001197 }
1198 }
1199
Brandon Jones22502d52014-08-29 16:58:36 -07001200 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001201 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001202 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001203
1204 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001205 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001206
1207 gl::InfoLog tempInfoLog;
1208 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1209
Jamie Madillada9ecc2015-08-17 12:53:37 -04001210 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001211 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001212 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001213 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001214 if (error.isError())
1215 {
1216 return error;
1217 }
1218
Jamie Madill97399232014-12-23 12:31:15 -05001219 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001220 {
Jamie Madill334d6152015-10-22 14:00:28 -04001221 mVertexExecutables.push_back(
1222 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001223 }
Jamie Madill97399232014-12-23 12:31:15 -05001224 else if (!infoLog)
1225 {
1226 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001227 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001228 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1229 }
Brandon Jones22502d52014-08-29 16:58:36 -07001230
Geoff Langb543aff2014-09-30 14:52:54 -04001231 *outExectuable = vertexExecutable;
1232 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001233}
1234
Jamie Madill9082b982016-04-27 15:21:51 -04001235gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001236 GLenum drawMode,
1237 ShaderExecutableD3D **outExecutable,
1238 gl::InfoLog *infoLog)
1239{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001240 if (outExecutable)
1241 {
1242 *outExecutable = nullptr;
1243 }
1244
Austin Kinross88829e82016-01-12 13:04:41 -08001245 // Return a null shader if the current rendering doesn't use a geometry shader
1246 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001247 {
1248 return gl::Error(GL_NO_ERROR);
1249 }
1250
Jamie Madill4e31ad52015-10-29 10:32:57 -04001251 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1252
1253 if (mGeometryExecutables[geometryShaderType] != nullptr)
1254 {
1255 if (outExecutable)
1256 {
1257 *outExecutable = mGeometryExecutables[geometryShaderType];
1258 }
1259 return gl::Error(GL_NO_ERROR);
1260 }
1261
1262 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001263 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001264 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001265
1266 gl::InfoLog tempInfoLog;
1267 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1268
1269 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001270 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001271 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
Jamie Madill4e31ad52015-10-29 10:32:57 -04001272 &mGeometryExecutables[geometryShaderType]);
1273
1274 if (!infoLog && error.isError())
1275 {
1276 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1277 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1278 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1279 }
1280
1281 if (outExecutable)
1282 {
1283 *outExecutable = mGeometryExecutables[geometryShaderType];
1284 }
1285 return error;
1286}
1287
Jamie Madill9082b982016-04-27 15:21:51 -04001288LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001289{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001290 const gl::InputLayout &defaultInputLayout =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001291 GetDefaultInputLayoutFromShader(mState.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001292 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001293 gl::Error error =
1294 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001295 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001296 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001297 return LinkResult(false, error);
1298 }
Austin Kinross434953e2015-02-20 10:49:51 -08001299
Jamie Madill334d6152015-10-22 14:00:28 -04001300 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001301 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001302 error =
1303 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001304 if (error.isError())
1305 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001306 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001307 }
Brandon Jones44151a92014-09-10 11:32:25 -07001308
Jamie Madill4e31ad52015-10-29 10:32:57 -04001309 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill847638a2015-11-20 13:01:41 -05001310 ShaderExecutableD3D *pointGS = nullptr;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001311 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001312 {
Jamie Madill847638a2015-11-20 13:01:41 -05001313 getGeometryExecutableForPrimitiveType(data, GL_POINTS, &pointGS, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001314 }
1315
Jamie Madill48ef11b2016-04-27 15:21:52 -04001316 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001317
1318 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001319 {
Jamie Madill334d6152015-10-22 14:00:28 -04001320 // Geometry shaders are currently only used internally, so there is no corresponding shader
1321 // object at the interface level. For now the geometry shader debug info is prepended to
1322 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001323 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001324 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001325 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1326 }
1327
1328 if (defaultVertexExecutable)
1329 {
1330 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1331 }
1332
1333 if (defaultPixelExecutable)
1334 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001335 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001336 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001337 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1338 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001339
Jamie Madill847638a2015-11-20 13:01:41 -05001340 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable &&
1341 (!usesGeometryShader(GL_POINTS) || pointGS));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001342 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001343}
1344
Jamie Madill9082b982016-04-27 15:21:51 -04001345LinkResult ProgramD3D::link(const gl::ContextState &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001346{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001347 reset();
1348
Jamie Madill48ef11b2016-04-27 15:21:52 -04001349 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1350 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001351
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001352 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1353 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1354
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001355 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1356 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001357
Arun Patole44efa0b2015-03-04 17:11:05 +05301358 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001359 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1360
Jamie Madill53ea9cc2016-05-17 10:12:52 -04001361 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
Austin Kinross02df7962015-07-01 10:03:42 -07001362 {
1363 if (fragmentShaderD3D->usesFrontFacing())
1364 {
1365 infoLog << "The current renderer doesn't support gl_FrontFacing";
1366 return LinkResult(false, gl::Error(GL_NO_ERROR));
1367 }
1368 }
1369
Jamie Madillca03b352015-09-02 12:38:13 -04001370 std::vector<PackedVarying> packedVaryings =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001371 MergeVaryings(*vertexShader, *fragmentShader, mState.getTransformFeedbackVaryingNames());
Jamie Madillca03b352015-09-02 12:38:13 -04001372
Brandon Jones22502d52014-08-29 16:58:36 -07001373 // Map the varyings to the register file
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001374 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors);
Jamie Madill9fc36822015-11-18 13:08:07 -05001375 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001376 mState.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001377 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001378 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001379 }
1380
Jamie Madillc9bde922016-07-24 17:58:50 -04001381 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madille39a3f02015-11-17 20:42:15 -05001382
Jamie Madill9fc36822015-11-18 13:08:07 -05001383 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1384 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1385
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001386 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.getCaps().maxVaryingVectors)
Jamie Madill9fc36822015-11-18 13:08:07 -05001387 {
1388 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1389 return LinkResult(false, gl::Error(GL_NO_ERROR));
1390 }
1391
1392 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1393 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1394 // intelligently, but D3D9 assumes one semantic per register.
1395 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001396 varyingPacking.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
Jamie Madill9fc36822015-11-18 13:08:07 -05001397 {
1398 infoLog << "Cannot pack these varyings on D3D9.";
1399 return LinkResult(false, gl::Error(GL_NO_ERROR));
1400 }
1401
Jamie Madill48ef11b2016-04-27 15:21:52 -04001402 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, varyingPacking, &mPixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -05001403 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001404 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001405 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001406 }
1407
Brandon Jones44151a92014-09-10 11:32:25 -07001408 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001409 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1410 mUsesFragDepth = metadata.usesFragDepth();
Brandon Jones44151a92014-09-10 11:32:25 -07001411
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001412 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001413 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001414 for (const auto &varying : packedVaryings)
1415 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001416 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001417 {
1418 mUsesFlatInterpolation = true;
1419 break;
1420 }
1421 }
1422
Jamie Madill4e31ad52015-10-29 10:32:57 -04001423 if (mRenderer->getMajorShaderModel() >= 4)
1424 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001425 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1426 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001427 }
1428
Jamie Madill8047c0d2016-03-07 13:02:12 -05001429 initAttribLocationsToD3DSemantic();
Jamie Madill437d2662014-12-05 14:23:35 -05001430
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001431 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001432
Jamie Madill9fc36822015-11-18 13:08:07 -05001433 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001434
Jamie Madill4e31ad52015-10-29 10:32:57 -04001435 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001436 if (result.error.isError() || !result.linkSuccess)
1437 {
1438 infoLog << "Failed to create D3D shaders.";
1439 return result;
1440 }
1441
Jamie Madill4a3c2342015-10-08 12:58:45 -04001442 initUniformBlockInfo();
1443
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001445}
1446
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001447GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001448{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001449 // TODO(jmadill): Do something useful here?
1450 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001451}
1452
Jamie Madill4a3c2342015-10-08 12:58:45 -04001453void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001454{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001455 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001456
Jamie Madill62d31cb2015-09-11 13:25:51 -04001457 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1458 {
1459 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1460 continue;
1461
Jamie Madill4a3c2342015-10-08 12:58:45 -04001462 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001463 continue;
1464
Jamie Madill4a3c2342015-10-08 12:58:45 -04001465 size_t dataSize = getUniformBlockInfo(vertexBlock);
1466 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001467 }
1468
Jamie Madill48ef11b2016-04-27 15:21:52 -04001469 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001470
1471 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1472 {
1473 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1474 continue;
1475
Jamie Madill4a3c2342015-10-08 12:58:45 -04001476 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001477 continue;
1478
Jamie Madill4a3c2342015-10-08 12:58:45 -04001479 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1480 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001481 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001482}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001483
Jamie Madill4a3c2342015-10-08 12:58:45 -04001484void ProgramD3D::assignUniformBlockRegisters()
1485{
1486 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001487
1488 // Assign registers and update sizes.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001489 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1490 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001491
Jamie Madill48ef11b2016-04-27 15:21:52 -04001492 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001493 {
1494 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1495
Jamie Madill4a3c2342015-10-08 12:58:45 -04001496 D3DUniformBlock d3dUniformBlock;
1497
Jamie Madill62d31cb2015-09-11 13:25:51 -04001498 if (uniformBlock.vertexStaticUse)
1499 {
1500 unsigned int baseRegister =
1501 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001502 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001503 }
1504
1505 if (uniformBlock.fragmentStaticUse)
1506 {
1507 unsigned int baseRegister =
1508 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001509 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001510 }
1511
Jamie Madill4a3c2342015-10-08 12:58:45 -04001512 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001513 }
1514}
1515
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001516void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001517{
1518 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001519 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001520 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001521 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001522 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001523 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001524 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001525 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001526 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001527 vertexRegisters = std::max(vertexRegisters,
1528 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001529 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001530 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001531 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001532 fragmentRegisters = std::max(
1533 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001534 }
1535 }
1536 }
1537
Jamie Madill334d6152015-10-22 14:00:28 -04001538 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001539 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1540}
1541
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001542gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001543{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001544 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001545
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001546 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001547 if (error.isError())
1548 {
1549 return error;
1550 }
1551
Jamie Madill62d31cb2015-09-11 13:25:51 -04001552 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001553 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001554 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001555 }
1556
1557 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001558}
1559
Jamie Madill9082b982016-04-27 15:21:51 -04001560gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001561{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001562 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001563 {
1564 return gl::Error(GL_NO_ERROR);
1565 }
1566
1567 // Lazy init.
1568 if (mD3DUniformBlocks.empty())
1569 {
1570 assignUniformBlockRegisters();
1571 }
1572
Jamie Madill03260fa2015-06-22 13:57:22 -04001573 mVertexUBOCache.clear();
1574 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001575
1576 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1577 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1578
Jamie Madill4a3c2342015-10-08 12:58:45 -04001579 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001580 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001581 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001582 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001583 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001584
Brandon Jones18bd4102014-09-22 14:21:44 -07001585 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001586 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001587 {
1588 continue;
1589 }
1590
Jamie Madill4a3c2342015-10-08 12:58:45 -04001591 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001592 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001593 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001594 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001595
Jamie Madill969194d2015-07-20 14:36:56 -04001596 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001597 {
1598 mVertexUBOCache.resize(registerIndex + 1, -1);
1599 }
1600
1601 ASSERT(mVertexUBOCache[registerIndex] == -1);
1602 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001603 }
1604
Jamie Madill4a3c2342015-10-08 12:58:45 -04001605 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001606 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001607 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001608 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001609
1610 if (mFragmentUBOCache.size() <= registerIndex)
1611 {
1612 mFragmentUBOCache.resize(registerIndex + 1, -1);
1613 }
1614
1615 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1616 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001617 }
1618 }
1619
Jamie Madill03260fa2015-06-22 13:57:22 -04001620 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001621}
1622
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001623void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001624{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001625 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001626 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001627 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001628 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001629}
1630
Jamie Madill334d6152015-10-22 14:00:28 -04001631void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001632{
1633 setUniform(location, count, v, GL_FLOAT);
1634}
1635
1636void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1637{
1638 setUniform(location, count, v, GL_FLOAT_VEC2);
1639}
1640
1641void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1642{
1643 setUniform(location, count, v, GL_FLOAT_VEC3);
1644}
1645
1646void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1647{
1648 setUniform(location, count, v, GL_FLOAT_VEC4);
1649}
1650
Jamie Madill334d6152015-10-22 14:00:28 -04001651void ProgramD3D::setUniformMatrix2fv(GLint location,
1652 GLsizei count,
1653 GLboolean transpose,
1654 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001655{
1656 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1657}
1658
Jamie Madill334d6152015-10-22 14:00:28 -04001659void ProgramD3D::setUniformMatrix3fv(GLint location,
1660 GLsizei count,
1661 GLboolean transpose,
1662 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001663{
1664 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1665}
1666
Jamie Madill334d6152015-10-22 14:00:28 -04001667void ProgramD3D::setUniformMatrix4fv(GLint location,
1668 GLsizei count,
1669 GLboolean transpose,
1670 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001671{
1672 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1673}
1674
Jamie Madill334d6152015-10-22 14:00:28 -04001675void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1676 GLsizei count,
1677 GLboolean transpose,
1678 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001679{
1680 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1681}
1682
Jamie Madill334d6152015-10-22 14:00:28 -04001683void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1684 GLsizei count,
1685 GLboolean transpose,
1686 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001687{
1688 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1689}
1690
Jamie Madill334d6152015-10-22 14:00:28 -04001691void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1692 GLsizei count,
1693 GLboolean transpose,
1694 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001695{
1696 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1697}
1698
Jamie Madill334d6152015-10-22 14:00:28 -04001699void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1700 GLsizei count,
1701 GLboolean transpose,
1702 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001703{
1704 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1705}
1706
Jamie Madill334d6152015-10-22 14:00:28 -04001707void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1708 GLsizei count,
1709 GLboolean transpose,
1710 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001711{
1712 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1713}
1714
Jamie Madill334d6152015-10-22 14:00:28 -04001715void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1716 GLsizei count,
1717 GLboolean transpose,
1718 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001719{
1720 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1721}
1722
1723void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1724{
1725 setUniform(location, count, v, GL_INT);
1726}
1727
1728void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1729{
1730 setUniform(location, count, v, GL_INT_VEC2);
1731}
1732
1733void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1734{
1735 setUniform(location, count, v, GL_INT_VEC3);
1736}
1737
1738void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1739{
1740 setUniform(location, count, v, GL_INT_VEC4);
1741}
1742
1743void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1744{
1745 setUniform(location, count, v, GL_UNSIGNED_INT);
1746}
1747
1748void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1749{
1750 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1751}
1752
1753void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1754{
1755 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1756}
1757
1758void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1759{
1760 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1761}
1762
Jamie Madill4a3c2342015-10-08 12:58:45 -04001763void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1764 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001765{
1766}
1767
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001768void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001769{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001770 D3DUniformMap uniformMap;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001771 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001772 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001773
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001774 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001775 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001776 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001777 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001778 }
1779 }
1780
Jamie Madill48ef11b2016-04-27 15:21:52 -04001781 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001782 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001783 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001784 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001785 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001786 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001787 }
1788 }
1789
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001790 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001791 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001792 {
1793 if (!glUniform.isInDefaultBlock())
1794 continue;
1795
1796 auto mapEntry = uniformMap.find(glUniform.name);
1797 ASSERT(mapEntry != uniformMap.end());
1798 mD3DUniforms.push_back(mapEntry->second);
1799 }
1800
Jamie Madill62d31cb2015-09-11 13:25:51 -04001801 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001802 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001803}
1804
Jamie Madill91445bc2015-09-23 16:47:53 -04001805void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001806 const sh::Uniform &uniform,
1807 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001808{
Olli Etuaho96963162016-03-21 11:54:33 +02001809 // Samplers get their registers assigned in assignAllSamplerRegisters.
1810 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001811 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001812 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001813 return;
1814 }
1815
Jamie Madill91445bc2015-09-23 16:47:53 -04001816 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1817
1818 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1819 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001820 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001821 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001822
Jamie Madill91445bc2015-09-23 16:47:53 -04001823 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001824}
1825
Jamie Madill62d31cb2015-09-11 13:25:51 -04001826D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1827{
1828 for (D3DUniform *d3dUniform : mD3DUniforms)
1829 {
1830 if (d3dUniform->name == name)
1831 {
1832 return d3dUniform;
1833 }
1834 }
1835
1836 return nullptr;
1837}
1838
Jamie Madill91445bc2015-09-23 16:47:53 -04001839void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001840 const sh::ShaderVariable &uniform,
1841 const std::string &fullName,
1842 sh::HLSLBlockEncoder *encoder,
1843 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001844{
1845 if (uniform.isStruct())
1846 {
1847 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1848 {
1849 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1850
Jamie Madill55def582015-05-04 11:24:57 -04001851 if (encoder)
1852 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001853
1854 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1855 {
Jamie Madill334d6152015-10-22 14:00:28 -04001856 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001857 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1858
Olli Etuaho96963162016-03-21 11:54:33 +02001859 // Samplers get their registers assigned in assignAllSamplerRegisters.
1860 // Also they couldn't use the same encoder as the rest of the struct, since they are
1861 // extracted out of the struct by the shader translator.
1862 if (gl::IsSamplerType(field.type))
1863 {
1864 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
1865 }
1866 else
1867 {
1868 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
1869 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001870 }
1871
Jamie Madill55def582015-05-04 11:24:57 -04001872 if (encoder)
1873 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001874 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001875 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001876 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001877
1878 // Not a struct. Arrays are treated as aggregate types.
1879 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001880 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001881 encoder->enterAggregateType();
1882 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001883
Jamie Madill62d31cb2015-09-11 13:25:51 -04001884 // Advance the uniform offset, to track registers allocation for structs
1885 sh::BlockMemberInfo blockInfo =
1886 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1887 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001888
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001889 auto uniformMapEntry = uniformMap->find(fullName);
1890 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001891
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001892 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001893 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001894 d3dUniform = uniformMapEntry->second;
1895 }
1896 else
1897 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001898 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001899 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001900 }
1901
1902 if (encoder)
1903 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001904 d3dUniform->registerElement =
1905 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001906 unsigned int reg =
1907 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001908 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001909 {
1910 d3dUniform->psRegisterIndex = reg;
1911 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001912 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001913 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001914 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001915 d3dUniform->vsRegisterIndex = reg;
1916 }
Jamie Madillfb536032015-09-11 13:19:49 -04001917
1918 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001919 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001920 {
1921 encoder->exitAggregateType();
1922 }
1923 }
1924}
1925
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001926template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001927void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001928{
Jamie Madill334d6152015-10-22 14:00:28 -04001929 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001930 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1931
Jamie Madill62d31cb2015-09-11 13:25:51 -04001932 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001933
Jamie Madill62d31cb2015-09-11 13:25:51 -04001934 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001935 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001936 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001937
1938 if (targetUniform->type == targetUniformType)
1939 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001940 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001941
Jamie Madill62d31cb2015-09-11 13:25:51 -04001942 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001943 {
Jamie Madill334d6152015-10-22 14:00:28 -04001944 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001945 const T *source = v + (i * components);
1946
1947 for (int c = 0; c < components; c++)
1948 {
1949 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1950 }
1951 for (int c = components; c < 4; c++)
1952 {
1953 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1954 }
1955 }
1956 }
1957 else if (targetUniform->type == targetBoolType)
1958 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001959 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001960
Jamie Madill62d31cb2015-09-11 13:25:51 -04001961 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001962 {
Jamie Madill334d6152015-10-22 14:00:28 -04001963 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001964 const T *source = v + (i * components);
1965
1966 for (int c = 0; c < components; c++)
1967 {
Jamie Madill334d6152015-10-22 14:00:28 -04001968 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1969 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001970 }
1971 for (int c = components; c < 4; c++)
1972 {
1973 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1974 }
1975 }
1976 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001977 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001978 {
1979 ASSERT(targetUniformType == GL_INT);
1980
Jamie Madill62d31cb2015-09-11 13:25:51 -04001981 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001982
1983 bool wasDirty = targetUniform->dirty;
1984
Jamie Madill62d31cb2015-09-11 13:25:51 -04001985 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986 {
Jamie Madill334d6152015-10-22 14:00:28 -04001987 GLint *dest = target + (i * 4);
1988 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001989
1990 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1991 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1992 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1993 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1994 }
1995
1996 if (!wasDirty && targetUniform->dirty)
1997 {
1998 mDirtySamplerMapping = true;
1999 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002000 }
Jamie Madill334d6152015-10-22 14:00:28 -04002001 else
2002 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002003}
2004
2005template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002006void ProgramD3D::setUniformMatrixfv(GLint location,
2007 GLsizei countIn,
2008 GLboolean transpose,
2009 const GLfloat *value,
2010 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002011{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002012 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002013
Jamie Madill62d31cb2015-09-11 13:25:51 -04002014 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002015 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002016 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002019 GLfloat *target =
2020 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002021
Jamie Madill62d31cb2015-09-11 13:25:51 -04002022 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002023 {
2024 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2025 if (transpose == GL_FALSE)
2026 {
Jamie Madill334d6152015-10-22 14:00:28 -04002027 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2028 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002029 }
2030 else
2031 {
Jamie Madill334d6152015-10-22 14:00:28 -04002032 targetUniform->dirty =
2033 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002034 }
2035 target += targetMatrixStride;
2036 value += cols * rows;
2037 }
2038}
2039
Jamie Madill4a3c2342015-10-08 12:58:45 -04002040size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002041{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002042 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002043
Jamie Madill62d31cb2015-09-11 13:25:51 -04002044 // define member uniforms
2045 sh::Std140BlockEncoder std140Encoder;
2046 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2047 sh::BlockLayoutEncoder *encoder = nullptr;
2048
2049 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002050 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002051 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002052 }
2053 else
2054 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002055 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002056 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002057
Jamie Madill39046162016-02-08 15:05:17 -05002058 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2059 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002060
2061 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002062}
2063
Jamie Madill62d31cb2015-09-11 13:25:51 -04002064void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002065{
Olli Etuaho96963162016-03-21 11:54:33 +02002066 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002067 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002068 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002069 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002070 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002071 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002072 }
2073}
2074
Olli Etuaho96963162016-03-21 11:54:33 +02002075void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002076{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002077 ASSERT(d3dUniform->isSampler());
Jamie Madill48ef11b2016-04-27 15:21:52 -04002078 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2079 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Olli Etuaho96963162016-03-21 11:54:33 +02002080 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) || fragmentShaderD3D->hasUniform(d3dUniform));
2081 if (vertexShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002082 {
Olli Etuaho96963162016-03-21 11:54:33 +02002083 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2084 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002085 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2086 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002087 }
Olli Etuaho96963162016-03-21 11:54:33 +02002088 if (fragmentShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002089 {
Olli Etuaho96963162016-03-21 11:54:33 +02002090 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2091 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002092 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2093 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002094 }
2095}
2096
Jamie Madill62d31cb2015-09-11 13:25:51 -04002097// static
2098void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002099 GLenum samplerType,
2100 unsigned int samplerCount,
2101 std::vector<Sampler> &outSamplers,
2102 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002103{
2104 unsigned int samplerIndex = startSamplerIndex;
2105
2106 do
2107 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002108 ASSERT(samplerIndex < outSamplers.size());
2109 Sampler *sampler = &outSamplers[samplerIndex];
2110 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002111 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002112 sampler->logicalTextureUnit = 0;
2113 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002114 samplerIndex++;
2115 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002116}
2117
Brandon Jonesc9610c52014-08-25 17:02:59 -07002118void ProgramD3D::reset()
2119{
Brandon Joneseb994362014-09-24 10:27:28 -07002120 SafeDeleteContainer(mVertexExecutables);
2121 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002122
2123 for (auto &element : mGeometryExecutables)
2124 {
2125 SafeDelete(element);
2126 }
Brandon Joneseb994362014-09-24 10:27:28 -07002127
Brandon Jones22502d52014-08-29 16:58:36 -07002128 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002129 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002130
2131 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002132 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002133 mUsesFragDepth = false;
2134 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002135 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002136 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002137
Jamie Madill62d31cb2015-09-11 13:25:51 -04002138 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002139 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002140
Brandon Jonesc9610c52014-08-25 17:02:59 -07002141 SafeDelete(mVertexUniformStorage);
2142 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002143
2144 mSamplersPS.clear();
2145 mSamplersVS.clear();
2146
2147 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002148 mUsedPixelSamplerRange = 0;
2149 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002150
Jamie Madill8047c0d2016-03-07 13:02:12 -05002151 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002152
Jamie Madill9fc36822015-11-18 13:08:07 -05002153 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002154
2155 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002156}
2157
Geoff Lang7dd2e102014-11-10 15:19:26 -05002158unsigned int ProgramD3D::getSerial() const
2159{
2160 return mSerial;
2161}
2162
2163unsigned int ProgramD3D::issueSerial()
2164{
2165 return mCurrentSerial++;
2166}
2167
Jamie Madill8047c0d2016-03-07 13:02:12 -05002168void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002169{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002170 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002171 ASSERT(vertexShader != nullptr);
2172
2173 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002174 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002175 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002176 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2177 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002178
Jamie Madill8047c0d2016-03-07 13:02:12 -05002179 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002180 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002181 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002182 }
2183 }
Jamie Madill437d2662014-12-05 14:23:35 -05002184}
2185
Jamie Madill63805b42015-08-25 13:17:39 -04002186void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002187{
Jamie Madillbd136f92015-08-10 14:51:37 -04002188 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002189 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002190
Jamie Madill48ef11b2016-04-27 15:21:52 -04002191 for (unsigned int locationIndex : angle::IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002192 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002193 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002194
Jamie Madill8047c0d2016-03-07 13:02:12 -05002195 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002196 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002197 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002198 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002199 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002200 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002201 mCachedInputLayout[d3dSemantic] =
2202 GetVertexFormatType(vertexAttributes[locationIndex],
2203 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002204 }
2205 }
2206}
2207
Jamie Madill9fc36822015-11-18 13:08:07 -05002208void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002209{
Jamie Madill9fc36822015-11-18 13:08:07 -05002210 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2211
2212 const std::string &varyingSemantic =
2213 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2214
Jamie Madillccdf74b2015-08-18 10:46:12 -04002215 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002216 mStreamOutVaryings.clear();
2217
Jamie Madill48ef11b2016-04-27 15:21:52 -04002218 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002219 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2220 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002221 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002222 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2223 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002224 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002225 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002226 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002227 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2228 builtins.glPosition.index, 4, outputSlot));
2229 }
2230 }
2231 else if (tfVaryingName == "gl_FragCoord")
2232 {
2233 if (builtins.glFragCoord.enabled)
2234 {
2235 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2236 builtins.glFragCoord.index, 4, outputSlot));
2237 }
2238 }
2239 else if (tfVaryingName == "gl_PointSize")
2240 {
2241 if (builtins.glPointSize.enabled)
2242 {
2243 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2244 }
2245 }
2246 else
2247 {
2248 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2249 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002250 const auto &varying = *registerInfo.packedVarying->varying;
2251 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002252 int componentCount = gl::VariableColumnCount(transposedType);
2253 ASSERT(!varying.isBuiltIn());
2254
Jamie Madill55c25d02015-11-18 13:08:08 -05002255 // Transform feedback for varying structs is underspecified.
2256 // See Khronos bug 9856.
2257 // TODO(jmadill): Figure out how to be spec-compliant here.
2258 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2259 continue;
2260
Jamie Madill9fc36822015-11-18 13:08:07 -05002261 // There can be more than one register assigned to a particular varying, and each
2262 // register needs its own stream out entry.
2263 if (tfVaryingName == varying.name)
2264 {
2265 mStreamOutVaryings.push_back(D3DVarying(
2266 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2267 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002268 }
2269 }
2270 }
2271}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002272
2273D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2274{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002275 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002276}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002277
2278bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2279{
2280 std::string baseName = blockName;
2281 gl::ParseAndStripArrayIndex(&baseName);
2282
2283 auto sizeIter = mBlockDataSizes.find(baseName);
2284 if (sizeIter == mBlockDataSizes.end())
2285 {
2286 *sizeOut = 0;
2287 return false;
2288 }
2289
2290 *sizeOut = sizeIter->second;
2291 return true;
2292}
2293
2294bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2295 sh::BlockMemberInfo *memberInfoOut) const
2296{
2297 auto infoIter = mBlockInfo.find(memberUniformName);
2298 if (infoIter == mBlockInfo.end())
2299 {
2300 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2301 return false;
2302 }
2303
2304 *memberInfoOut = infoIter->second;
2305 return true;
2306}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002307
2308void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2309 GLenum genMode,
2310 GLint components,
2311 const GLfloat *coeffs)
2312{
2313 UNREACHABLE();
2314}
2315
Jamie Madill8047c0d2016-03-07 13:02:12 -05002316} // namespace rx