blob: 5c949945bc1db2ba8f8527c2973248a086c0fcce [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);
Geoff Lang65d17e52016-09-08 09:52:58 -04001436 if (result.error.isError())
1437 {
1438 infoLog << result.error.getMessage();
1439 return result;
1440 }
1441 else if (!result.linkSuccess)
Jamie Madill31c8c562015-08-19 14:08:03 -04001442 {
1443 infoLog << "Failed to create D3D shaders.";
1444 return result;
1445 }
1446
Jamie Madill4a3c2342015-10-08 12:58:45 -04001447 initUniformBlockInfo();
1448
Geoff Lang7dd2e102014-11-10 15:19:26 -05001449 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001450}
1451
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001452GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001453{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001454 // TODO(jmadill): Do something useful here?
1455 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001456}
1457
Jamie Madill4a3c2342015-10-08 12:58:45 -04001458void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001459{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001460 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001461
Jamie Madill62d31cb2015-09-11 13:25:51 -04001462 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1463 {
1464 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1465 continue;
1466
Jamie Madill4a3c2342015-10-08 12:58:45 -04001467 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001468 continue;
1469
Jamie Madill4a3c2342015-10-08 12:58:45 -04001470 size_t dataSize = getUniformBlockInfo(vertexBlock);
1471 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001472 }
1473
Jamie Madill48ef11b2016-04-27 15:21:52 -04001474 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001475
1476 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1477 {
1478 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1479 continue;
1480
Jamie Madill4a3c2342015-10-08 12:58:45 -04001481 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001482 continue;
1483
Jamie Madill4a3c2342015-10-08 12:58:45 -04001484 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1485 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001486 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001487}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001488
Jamie Madill4a3c2342015-10-08 12:58:45 -04001489void ProgramD3D::assignUniformBlockRegisters()
1490{
1491 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001492
1493 // Assign registers and update sizes.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001494 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1495 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001496
Jamie Madill48ef11b2016-04-27 15:21:52 -04001497 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001498 {
1499 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1500
Jamie Madill4a3c2342015-10-08 12:58:45 -04001501 D3DUniformBlock d3dUniformBlock;
1502
Jamie Madill62d31cb2015-09-11 13:25:51 -04001503 if (uniformBlock.vertexStaticUse)
1504 {
1505 unsigned int baseRegister =
1506 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001507 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001508 }
1509
1510 if (uniformBlock.fragmentStaticUse)
1511 {
1512 unsigned int baseRegister =
1513 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001514 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001515 }
1516
Jamie Madill4a3c2342015-10-08 12:58:45 -04001517 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001518 }
1519}
1520
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001521void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001522{
1523 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001524 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001525 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001526 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001527 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001528 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001529 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001530 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001531 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001532 vertexRegisters = std::max(vertexRegisters,
1533 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001534 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001535 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001536 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001537 fragmentRegisters = std::max(
1538 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001539 }
1540 }
1541 }
1542
Jamie Madill334d6152015-10-22 14:00:28 -04001543 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001544 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1545}
1546
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001547gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001548{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001549 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001550
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001551 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001552 if (error.isError())
1553 {
1554 return error;
1555 }
1556
Jamie Madill62d31cb2015-09-11 13:25:51 -04001557 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001558 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001559 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001560 }
1561
1562 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001563}
1564
Jamie Madill9082b982016-04-27 15:21:51 -04001565gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001566{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001567 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001568 {
1569 return gl::Error(GL_NO_ERROR);
1570 }
1571
1572 // Lazy init.
1573 if (mD3DUniformBlocks.empty())
1574 {
1575 assignUniformBlockRegisters();
1576 }
1577
Jamie Madill03260fa2015-06-22 13:57:22 -04001578 mVertexUBOCache.clear();
1579 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001580
1581 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1582 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1583
Jamie Madill4a3c2342015-10-08 12:58:45 -04001584 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001585 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001586 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001587 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001588 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001589
Brandon Jones18bd4102014-09-22 14:21:44 -07001590 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001591 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001592 {
1593 continue;
1594 }
1595
Jamie Madill4a3c2342015-10-08 12:58:45 -04001596 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001597 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001598 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001599 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001600
Jamie Madill969194d2015-07-20 14:36:56 -04001601 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001602 {
1603 mVertexUBOCache.resize(registerIndex + 1, -1);
1604 }
1605
1606 ASSERT(mVertexUBOCache[registerIndex] == -1);
1607 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001608 }
1609
Jamie Madill4a3c2342015-10-08 12:58:45 -04001610 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001611 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001612 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001613 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001614
1615 if (mFragmentUBOCache.size() <= registerIndex)
1616 {
1617 mFragmentUBOCache.resize(registerIndex + 1, -1);
1618 }
1619
1620 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1621 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001622 }
1623 }
1624
Jamie Madill03260fa2015-06-22 13:57:22 -04001625 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001626}
1627
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001628void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001629{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001630 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001631 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001632 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001633 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001634}
1635
Jamie Madill334d6152015-10-22 14:00:28 -04001636void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001637{
1638 setUniform(location, count, v, GL_FLOAT);
1639}
1640
1641void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1642{
1643 setUniform(location, count, v, GL_FLOAT_VEC2);
1644}
1645
1646void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1647{
1648 setUniform(location, count, v, GL_FLOAT_VEC3);
1649}
1650
1651void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1652{
1653 setUniform(location, count, v, GL_FLOAT_VEC4);
1654}
1655
Jamie Madill334d6152015-10-22 14:00:28 -04001656void ProgramD3D::setUniformMatrix2fv(GLint location,
1657 GLsizei count,
1658 GLboolean transpose,
1659 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001660{
1661 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1662}
1663
Jamie Madill334d6152015-10-22 14:00:28 -04001664void ProgramD3D::setUniformMatrix3fv(GLint location,
1665 GLsizei count,
1666 GLboolean transpose,
1667 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001668{
1669 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1670}
1671
Jamie Madill334d6152015-10-22 14:00:28 -04001672void ProgramD3D::setUniformMatrix4fv(GLint location,
1673 GLsizei count,
1674 GLboolean transpose,
1675 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001676{
1677 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1678}
1679
Jamie Madill334d6152015-10-22 14:00:28 -04001680void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1681 GLsizei count,
1682 GLboolean transpose,
1683 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001684{
1685 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1686}
1687
Jamie Madill334d6152015-10-22 14:00:28 -04001688void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1689 GLsizei count,
1690 GLboolean transpose,
1691 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001692{
1693 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1694}
1695
Jamie Madill334d6152015-10-22 14:00:28 -04001696void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1697 GLsizei count,
1698 GLboolean transpose,
1699 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001700{
1701 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1702}
1703
Jamie Madill334d6152015-10-22 14:00:28 -04001704void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1705 GLsizei count,
1706 GLboolean transpose,
1707 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001708{
1709 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1710}
1711
Jamie Madill334d6152015-10-22 14:00:28 -04001712void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1713 GLsizei count,
1714 GLboolean transpose,
1715 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001716{
1717 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1718}
1719
Jamie Madill334d6152015-10-22 14:00:28 -04001720void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1721 GLsizei count,
1722 GLboolean transpose,
1723 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001724{
1725 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1726}
1727
1728void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1729{
1730 setUniform(location, count, v, GL_INT);
1731}
1732
1733void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1734{
1735 setUniform(location, count, v, GL_INT_VEC2);
1736}
1737
1738void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1739{
1740 setUniform(location, count, v, GL_INT_VEC3);
1741}
1742
1743void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1744{
1745 setUniform(location, count, v, GL_INT_VEC4);
1746}
1747
1748void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1749{
1750 setUniform(location, count, v, GL_UNSIGNED_INT);
1751}
1752
1753void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1754{
1755 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1756}
1757
1758void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1759{
1760 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1761}
1762
1763void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1764{
1765 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1766}
1767
Jamie Madill4a3c2342015-10-08 12:58:45 -04001768void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1769 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001770{
1771}
1772
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001773void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001774{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001775 D3DUniformMap uniformMap;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001776 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001777 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001778
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001779 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001780 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001781 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001782 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001783 }
1784 }
1785
Jamie Madill48ef11b2016-04-27 15:21:52 -04001786 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001787 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001788 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001789 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001790 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001791 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001792 }
1793 }
1794
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001795 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001796 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001797 {
1798 if (!glUniform.isInDefaultBlock())
1799 continue;
1800
1801 auto mapEntry = uniformMap.find(glUniform.name);
1802 ASSERT(mapEntry != uniformMap.end());
1803 mD3DUniforms.push_back(mapEntry->second);
1804 }
1805
Jamie Madill62d31cb2015-09-11 13:25:51 -04001806 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001807 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001808}
1809
Jamie Madill91445bc2015-09-23 16:47:53 -04001810void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001811 const sh::Uniform &uniform,
1812 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001813{
Olli Etuaho96963162016-03-21 11:54:33 +02001814 // Samplers get their registers assigned in assignAllSamplerRegisters.
1815 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001816 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001817 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001818 return;
1819 }
1820
Jamie Madill91445bc2015-09-23 16:47:53 -04001821 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1822
1823 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1824 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001825 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001826 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001827
Jamie Madill91445bc2015-09-23 16:47:53 -04001828 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001829}
1830
Jamie Madill62d31cb2015-09-11 13:25:51 -04001831D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1832{
1833 for (D3DUniform *d3dUniform : mD3DUniforms)
1834 {
1835 if (d3dUniform->name == name)
1836 {
1837 return d3dUniform;
1838 }
1839 }
1840
1841 return nullptr;
1842}
1843
Jamie Madill91445bc2015-09-23 16:47:53 -04001844void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001845 const sh::ShaderVariable &uniform,
1846 const std::string &fullName,
1847 sh::HLSLBlockEncoder *encoder,
1848 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001849{
1850 if (uniform.isStruct())
1851 {
1852 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1853 {
1854 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1855
Jamie Madill55def582015-05-04 11:24:57 -04001856 if (encoder)
1857 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001858
1859 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1860 {
Jamie Madill334d6152015-10-22 14:00:28 -04001861 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001862 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1863
Olli Etuaho96963162016-03-21 11:54:33 +02001864 // Samplers get their registers assigned in assignAllSamplerRegisters.
1865 // Also they couldn't use the same encoder as the rest of the struct, since they are
1866 // extracted out of the struct by the shader translator.
1867 if (gl::IsSamplerType(field.type))
1868 {
1869 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
1870 }
1871 else
1872 {
1873 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
1874 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001875 }
1876
Jamie Madill55def582015-05-04 11:24:57 -04001877 if (encoder)
1878 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001879 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001880 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001881 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001882
1883 // Not a struct. Arrays are treated as aggregate types.
1884 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001885 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001886 encoder->enterAggregateType();
1887 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001888
Jamie Madill62d31cb2015-09-11 13:25:51 -04001889 // Advance the uniform offset, to track registers allocation for structs
1890 sh::BlockMemberInfo blockInfo =
1891 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1892 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001893
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001894 auto uniformMapEntry = uniformMap->find(fullName);
1895 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001896
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001897 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001898 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001899 d3dUniform = uniformMapEntry->second;
1900 }
1901 else
1902 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001903 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001904 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001905 }
1906
1907 if (encoder)
1908 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001909 d3dUniform->registerElement =
1910 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001911 unsigned int reg =
1912 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001913 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001914 {
1915 d3dUniform->psRegisterIndex = reg;
1916 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001917 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001918 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001919 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001920 d3dUniform->vsRegisterIndex = reg;
1921 }
Jamie Madillfb536032015-09-11 13:19:49 -04001922
1923 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001924 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001925 {
1926 encoder->exitAggregateType();
1927 }
1928 }
1929}
1930
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001931template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001932void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001933{
Jamie Madill334d6152015-10-22 14:00:28 -04001934 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001935 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1936
Jamie Madill62d31cb2015-09-11 13:25:51 -04001937 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001938
Jamie Madill62d31cb2015-09-11 13:25:51 -04001939 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001940 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001941 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001942
1943 if (targetUniform->type == targetUniformType)
1944 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001945 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001946
Jamie Madill62d31cb2015-09-11 13:25:51 -04001947 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001948 {
Jamie Madill334d6152015-10-22 14:00:28 -04001949 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001950 const T *source = v + (i * components);
1951
1952 for (int c = 0; c < components; c++)
1953 {
1954 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1955 }
1956 for (int c = components; c < 4; c++)
1957 {
1958 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1959 }
1960 }
1961 }
1962 else if (targetUniform->type == targetBoolType)
1963 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001964 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001965
Jamie Madill62d31cb2015-09-11 13:25:51 -04001966 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001967 {
Jamie Madill334d6152015-10-22 14:00:28 -04001968 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001969 const T *source = v + (i * components);
1970
1971 for (int c = 0; c < components; c++)
1972 {
Jamie Madill334d6152015-10-22 14:00:28 -04001973 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1974 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001975 }
1976 for (int c = components; c < 4; c++)
1977 {
1978 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1979 }
1980 }
1981 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001982 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001983 {
1984 ASSERT(targetUniformType == GL_INT);
1985
Jamie Madill62d31cb2015-09-11 13:25:51 -04001986 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001987
1988 bool wasDirty = targetUniform->dirty;
1989
Jamie Madill62d31cb2015-09-11 13:25:51 -04001990 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001991 {
Jamie Madill334d6152015-10-22 14:00:28 -04001992 GLint *dest = target + (i * 4);
1993 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001994
1995 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1996 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1997 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1998 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1999 }
2000
2001 if (!wasDirty && targetUniform->dirty)
2002 {
2003 mDirtySamplerMapping = true;
2004 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002005 }
Jamie Madill334d6152015-10-22 14:00:28 -04002006 else
2007 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002008}
2009
2010template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002011void ProgramD3D::setUniformMatrixfv(GLint location,
2012 GLsizei countIn,
2013 GLboolean transpose,
2014 const GLfloat *value,
2015 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002016{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002017 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018
Jamie Madill62d31cb2015-09-11 13:25:51 -04002019 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002020 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002021 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002022
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002023 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002024 GLfloat *target =
2025 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002026
Jamie Madill62d31cb2015-09-11 13:25:51 -04002027 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002028 {
2029 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2030 if (transpose == GL_FALSE)
2031 {
Jamie Madill334d6152015-10-22 14:00:28 -04002032 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2033 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002034 }
2035 else
2036 {
Jamie Madill334d6152015-10-22 14:00:28 -04002037 targetUniform->dirty =
2038 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002039 }
2040 target += targetMatrixStride;
2041 value += cols * rows;
2042 }
2043}
2044
Jamie Madill4a3c2342015-10-08 12:58:45 -04002045size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002046{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002047 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002048
Jamie Madill62d31cb2015-09-11 13:25:51 -04002049 // define member uniforms
2050 sh::Std140BlockEncoder std140Encoder;
2051 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2052 sh::BlockLayoutEncoder *encoder = nullptr;
2053
2054 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002055 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002056 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002057 }
2058 else
2059 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002060 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002061 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002062
Jamie Madill39046162016-02-08 15:05:17 -05002063 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2064 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002065
2066 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002067}
2068
Jamie Madill62d31cb2015-09-11 13:25:51 -04002069void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002070{
Olli Etuaho96963162016-03-21 11:54:33 +02002071 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002072 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002073 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002074 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002075 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002076 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002077 }
2078}
2079
Olli Etuaho96963162016-03-21 11:54:33 +02002080void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002081{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002082 ASSERT(d3dUniform->isSampler());
Jamie Madill48ef11b2016-04-27 15:21:52 -04002083 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2084 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Olli Etuaho96963162016-03-21 11:54:33 +02002085 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) || fragmentShaderD3D->hasUniform(d3dUniform));
2086 if (vertexShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002087 {
Olli Etuaho96963162016-03-21 11:54:33 +02002088 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2089 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002090 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2091 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002092 }
Olli Etuaho96963162016-03-21 11:54:33 +02002093 if (fragmentShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002094 {
Olli Etuaho96963162016-03-21 11:54:33 +02002095 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2096 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002097 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2098 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002099 }
2100}
2101
Jamie Madill62d31cb2015-09-11 13:25:51 -04002102// static
2103void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002104 GLenum samplerType,
2105 unsigned int samplerCount,
2106 std::vector<Sampler> &outSamplers,
2107 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002108{
2109 unsigned int samplerIndex = startSamplerIndex;
2110
2111 do
2112 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002113 ASSERT(samplerIndex < outSamplers.size());
2114 Sampler *sampler = &outSamplers[samplerIndex];
2115 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002116 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002117 sampler->logicalTextureUnit = 0;
2118 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002119 samplerIndex++;
2120 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002121}
2122
Brandon Jonesc9610c52014-08-25 17:02:59 -07002123void ProgramD3D::reset()
2124{
Brandon Joneseb994362014-09-24 10:27:28 -07002125 SafeDeleteContainer(mVertexExecutables);
2126 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002127
2128 for (auto &element : mGeometryExecutables)
2129 {
2130 SafeDelete(element);
2131 }
Brandon Joneseb994362014-09-24 10:27:28 -07002132
Brandon Jones22502d52014-08-29 16:58:36 -07002133 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002134 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002135
2136 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002137 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002138 mUsesFragDepth = false;
2139 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002140 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002141 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002142
Jamie Madill62d31cb2015-09-11 13:25:51 -04002143 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002144 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002145
Brandon Jonesc9610c52014-08-25 17:02:59 -07002146 SafeDelete(mVertexUniformStorage);
2147 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002148
2149 mSamplersPS.clear();
2150 mSamplersVS.clear();
2151
2152 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002153 mUsedPixelSamplerRange = 0;
2154 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002155
Jamie Madill8047c0d2016-03-07 13:02:12 -05002156 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002157
Jamie Madill9fc36822015-11-18 13:08:07 -05002158 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002159
2160 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002161}
2162
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163unsigned int ProgramD3D::getSerial() const
2164{
2165 return mSerial;
2166}
2167
2168unsigned int ProgramD3D::issueSerial()
2169{
2170 return mCurrentSerial++;
2171}
2172
Jamie Madill8047c0d2016-03-07 13:02:12 -05002173void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002174{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002175 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002176 ASSERT(vertexShader != nullptr);
2177
2178 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002179 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002180 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002181 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2182 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002183
Jamie Madill8047c0d2016-03-07 13:02:12 -05002184 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002185 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002186 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002187 }
2188 }
Jamie Madill437d2662014-12-05 14:23:35 -05002189}
2190
Jamie Madill63805b42015-08-25 13:17:39 -04002191void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002192{
Jamie Madillbd136f92015-08-10 14:51:37 -04002193 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002194 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002195
Jamie Madill48ef11b2016-04-27 15:21:52 -04002196 for (unsigned int locationIndex : angle::IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002197 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002198 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002199
Jamie Madill8047c0d2016-03-07 13:02:12 -05002200 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002201 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002202 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002203 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002204 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002205 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002206 mCachedInputLayout[d3dSemantic] =
2207 GetVertexFormatType(vertexAttributes[locationIndex],
2208 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002209 }
2210 }
2211}
2212
Jamie Madill9fc36822015-11-18 13:08:07 -05002213void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002214{
Jamie Madill9fc36822015-11-18 13:08:07 -05002215 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2216
2217 const std::string &varyingSemantic =
2218 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2219
Jamie Madillccdf74b2015-08-18 10:46:12 -04002220 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002221 mStreamOutVaryings.clear();
2222
Jamie Madill48ef11b2016-04-27 15:21:52 -04002223 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002224 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2225 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002226 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002227 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2228 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002229 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002230 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002231 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002232 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2233 builtins.glPosition.index, 4, outputSlot));
2234 }
2235 }
2236 else if (tfVaryingName == "gl_FragCoord")
2237 {
2238 if (builtins.glFragCoord.enabled)
2239 {
2240 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2241 builtins.glFragCoord.index, 4, outputSlot));
2242 }
2243 }
2244 else if (tfVaryingName == "gl_PointSize")
2245 {
2246 if (builtins.glPointSize.enabled)
2247 {
2248 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2249 }
2250 }
2251 else
2252 {
2253 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2254 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002255 const auto &varying = *registerInfo.packedVarying->varying;
2256 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002257 int componentCount = gl::VariableColumnCount(transposedType);
2258 ASSERT(!varying.isBuiltIn());
2259
Jamie Madill55c25d02015-11-18 13:08:08 -05002260 // Transform feedback for varying structs is underspecified.
2261 // See Khronos bug 9856.
2262 // TODO(jmadill): Figure out how to be spec-compliant here.
2263 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2264 continue;
2265
Jamie Madill9fc36822015-11-18 13:08:07 -05002266 // There can be more than one register assigned to a particular varying, and each
2267 // register needs its own stream out entry.
2268 if (tfVaryingName == varying.name)
2269 {
2270 mStreamOutVaryings.push_back(D3DVarying(
2271 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2272 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002273 }
2274 }
2275 }
2276}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002277
2278D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2279{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002280 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002281}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002282
2283bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2284{
2285 std::string baseName = blockName;
2286 gl::ParseAndStripArrayIndex(&baseName);
2287
2288 auto sizeIter = mBlockDataSizes.find(baseName);
2289 if (sizeIter == mBlockDataSizes.end())
2290 {
2291 *sizeOut = 0;
2292 return false;
2293 }
2294
2295 *sizeOut = sizeIter->second;
2296 return true;
2297}
2298
2299bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2300 sh::BlockMemberInfo *memberInfoOut) const
2301{
2302 auto infoIter = mBlockInfo.find(memberUniformName);
2303 if (infoIter == mBlockInfo.end())
2304 {
2305 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2306 return false;
2307 }
2308
2309 *memberInfoOut = infoIter->second;
2310 return true;
2311}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002312
2313void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2314 GLenum genMode,
2315 GLint components,
2316 const GLfloat *coeffs)
2317{
2318 UNREACHABLE();
2319}
2320
Jamie Madill8047c0d2016-03-07 13:02:12 -05002321} // namespace rx