blob: 4fc18c8aab3a534690adc8e4c874bbd1dd478ddf [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
380ProgramD3DMetadata::ProgramD3DMetadata(int rendererMajorShaderModel,
381 const std::string &shaderModelSuffix,
382 bool usesInstancedPointSpriteEmulation,
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800383 bool usesViewScale,
Jamie Madille39a3f02015-11-17 20:42:15 -0500384 const ShaderD3D *vertexShader,
385 const ShaderD3D *fragmentShader)
386 : mRendererMajorShaderModel(rendererMajorShaderModel),
387 mShaderModelSuffix(shaderModelSuffix),
388 mUsesInstancedPointSpriteEmulation(usesInstancedPointSpriteEmulation),
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800389 mUsesViewScale(usesViewScale),
Jamie Madille39a3f02015-11-17 20:42:15 -0500390 mVertexShader(vertexShader),
391 mFragmentShader(fragmentShader)
392{
393}
394
395int ProgramD3DMetadata::getRendererMajorShaderModel() const
396{
397 return mRendererMajorShaderModel;
398}
399
Jamie Madill9082b982016-04-27 15:21:51 -0400400bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500401{
Martin Radev1be913c2016-07-11 17:59:16 +0300402 return (mFragmentShader->usesFragColor() && data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500403}
404
Jamie Madill48ef11b2016-04-27 15:21:52 -0400405bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500406{
Jamie Madill63286672015-11-24 13:00:08 -0500407 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500408}
409
410bool ProgramD3DMetadata::usesPointCoord() const
411{
412 return mFragmentShader->usesPointCoord();
413}
414
415bool ProgramD3DMetadata::usesFragCoord() const
416{
417 return mFragmentShader->usesFragCoord();
418}
419
420bool ProgramD3DMetadata::usesPointSize() const
421{
422 return mVertexShader->usesPointSize();
423}
424
425bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
426{
427 return !usesPointSize() && usesPointCoord() && mRendererMajorShaderModel >= 4;
428}
429
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800430bool ProgramD3DMetadata::usesViewScale() const
431{
432 return mUsesViewScale;
433}
434
Jamie Madille39a3f02015-11-17 20:42:15 -0500435bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
436{
437 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader
438 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
439 // GeometryShader PointSprite emulation does not require this additional entry because the
440 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the
441 // PS_INPUT of the generated pixel shader. The Geometry Shader point sprite implementation needs
442 // gl_PointSize to be in VS_OUTPUT and GS_INPUT. Instanced point sprites doesn't need
443 // gl_PointSize in VS_OUTPUT.
444 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
445 usesInsertedPointCoordValue();
446}
447
448bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
449{
450 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
451 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
452 // the vertex shader in this case. This saves us 1 output vector.
453 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
454}
455
456bool ProgramD3DMetadata::usesSystemValuePointSize() const
457{
458 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
459}
460
461bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
462{
463 return mFragmentShader->usesMultipleRenderTargets();
464}
465
466GLint ProgramD3DMetadata::getMajorShaderVersion() const
467{
468 return mVertexShader->getData().getShaderVersion();
469}
470
471const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
472{
473 return mFragmentShader;
474}
475
Jamie Madill28afae52015-11-09 15:07:57 -0500476// ProgramD3D Implementation
477
Jamie Madilld3dfda22015-07-06 08:28:49 -0400478ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
479 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500480 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400481 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700482{
Brandon Joneseb994362014-09-24 10:27:28 -0700483}
484
485ProgramD3D::VertexExecutable::~VertexExecutable()
486{
487 SafeDelete(mShaderExecutable);
488}
489
Jamie Madilld3dfda22015-07-06 08:28:49 -0400490// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500491ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
492 GLenum type)
493{
494 switch (type)
495 {
496 case GL_INT:
497 return HLSLAttribType::SIGNED_INT;
498 case GL_UNSIGNED_INT:
499 return HLSLAttribType::UNSIGNED_INT;
500 case GL_SIGNED_NORMALIZED:
501 case GL_UNSIGNED_NORMALIZED:
502 case GL_FLOAT:
503 return HLSLAttribType::FLOAT;
504 default:
505 UNREACHABLE();
506 return HLSLAttribType::FLOAT;
507 }
508}
509
510// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400511void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
512 const gl::InputLayout &inputLayout,
513 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700514{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500515 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400516
517 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700518 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400519 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500520 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
521 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400522
Jamie Madillbdec2f42016-03-02 16:35:32 -0500523 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
524 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
525 continue;
526
527 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
528 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700529 }
Brandon Joneseb994362014-09-24 10:27:28 -0700530}
531
Jamie Madilld3dfda22015-07-06 08:28:49 -0400532bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
533{
Jamie Madillbd136f92015-08-10 14:51:37 -0400534 size_t limit = std::max(mSignature.size(), signature.size());
535 for (size_t index = 0; index < limit; ++index)
536 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500537 // treat undefined indexes as FLOAT
538 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
539 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400540 if (a != b)
541 return false;
542 }
543
544 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400545}
546
547ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
548 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400549 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700550{
551}
552
553ProgramD3D::PixelExecutable::~PixelExecutable()
554{
555 SafeDelete(mShaderExecutable);
556}
557
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700558ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
559{
560}
561
Geoff Lang7dd2e102014-11-10 15:19:26 -0500562unsigned int ProgramD3D::mCurrentSerial = 1;
563
Jamie Madill48ef11b2016-04-27 15:21:52 -0400564ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
565 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700566 mRenderer(renderer),
567 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400568 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700569 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400570 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700571 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700572 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700573 mUsedVertexSamplerRange(0),
574 mUsedPixelSamplerRange(0),
575 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500576 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700577{
Brandon Joneseb994362014-09-24 10:27:28 -0700578 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700579}
580
581ProgramD3D::~ProgramD3D()
582{
583 reset();
584 SafeDelete(mDynamicHLSL);
585}
586
Brandon Jones44151a92014-09-10 11:32:25 -0700587bool ProgramD3D::usesPointSpriteEmulation() const
588{
589 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
590}
591
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400592bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700593{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400594 if (drawMode != GL_POINTS)
595 {
596 return mUsesFlatInterpolation;
597 }
598
Cooper Partine6664f02015-01-09 16:22:24 -0800599 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
600}
601
602bool ProgramD3D::usesInstancedPointSpriteEmulation() const
603{
604 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700605}
606
Jamie Madill334d6152015-10-22 14:00:28 -0400607GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
608 unsigned int samplerIndex,
609 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700610{
611 GLint logicalTextureUnit = -1;
612
613 switch (type)
614 {
Jamie Madill334d6152015-10-22 14:00:28 -0400615 case gl::SAMPLER_PIXEL:
616 ASSERT(samplerIndex < caps.maxTextureImageUnits);
617 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
618 {
619 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
620 }
621 break;
622 case gl::SAMPLER_VERTEX:
623 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
624 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
625 {
626 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
627 }
628 break;
629 default:
630 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700631 }
632
Jamie Madill334d6152015-10-22 14:00:28 -0400633 if (logicalTextureUnit >= 0 &&
634 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700635 {
636 return logicalTextureUnit;
637 }
638
639 return -1;
640}
641
642// Returns the texture type for a given Direct3D 9 sampler type and
643// index (0-15 for the pixel shader and 0-3 for the vertex shader).
644GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
645{
646 switch (type)
647 {
Jamie Madill334d6152015-10-22 14:00:28 -0400648 case gl::SAMPLER_PIXEL:
649 ASSERT(samplerIndex < mSamplersPS.size());
650 ASSERT(mSamplersPS[samplerIndex].active);
651 return mSamplersPS[samplerIndex].textureType;
652 case gl::SAMPLER_VERTEX:
653 ASSERT(samplerIndex < mSamplersVS.size());
654 ASSERT(mSamplersVS[samplerIndex].active);
655 return mSamplersVS[samplerIndex].textureType;
656 default:
657 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700658 }
659
660 return GL_TEXTURE_2D;
661}
662
Olli Etuaho618bebc2016-01-15 16:40:00 +0200663GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700664{
665 switch (type)
666 {
Jamie Madill334d6152015-10-22 14:00:28 -0400667 case gl::SAMPLER_PIXEL:
668 return mUsedPixelSamplerRange;
669 case gl::SAMPLER_VERTEX:
670 return mUsedVertexSamplerRange;
671 default:
672 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200673 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700674 }
675}
676
677void ProgramD3D::updateSamplerMapping()
678{
679 if (!mDirtySamplerMapping)
680 {
681 return;
682 }
683
684 mDirtySamplerMapping = false;
685
686 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400687 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700688 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400689 if (!d3dUniform->dirty)
690 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700691
Jamie Madill62d31cb2015-09-11 13:25:51 -0400692 if (!d3dUniform->isSampler())
693 continue;
694
695 int count = d3dUniform->elementCount();
696 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
697
698 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700699 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400700 unsigned int firstIndex = d3dUniform->psRegisterIndex;
701
702 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700703 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400704 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700705
Jamie Madill62d31cb2015-09-11 13:25:51 -0400706 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700707 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400708 ASSERT(mSamplersPS[samplerIndex].active);
709 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700710 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400711 }
712 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700713
Jamie Madill62d31cb2015-09-11 13:25:51 -0400714 if (d3dUniform->isReferencedByVertexShader())
715 {
716 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
717
718 for (int i = 0; i < count; i++)
719 {
720 unsigned int samplerIndex = firstIndex + i;
721
722 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700723 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400724 ASSERT(mSamplersVS[samplerIndex].active);
725 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700726 }
727 }
728 }
729 }
730}
731
Geoff Lang7dd2e102014-11-10 15:19:26 -0500732LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700733{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400734 reset();
735
Jamie Madill334d6152015-10-22 14:00:28 -0400736 DeviceIdentifier binaryDeviceIdentifier = {0};
737 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
738 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700739
740 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
741 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
742 {
743 infoLog << "Invalid program binary, device configuration has changed.";
744 return LinkResult(false, gl::Error(GL_NO_ERROR));
745 }
746
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500747 int compileFlags = stream->readInt<int>();
748 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
749 {
Jamie Madillf6113162015-05-07 11:49:21 -0400750 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500751 return LinkResult(false, gl::Error(GL_NO_ERROR));
752 }
753
Jamie Madill8047c0d2016-03-07 13:02:12 -0500754 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400755 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500756 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400757 }
758
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700759 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
760 for (unsigned int i = 0; i < psSamplerCount; ++i)
761 {
762 Sampler sampler;
763 stream->readBool(&sampler.active);
764 stream->readInt(&sampler.logicalTextureUnit);
765 stream->readInt(&sampler.textureType);
766 mSamplersPS.push_back(sampler);
767 }
768 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
769 for (unsigned int i = 0; i < vsSamplerCount; ++i)
770 {
771 Sampler sampler;
772 stream->readBool(&sampler.active);
773 stream->readInt(&sampler.logicalTextureUnit);
774 stream->readInt(&sampler.textureType);
775 mSamplersVS.push_back(sampler);
776 }
777
778 stream->readInt(&mUsedVertexSamplerRange);
779 stream->readInt(&mUsedPixelSamplerRange);
780
781 const unsigned int uniformCount = stream->readInt<unsigned int>();
782 if (stream->error())
783 {
Jamie Madillf6113162015-05-07 11:49:21 -0400784 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500785 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700786 }
787
Jamie Madill48ef11b2016-04-27 15:21:52 -0400788 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400789 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700790 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
791 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400792 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700793
Jamie Madill62d31cb2015-09-11 13:25:51 -0400794 D3DUniform *d3dUniform =
795 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
796 linkedUniform.isInDefaultBlock());
797 stream->readInt(&d3dUniform->psRegisterIndex);
798 stream->readInt(&d3dUniform->vsRegisterIndex);
799 stream->readInt(&d3dUniform->registerCount);
800 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700801
Jamie Madill62d31cb2015-09-11 13:25:51 -0400802 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700803 }
804
Jamie Madill4a3c2342015-10-08 12:58:45 -0400805 const unsigned int blockCount = stream->readInt<unsigned int>();
806 if (stream->error())
807 {
808 infoLog << "Invalid program binary.";
809 return LinkResult(false, gl::Error(GL_NO_ERROR));
810 }
811
812 ASSERT(mD3DUniformBlocks.empty());
813 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
814 {
815 D3DUniformBlock uniformBlock;
816 stream->readInt(&uniformBlock.psRegisterIndex);
817 stream->readInt(&uniformBlock.vsRegisterIndex);
818 mD3DUniformBlocks.push_back(uniformBlock);
819 }
820
Jamie Madill9fc36822015-11-18 13:08:07 -0500821 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
822 mStreamOutVaryings.resize(streamOutVaryingCount);
823 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700824 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500825 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700826
Jamie Madill28afae52015-11-09 15:07:57 -0500827 stream->readString(&varying->semanticName);
828 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500829 stream->readInt(&varying->componentCount);
830 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700831 }
832
Brandon Jones22502d52014-08-29 16:58:36 -0700833 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400834 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
835 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700836 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400837 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
838 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700839 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700840 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400841 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700842
843 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
844 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400845 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
846 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700847 {
848 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
849 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
850 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
851 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
852 }
853
Jamie Madill4e31ad52015-10-29 10:32:57 -0400854 stream->readString(&mGeometryShaderPreamble);
855
Jamie Madill334d6152015-10-22 14:00:28 -0400856 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700857
858 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400859 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
860 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700861 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400862 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400863 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700864
Jamie Madilld3dfda22015-07-06 08:28:49 -0400865 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700866 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400867 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700868 }
869
Jamie Madill334d6152015-10-22 14:00:28 -0400870 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700871 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400872
Jamie Madillada9ecc2015-08-17 12:53:37 -0400873 ShaderExecutableD3D *shaderExecutable = nullptr;
874
875 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500876 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400877 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400878 if (error.isError())
879 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500880 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400881 }
882
Brandon Joneseb994362014-09-24 10:27:28 -0700883 if (!shaderExecutable)
884 {
Jamie Madillf6113162015-05-07 11:49:21 -0400885 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500886 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700887 }
888
889 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400890 VertexExecutable::Signature signature;
891 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700892
893 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400894 mVertexExecutables.push_back(
895 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700896
897 stream->skip(vertexShaderSize);
898 }
899
900 const size_t pixelShaderCount = stream->readInt<unsigned int>();
901 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
902 {
903 const size_t outputCount = stream->readInt<unsigned int>();
904 std::vector<GLenum> outputs(outputCount);
905 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
906 {
907 stream->readInt(&outputs[outputIndex]);
908 }
909
Jamie Madill334d6152015-10-22 14:00:28 -0400910 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700911 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400912 ShaderExecutableD3D *shaderExecutable = nullptr;
913
914 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500915 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400916 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400917 if (error.isError())
918 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500919 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400920 }
Brandon Joneseb994362014-09-24 10:27:28 -0700921
922 if (!shaderExecutable)
923 {
Jamie Madillf6113162015-05-07 11:49:21 -0400924 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500925 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700926 }
927
928 // add new binary
929 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
930
931 stream->skip(pixelShaderSize);
932 }
933
Jamie Madill4e31ad52015-10-29 10:32:57 -0400934 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
935 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700936 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400937 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
938 if (geometryShaderSize == 0)
939 {
940 mGeometryExecutables[geometryExeIndex] = nullptr;
941 continue;
942 }
943
Brandon Joneseb994362014-09-24 10:27:28 -0700944 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400945 bool splitAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400946
Jamie Madill28afae52015-11-09 15:07:57 -0500947 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500948 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, mStreamOutVaryings,
949 splitAttribs, &mGeometryExecutables[geometryExeIndex]);
Geoff Langb543aff2014-09-30 14:52:54 -0400950 if (error.isError())
951 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500952 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400953 }
Brandon Joneseb994362014-09-24 10:27:28 -0700954
Jamie Madill4e31ad52015-10-29 10:32:57 -0400955 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700956 {
Jamie Madillf6113162015-05-07 11:49:21 -0400957 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500958 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700959 }
960 stream->skip(geometryShaderSize);
961 }
962
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700963 initializeUniformStorage();
964
Geoff Lang7dd2e102014-11-10 15:19:26 -0500965 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700966}
967
Geoff Langb543aff2014-09-30 14:52:54 -0400968gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700969{
Austin Kinross137b1512015-06-17 16:14:53 -0700970 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400971 // When we load the binary again later, we can validate the device identifier before trying to
972 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700973 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400974 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
975 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700976
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500977 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
978
Jamie Madill8047c0d2016-03-07 13:02:12 -0500979 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400980 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500981 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -0400982 }
983
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700984 stream->writeInt(mSamplersPS.size());
985 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
986 {
987 stream->writeInt(mSamplersPS[i].active);
988 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
989 stream->writeInt(mSamplersPS[i].textureType);
990 }
991
992 stream->writeInt(mSamplersVS.size());
993 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
994 {
995 stream->writeInt(mSamplersVS[i].active);
996 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
997 stream->writeInt(mSamplersVS[i].textureType);
998 }
999
1000 stream->writeInt(mUsedVertexSamplerRange);
1001 stream->writeInt(mUsedPixelSamplerRange);
1002
Jamie Madill62d31cb2015-09-11 13:25:51 -04001003 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001004 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001005 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001006 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001007 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1008 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001009 stream->writeInt(uniform->registerCount);
1010 stream->writeInt(uniform->registerElement);
1011 }
1012
1013 stream->writeInt(mD3DUniformBlocks.size());
1014 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1015 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001016 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1017 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001018 }
1019
Jamie Madill9fc36822015-11-18 13:08:07 -05001020 stream->writeInt(mStreamOutVaryings.size());
1021 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001022 {
Brandon Joneseb994362014-09-24 10:27:28 -07001023 stream->writeString(varying.semanticName);
1024 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001025 stream->writeInt(varying.componentCount);
1026 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001027 }
1028
Brandon Jones22502d52014-08-29 16:58:36 -07001029 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001030 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1031 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001032 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001033 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1034 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001035 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001036 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001037 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001038
Brandon Joneseb994362014-09-24 10:27:28 -07001039 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001040 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001041 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1042 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001043 {
Brandon Joneseb994362014-09-24 10:27:28 -07001044 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001045 stream->writeInt(variable.type);
1046 stream->writeString(variable.name);
1047 stream->writeString(variable.source);
1048 stream->writeInt(variable.outputIndex);
1049 }
1050
Jamie Madill4e31ad52015-10-29 10:32:57 -04001051 stream->writeString(mGeometryShaderPreamble);
1052
Brandon Joneseb994362014-09-24 10:27:28 -07001053 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001054 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1055 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001056 {
1057 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1058
Jamie Madilld3dfda22015-07-06 08:28:49 -04001059 const auto &inputLayout = vertexExecutable->inputs();
1060 stream->writeInt(inputLayout.size());
1061
1062 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001063 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001064 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001065 }
1066
1067 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1068 stream->writeInt(vertexShaderSize);
1069
1070 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1071 stream->writeBytes(vertexBlob, vertexShaderSize);
1072 }
1073
1074 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001075 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1076 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001077 {
1078 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1079
1080 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1081 stream->writeInt(outputs.size());
1082 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1083 {
1084 stream->writeInt(outputs[outputIndex]);
1085 }
1086
1087 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1088 stream->writeInt(pixelShaderSize);
1089
1090 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1091 stream->writeBytes(pixelBlob, pixelShaderSize);
1092 }
1093
Jamie Madill4e31ad52015-10-29 10:32:57 -04001094 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001095 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001096 if (geometryExe == nullptr)
1097 {
1098 stream->writeInt(0);
1099 continue;
1100 }
1101
1102 size_t geometryShaderSize = geometryExe->getLength();
1103 stream->writeInt(geometryShaderSize);
1104 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001105 }
1106
Geoff Langb543aff2014-09-30 14:52:54 -04001107 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001108}
1109
Geoff Langc5629752015-12-07 16:29:04 -05001110void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1111{
1112}
1113
Jamie Madill334d6152015-10-22 14:00:28 -04001114gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1115 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001116{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001117 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001118
Jamie Madill85a18042015-03-05 15:41:41 -05001119 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001120 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001121
1122 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1123 {
1124 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1125
1126 if (colorbuffer)
1127 {
Jamie Madill334d6152015-10-22 14:00:28 -04001128 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1129 ? GL_COLOR_ATTACHMENT0
1130 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001131 }
1132 else
1133 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001134 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001135 }
1136 }
1137
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001138 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001139}
1140
Jamie Madill97399232014-12-23 12:31:15 -05001141gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001142 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001143 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001144{
1145 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1146 {
1147 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1148 {
Geoff Langb543aff2014-09-30 14:52:54 -04001149 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1150 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001151 }
1152 }
1153
Jamie Madill334d6152015-10-22 14:00:28 -04001154 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1155 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001156
1157 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001158 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001159
1160 gl::InfoLog tempInfoLog;
1161 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1162
Jamie Madillada9ecc2015-08-17 12:53:37 -04001163 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001164 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001165 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001166 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001167 if (error.isError())
1168 {
1169 return error;
1170 }
Brandon Joneseb994362014-09-24 10:27:28 -07001171
Jamie Madill97399232014-12-23 12:31:15 -05001172 if (pixelExecutable)
1173 {
1174 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1175 }
1176 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001177 {
1178 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001179 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001180 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1181 }
Brandon Jones22502d52014-08-29 16:58:36 -07001182
Geoff Langb543aff2014-09-30 14:52:54 -04001183 *outExectuable = pixelExecutable;
1184 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001185}
1186
Jamie Madilld3dfda22015-07-06 08:28:49 -04001187gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001188 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001189 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001190{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001191 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001192
1193 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1194 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001195 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001196 {
Geoff Langb543aff2014-09-30 14:52:54 -04001197 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1198 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001199 }
1200 }
1201
Brandon Jones22502d52014-08-29 16:58:36 -07001202 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001203 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001204 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001205
1206 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001207 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001208
1209 gl::InfoLog tempInfoLog;
1210 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1211
Jamie Madillada9ecc2015-08-17 12:53:37 -04001212 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001213 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001214 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001215 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001216 if (error.isError())
1217 {
1218 return error;
1219 }
1220
Jamie Madill97399232014-12-23 12:31:15 -05001221 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001222 {
Jamie Madill334d6152015-10-22 14:00:28 -04001223 mVertexExecutables.push_back(
1224 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001225 }
Jamie Madill97399232014-12-23 12:31:15 -05001226 else if (!infoLog)
1227 {
1228 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001229 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001230 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1231 }
Brandon Jones22502d52014-08-29 16:58:36 -07001232
Geoff Langb543aff2014-09-30 14:52:54 -04001233 *outExectuable = vertexExecutable;
1234 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001235}
1236
Jamie Madill9082b982016-04-27 15:21:51 -04001237gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001238 GLenum drawMode,
1239 ShaderExecutableD3D **outExecutable,
1240 gl::InfoLog *infoLog)
1241{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001242 if (outExecutable)
1243 {
1244 *outExecutable = nullptr;
1245 }
1246
Austin Kinross88829e82016-01-12 13:04:41 -08001247 // Return a null shader if the current rendering doesn't use a geometry shader
1248 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001249 {
1250 return gl::Error(GL_NO_ERROR);
1251 }
1252
Jamie Madill4e31ad52015-10-29 10:32:57 -04001253 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1254
1255 if (mGeometryExecutables[geometryShaderType] != nullptr)
1256 {
1257 if (outExecutable)
1258 {
1259 *outExecutable = mGeometryExecutables[geometryShaderType];
1260 }
1261 return gl::Error(GL_NO_ERROR);
1262 }
1263
1264 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001265 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001266 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001267
1268 gl::InfoLog tempInfoLog;
1269 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1270
1271 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001272 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001273 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
Jamie Madill4e31ad52015-10-29 10:32:57 -04001274 &mGeometryExecutables[geometryShaderType]);
1275
1276 if (!infoLog && error.isError())
1277 {
1278 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1279 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1280 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1281 }
1282
1283 if (outExecutable)
1284 {
1285 *outExecutable = mGeometryExecutables[geometryShaderType];
1286 }
1287 return error;
1288}
1289
Jamie Madill9082b982016-04-27 15:21:51 -04001290LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001291{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001292 const gl::InputLayout &defaultInputLayout =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001293 GetDefaultInputLayoutFromShader(mState.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001294 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001295 gl::Error error =
1296 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001297 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001298 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001299 return LinkResult(false, error);
1300 }
Austin Kinross434953e2015-02-20 10:49:51 -08001301
Jamie Madill334d6152015-10-22 14:00:28 -04001302 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001303 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001304 error =
1305 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001306 if (error.isError())
1307 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001308 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001309 }
Brandon Jones44151a92014-09-10 11:32:25 -07001310
Jamie Madill4e31ad52015-10-29 10:32:57 -04001311 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill847638a2015-11-20 13:01:41 -05001312 ShaderExecutableD3D *pointGS = nullptr;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001313 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001314 {
Jamie Madill847638a2015-11-20 13:01:41 -05001315 getGeometryExecutableForPrimitiveType(data, GL_POINTS, &pointGS, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001316 }
1317
Jamie Madill48ef11b2016-04-27 15:21:52 -04001318 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001319
1320 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001321 {
Jamie Madill334d6152015-10-22 14:00:28 -04001322 // Geometry shaders are currently only used internally, so there is no corresponding shader
1323 // object at the interface level. For now the geometry shader debug info is prepended to
1324 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001325 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001326 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001327 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1328 }
1329
1330 if (defaultVertexExecutable)
1331 {
1332 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1333 }
1334
1335 if (defaultPixelExecutable)
1336 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001337 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001338 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001339 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1340 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001341
Jamie Madill847638a2015-11-20 13:01:41 -05001342 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable &&
1343 (!usesGeometryShader(GL_POINTS) || pointGS));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001344 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001345}
1346
Jamie Madill9082b982016-04-27 15:21:51 -04001347LinkResult ProgramD3D::link(const gl::ContextState &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001348{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001349 reset();
1350
Jamie Madill48ef11b2016-04-27 15:21:52 -04001351 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1352 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001353
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001354 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1355 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1356
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001357 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1358 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001359
Arun Patole44efa0b2015-03-04 17:11:05 +05301360 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001361 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1362
Jamie Madill53ea9cc2016-05-17 10:12:52 -04001363 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
Austin Kinross02df7962015-07-01 10:03:42 -07001364 {
1365 if (fragmentShaderD3D->usesFrontFacing())
1366 {
1367 infoLog << "The current renderer doesn't support gl_FrontFacing";
1368 return LinkResult(false, gl::Error(GL_NO_ERROR));
1369 }
1370 }
1371
Jamie Madillca03b352015-09-02 12:38:13 -04001372 std::vector<PackedVarying> packedVaryings =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001373 MergeVaryings(*vertexShader, *fragmentShader, mState.getTransformFeedbackVaryingNames());
Jamie Madillca03b352015-09-02 12:38:13 -04001374
Brandon Jones22502d52014-08-29 16:58:36 -07001375 // Map the varyings to the register file
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001376 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors);
Jamie Madill9fc36822015-11-18 13:08:07 -05001377 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001378 mState.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001379 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001380 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001381 }
1382
Jamie Madille39a3f02015-11-17 20:42:15 -05001383 ProgramD3DMetadata metadata(mRenderer->getMajorShaderModel(), mRenderer->getShaderModelSuffix(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001384 usesInstancedPointSpriteEmulation(),
1385 mRenderer->presentPathFastEnabled(), vertexShaderD3D,
Jamie Madille39a3f02015-11-17 20:42:15 -05001386 fragmentShaderD3D);
1387
Jamie Madill9fc36822015-11-18 13:08:07 -05001388 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1389 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1390
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001391 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.getCaps().maxVaryingVectors)
Jamie Madill9fc36822015-11-18 13:08:07 -05001392 {
1393 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1394 return LinkResult(false, gl::Error(GL_NO_ERROR));
1395 }
1396
1397 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1398 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1399 // intelligently, but D3D9 assumes one semantic per register.
1400 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001401 varyingPacking.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
Jamie Madill9fc36822015-11-18 13:08:07 -05001402 {
1403 infoLog << "Cannot pack these varyings on D3D9.";
1404 return LinkResult(false, gl::Error(GL_NO_ERROR));
1405 }
1406
Jamie Madill48ef11b2016-04-27 15:21:52 -04001407 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, varyingPacking, &mPixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -05001408 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001409 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001410 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001411 }
1412
Brandon Jones44151a92014-09-10 11:32:25 -07001413 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001414 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1415 mUsesFragDepth = metadata.usesFragDepth();
Brandon Jones44151a92014-09-10 11:32:25 -07001416
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001417 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001418 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001419 for (const auto &varying : packedVaryings)
1420 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001421 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001422 {
1423 mUsesFlatInterpolation = true;
1424 break;
1425 }
1426 }
1427
Jamie Madill4e31ad52015-10-29 10:32:57 -04001428 if (mRenderer->getMajorShaderModel() >= 4)
1429 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001430 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1431 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001432 }
1433
Jamie Madill8047c0d2016-03-07 13:02:12 -05001434 initAttribLocationsToD3DSemantic();
Jamie Madill437d2662014-12-05 14:23:35 -05001435
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001436 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001437
Jamie Madill9fc36822015-11-18 13:08:07 -05001438 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001439
Jamie Madill4e31ad52015-10-29 10:32:57 -04001440 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001441 if (result.error.isError() || !result.linkSuccess)
1442 {
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