blob: 58ac5f82c48b601f1fb076b01173792e12e3fad9 [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
Dian Xianga4928832015-09-15 10:11:17 -070011#include "common/BitSetIterator.h"
Jamie Madill437d2662014-12-05 14:23:35 -050012#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Framebuffer.h"
14#include "libANGLE/FramebufferAttachment.h"
15#include "libANGLE/Program.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040016#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000017#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050018#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050019#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/renderer/d3d/RendererD3D.h"
21#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050022#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill65345da2015-11-13 11:25:23 -050023#include "libANGLE/renderer/d3d/VaryingPacking.h"
Jamie Madill437d2662014-12-05 14:23:35 -050024#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050025
Brandon Jonesc9610c52014-08-25 17:02:59 -070026namespace rx
27{
28
Brandon Joneseb994362014-09-24 10:27:28 -070029namespace
30{
31
Jamie Madillf8dd7b12015-08-05 13:50:08 -040032gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070033{
Jamie Madillbd136f92015-08-10 14:51:37 -040034 gl::InputLayout defaultLayout;
35 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070036 {
Brandon Joneseb994362014-09-24 10:27:28 -070037 if (shaderAttr.type != GL_NONE)
38 {
39 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
40
Jamie Madilld3dfda22015-07-06 08:28:49 -040041 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040042 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070043 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040044 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040045 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040046 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040047 gl::VertexFormatType defaultType =
48 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070049
Jamie Madillbd136f92015-08-10 14:51:37 -040050 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070051 }
52 }
53 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040054
55 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070056}
57
Jamie Madill334d6152015-10-22 14:00:28 -040058std::vector<GLenum> GetDefaultOutputLayoutFromShader(
59 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070060{
Jamie Madillb4463142014-12-19 14:56:54 -050061 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070062
Jamie Madillb4463142014-12-19 14:56:54 -050063 if (!shaderOutputVars.empty())
64 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070065 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
66 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050067 }
Brandon Joneseb994362014-09-24 10:27:28 -070068
69 return defaultPixelOutput;
70}
71
Brandon Jones1a8a7e32014-10-01 12:49:30 -070072bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
73{
74 return var.isRowMajorLayout;
75}
76
77bool IsRowMajorLayout(const sh::ShaderVariable &var)
78{
79 return false;
80}
81
Jamie Madill437d2662014-12-05 14:23:35 -050082struct AttributeSorter
83{
Jamie Madill63805b42015-08-25 13:17:39 -040084 AttributeSorter(const ProgramD3D::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -050085 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -050086 {
87 }
88
89 bool operator()(int a, int b)
90 {
Jamie Madill80d934b2015-02-19 10:16:12 -050091 int indexA = (*originalIndices)[a];
92 int indexB = (*originalIndices)[b];
93
Jamie Madill334d6152015-10-22 14:00:28 -040094 if (indexA == -1)
95 return false;
96 if (indexB == -1)
97 return true;
Jamie Madill80d934b2015-02-19 10:16:12 -050098 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -050099 }
100
Jamie Madill63805b42015-08-25 13:17:39 -0400101 const ProgramD3D::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500102};
103
Jamie Madill9fc36822015-11-18 13:08:07 -0500104// true if varying x has a higher priority in packing than y
105bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
106{
107 return gl::CompareVarying(*x.varying, *y.varying);
108}
109
Jamie Madillca03b352015-09-02 12:38:13 -0400110std::vector<PackedVarying> MergeVaryings(const gl::Shader &vertexShader,
111 const gl::Shader &fragmentShader,
112 const std::vector<std::string> &tfVaryings)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400113{
Jamie Madillca03b352015-09-02 12:38:13 -0400114 std::vector<PackedVarying> packedVaryings;
115
116 for (const sh::Varying &output : vertexShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400117 {
Jamie Madillca03b352015-09-02 12:38:13 -0400118 bool packed = false;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400119
120 // Built-in varyings obey special rules
Jamie Madillca03b352015-09-02 12:38:13 -0400121 if (output.isBuiltIn())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400122 {
123 continue;
124 }
125
Jamie Madillca03b352015-09-02 12:38:13 -0400126 for (const sh::Varying &input : fragmentShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400127 {
Jamie Madillca03b352015-09-02 12:38:13 -0400128 if (output.name == input.name)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400129 {
Jamie Madillca03b352015-09-02 12:38:13 -0400130 packedVaryings.push_back(PackedVarying(input));
131 packed = true;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400132 break;
133 }
134 }
135
Jamie Madillca03b352015-09-02 12:38:13 -0400136 // Keep Transform FB varyings in the merged list always.
137 if (!packed)
138 {
139 for (const std::string &tfVarying : tfVaryings)
140 {
141 if (tfVarying == output.name)
142 {
143 packedVaryings.push_back(PackedVarying(output));
144 packedVaryings.back().vertexOnly = true;
145 break;
146 }
147 }
148 }
Jamie Madillada9ecc2015-08-17 12:53:37 -0400149 }
150
Jamie Madill9fc36822015-11-18 13:08:07 -0500151 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
152
Jamie Madillca03b352015-09-02 12:38:13 -0400153 return packedVaryings;
Brandon Joneseb994362014-09-24 10:27:28 -0700154}
155
Jamie Madill62d31cb2015-09-11 13:25:51 -0400156template <typename VarT>
157void GetUniformBlockInfo(const std::vector<VarT> &fields,
158 const std::string &prefix,
159 sh::BlockLayoutEncoder *encoder,
160 bool inRowMajorLayout,
161 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
162{
163 for (const VarT &field : fields)
164 {
165 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
166
167 if (field.isStruct())
168 {
169 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
170
171 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
172 {
173 encoder->enterAggregateType();
174
175 const std::string uniformElementName =
176 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
177 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
178 blockInfoOut);
179
180 encoder->exitAggregateType();
181 }
182 }
183 else
184 {
185 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
186 (*blockInfoOut)[fieldName] =
187 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
188 }
189 }
190}
191
Jamie Madill334d6152015-10-22 14:00:28 -0400192template <typename T>
193bool TransposeMatrix(T *target,
194 const GLfloat *value,
195 int targetWidth,
196 int targetHeight,
197 int srcWidth,
198 int srcHeight)
199{
200 bool dirty = false;
201 int copyWidth = std::min(targetHeight, srcWidth);
202 int copyHeight = std::min(targetWidth, srcHeight);
203
204 for (int x = 0; x < copyWidth; x++)
205 {
206 for (int y = 0; y < copyHeight; y++)
207 {
208 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]),
209 &dirty);
210 }
211 }
212 // clear unfilled right side
213 for (int y = 0; y < copyWidth; y++)
214 {
215 for (int x = copyHeight; x < targetWidth; x++)
216 {
217 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
218 }
219 }
220 // clear unfilled bottom.
221 for (int y = copyWidth; y < targetHeight; y++)
222 {
223 for (int x = 0; x < targetWidth; x++)
224 {
225 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
226 }
227 }
228
229 return dirty;
230}
231
232template <typename T>
233bool ExpandMatrix(T *target,
234 const GLfloat *value,
235 int targetWidth,
236 int targetHeight,
237 int srcWidth,
238 int srcHeight)
239{
240 bool dirty = false;
241 int copyWidth = std::min(targetWidth, srcWidth);
242 int copyHeight = std::min(targetHeight, srcHeight);
243
244 for (int y = 0; y < copyHeight; y++)
245 {
246 for (int x = 0; x < copyWidth; x++)
247 {
248 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]),
249 &dirty);
250 }
251 }
252 // clear unfilled right side
253 for (int y = 0; y < copyHeight; y++)
254 {
255 for (int x = copyWidth; x < targetWidth; x++)
256 {
257 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
258 }
259 }
260 // clear unfilled bottom.
261 for (int y = copyHeight; y < targetHeight; y++)
262 {
263 for (int x = 0; x < targetWidth; x++)
264 {
265 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
266 }
267 }
268
269 return dirty;
270}
271
Jamie Madill4e31ad52015-10-29 10:32:57 -0400272gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
273{
274 switch (drawMode)
275 {
276 // Uses the point sprite geometry shader.
277 case GL_POINTS:
278 return gl::PRIMITIVE_POINTS;
279
280 // All line drawing uses the same geometry shader.
281 case GL_LINES:
282 case GL_LINE_STRIP:
283 case GL_LINE_LOOP:
284 return gl::PRIMITIVE_LINES;
285
286 // The triangle fan primitive is emulated with strips in D3D11.
287 case GL_TRIANGLES:
288 case GL_TRIANGLE_FAN:
289 return gl::PRIMITIVE_TRIANGLES;
290
291 // Special case for triangle strips.
292 case GL_TRIANGLE_STRIP:
293 return gl::PRIMITIVE_TRIANGLE_STRIP;
294
295 default:
296 UNREACHABLE();
297 return gl::PRIMITIVE_TYPE_MAX;
298 }
299}
300
Jamie Madillada9ecc2015-08-17 12:53:37 -0400301} // anonymous namespace
302
Jamie Madill28afae52015-11-09 15:07:57 -0500303// D3DUniform Implementation
304
Jamie Madill62d31cb2015-09-11 13:25:51 -0400305D3DUniform::D3DUniform(GLenum typeIn,
306 const std::string &nameIn,
307 unsigned int arraySizeIn,
308 bool defaultBlock)
309 : type(typeIn),
310 name(nameIn),
311 arraySize(arraySizeIn),
312 data(nullptr),
313 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400314 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400315 psRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400316 registerCount(0),
317 registerElement(0)
318{
319 // We use data storage for default block uniforms to cache values that are sent to D3D during
320 // rendering
321 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
322 if (defaultBlock)
323 {
324 size_t bytes = gl::VariableInternalSize(type) * elementCount();
325 data = new uint8_t[bytes];
326 memset(data, 0, bytes);
327
328 // TODO(jmadill): is this correct with non-square matrices?
329 registerCount = gl::VariableRowCount(type) * elementCount();
330 }
331}
332
333D3DUniform::~D3DUniform()
334{
335 SafeDeleteArray(data);
336}
337
338bool D3DUniform::isSampler() const
339{
340 return gl::IsSamplerType(type);
341}
342
343bool D3DUniform::isReferencedByVertexShader() const
344{
345 return vsRegisterIndex != GL_INVALID_INDEX;
346}
347
348bool D3DUniform::isReferencedByFragmentShader() const
349{
350 return psRegisterIndex != GL_INVALID_INDEX;
351}
352
Jamie Madill28afae52015-11-09 15:07:57 -0500353// D3DVarying Implementation
354
Jamie Madill9fc36822015-11-18 13:08:07 -0500355D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500356{
357}
358
Jamie Madill9fc36822015-11-18 13:08:07 -0500359D3DVarying::D3DVarying(const std::string &semanticNameIn,
360 unsigned int semanticIndexIn,
361 unsigned int componentCountIn,
362 unsigned int outputSlotIn)
363 : semanticName(semanticNameIn),
364 semanticIndex(semanticIndexIn),
365 componentCount(componentCountIn),
366 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500367{
368}
369
Jamie Madille39a3f02015-11-17 20:42:15 -0500370// ProgramD3DMetadata Implementation
371
372ProgramD3DMetadata::ProgramD3DMetadata(int rendererMajorShaderModel,
373 const std::string &shaderModelSuffix,
374 bool usesInstancedPointSpriteEmulation,
375 const ShaderD3D *vertexShader,
376 const ShaderD3D *fragmentShader)
377 : mRendererMajorShaderModel(rendererMajorShaderModel),
378 mShaderModelSuffix(shaderModelSuffix),
379 mUsesInstancedPointSpriteEmulation(usesInstancedPointSpriteEmulation),
380 mVertexShader(vertexShader),
381 mFragmentShader(fragmentShader)
382{
383}
384
385int ProgramD3DMetadata::getRendererMajorShaderModel() const
386{
387 return mRendererMajorShaderModel;
388}
389
390bool ProgramD3DMetadata::usesBroadcast(const gl::Data &data) const
391{
392 return (mFragmentShader->usesFragColor() && data.clientVersion < 3);
393}
394
395bool ProgramD3DMetadata::usesFragDepth(const gl::Program::Data &programData) const
396{
397 // TODO(jmadill): Rename this or check if we need it for version 300
398 return (getMajorShaderVersion() < 300 && mFragmentShader->usesFragDepth());
399}
400
401bool ProgramD3DMetadata::usesPointCoord() const
402{
403 return mFragmentShader->usesPointCoord();
404}
405
406bool ProgramD3DMetadata::usesFragCoord() const
407{
408 return mFragmentShader->usesFragCoord();
409}
410
411bool ProgramD3DMetadata::usesPointSize() const
412{
413 return mVertexShader->usesPointSize();
414}
415
416bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
417{
418 return !usesPointSize() && usesPointCoord() && mRendererMajorShaderModel >= 4;
419}
420
421bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
422{
423 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader
424 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
425 // GeometryShader PointSprite emulation does not require this additional entry because the
426 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the
427 // PS_INPUT of the generated pixel shader. The Geometry Shader point sprite implementation needs
428 // gl_PointSize to be in VS_OUTPUT and GS_INPUT. Instanced point sprites doesn't need
429 // gl_PointSize in VS_OUTPUT.
430 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
431 usesInsertedPointCoordValue();
432}
433
434bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
435{
436 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
437 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
438 // the vertex shader in this case. This saves us 1 output vector.
439 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
440}
441
442bool ProgramD3DMetadata::usesSystemValuePointSize() const
443{
444 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
445}
446
447bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
448{
449 return mFragmentShader->usesMultipleRenderTargets();
450}
451
452GLint ProgramD3DMetadata::getMajorShaderVersion() const
453{
454 return mVertexShader->getData().getShaderVersion();
455}
456
457const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
458{
459 return mFragmentShader;
460}
461
Jamie Madill28afae52015-11-09 15:07:57 -0500462// ProgramD3D Implementation
463
Jamie Madilld3dfda22015-07-06 08:28:49 -0400464ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
465 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500466 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400467 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700468{
Brandon Joneseb994362014-09-24 10:27:28 -0700469}
470
471ProgramD3D::VertexExecutable::~VertexExecutable()
472{
473 SafeDelete(mShaderExecutable);
474}
475
Jamie Madilld3dfda22015-07-06 08:28:49 -0400476// static
477void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
478 const gl::InputLayout &inputLayout,
479 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700480{
Jamie Madillbd136f92015-08-10 14:51:37 -0400481 signatureOut->resize(inputLayout.size());
Jamie Madilld3dfda22015-07-06 08:28:49 -0400482
483 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700484 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400485 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbd136f92015-08-10 14:51:37 -0400486 bool converted = false;
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400487 if (vertexFormatType != gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700488 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400489 VertexConversionType conversionType =
490 renderer->getVertexConversionType(vertexFormatType);
Jamie Madillbd136f92015-08-10 14:51:37 -0400491 converted = ((conversionType & VERTEX_CONVERT_GPU) != 0);
Brandon Joneseb994362014-09-24 10:27:28 -0700492 }
Jamie Madillbd136f92015-08-10 14:51:37 -0400493
494 (*signatureOut)[index] = converted;
Brandon Joneseb994362014-09-24 10:27:28 -0700495 }
Brandon Joneseb994362014-09-24 10:27:28 -0700496}
497
Jamie Madilld3dfda22015-07-06 08:28:49 -0400498bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
499{
Jamie Madillbd136f92015-08-10 14:51:37 -0400500 size_t limit = std::max(mSignature.size(), signature.size());
501 for (size_t index = 0; index < limit; ++index)
502 {
503 // treat undefined indexes as 'not converted'
504 bool a = index < signature.size() ? signature[index] : false;
505 bool b = index < mSignature.size() ? mSignature[index] : false;
506 if (a != b)
507 return false;
508 }
509
510 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400511}
512
513ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
514 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400515 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700516{
517}
518
519ProgramD3D::PixelExecutable::~PixelExecutable()
520{
521 SafeDelete(mShaderExecutable);
522}
523
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700524ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
525{
526}
527
Geoff Lang7dd2e102014-11-10 15:19:26 -0500528unsigned int ProgramD3D::mCurrentSerial = 1;
529
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400530ProgramD3D::ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer)
531 : ProgramImpl(data),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700532 mRenderer(renderer),
533 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400534 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700535 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400536 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700537 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700538 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700539 mUsedVertexSamplerRange(0),
540 mUsedPixelSamplerRange(0),
541 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500542 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700543{
Brandon Joneseb994362014-09-24 10:27:28 -0700544 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700545}
546
547ProgramD3D::~ProgramD3D()
548{
549 reset();
550 SafeDelete(mDynamicHLSL);
551}
552
Brandon Jones44151a92014-09-10 11:32:25 -0700553bool ProgramD3D::usesPointSpriteEmulation() const
554{
555 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
556}
557
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400558bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700559{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400560 if (drawMode != GL_POINTS)
561 {
562 return mUsesFlatInterpolation;
563 }
564
Cooper Partine6664f02015-01-09 16:22:24 -0800565 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
566}
567
568bool ProgramD3D::usesInstancedPointSpriteEmulation() const
569{
570 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700571}
572
Jamie Madill334d6152015-10-22 14:00:28 -0400573GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
574 unsigned int samplerIndex,
575 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700576{
577 GLint logicalTextureUnit = -1;
578
579 switch (type)
580 {
Jamie Madill334d6152015-10-22 14:00:28 -0400581 case gl::SAMPLER_PIXEL:
582 ASSERT(samplerIndex < caps.maxTextureImageUnits);
583 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
584 {
585 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
586 }
587 break;
588 case gl::SAMPLER_VERTEX:
589 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
590 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
591 {
592 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
593 }
594 break;
595 default:
596 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700597 }
598
Jamie Madill334d6152015-10-22 14:00:28 -0400599 if (logicalTextureUnit >= 0 &&
600 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700601 {
602 return logicalTextureUnit;
603 }
604
605 return -1;
606}
607
608// Returns the texture type for a given Direct3D 9 sampler type and
609// index (0-15 for the pixel shader and 0-3 for the vertex shader).
610GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
611{
612 switch (type)
613 {
Jamie Madill334d6152015-10-22 14:00:28 -0400614 case gl::SAMPLER_PIXEL:
615 ASSERT(samplerIndex < mSamplersPS.size());
616 ASSERT(mSamplersPS[samplerIndex].active);
617 return mSamplersPS[samplerIndex].textureType;
618 case gl::SAMPLER_VERTEX:
619 ASSERT(samplerIndex < mSamplersVS.size());
620 ASSERT(mSamplersVS[samplerIndex].active);
621 return mSamplersVS[samplerIndex].textureType;
622 default:
623 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700624 }
625
626 return GL_TEXTURE_2D;
627}
628
629GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
630{
631 switch (type)
632 {
Jamie Madill334d6152015-10-22 14:00:28 -0400633 case gl::SAMPLER_PIXEL:
634 return mUsedPixelSamplerRange;
635 case gl::SAMPLER_VERTEX:
636 return mUsedVertexSamplerRange;
637 default:
638 UNREACHABLE();
639 return 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700640 }
641}
642
643void ProgramD3D::updateSamplerMapping()
644{
645 if (!mDirtySamplerMapping)
646 {
647 return;
648 }
649
650 mDirtySamplerMapping = false;
651
652 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400653 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700654 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400655 if (!d3dUniform->dirty)
656 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700657
Jamie Madill62d31cb2015-09-11 13:25:51 -0400658 if (!d3dUniform->isSampler())
659 continue;
660
661 int count = d3dUniform->elementCount();
662 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
663
664 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700665 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400666 unsigned int firstIndex = d3dUniform->psRegisterIndex;
667
668 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700669 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400670 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700671
Jamie Madill62d31cb2015-09-11 13:25:51 -0400672 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700673 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400674 ASSERT(mSamplersPS[samplerIndex].active);
675 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700676 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400677 }
678 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700679
Jamie Madill62d31cb2015-09-11 13:25:51 -0400680 if (d3dUniform->isReferencedByVertexShader())
681 {
682 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
683
684 for (int i = 0; i < count; i++)
685 {
686 unsigned int samplerIndex = firstIndex + i;
687
688 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700689 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400690 ASSERT(mSamplersVS[samplerIndex].active);
691 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700692 }
693 }
694 }
695 }
696}
697
Geoff Lang7dd2e102014-11-10 15:19:26 -0500698LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700699{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400700 reset();
701
Jamie Madill334d6152015-10-22 14:00:28 -0400702 DeviceIdentifier binaryDeviceIdentifier = {0};
703 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
704 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700705
706 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
707 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
708 {
709 infoLog << "Invalid program binary, device configuration has changed.";
710 return LinkResult(false, gl::Error(GL_NO_ERROR));
711 }
712
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500713 int compileFlags = stream->readInt<int>();
714 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
715 {
Jamie Madillf6113162015-05-07 11:49:21 -0400716 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500717 return LinkResult(false, gl::Error(GL_NO_ERROR));
718 }
719
Jamie Madill63805b42015-08-25 13:17:39 -0400720 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
721 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
722 {
723 stream->readInt(&mSemanticIndexes[i]);
724 }
725
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700726 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
727 for (unsigned int i = 0; i < psSamplerCount; ++i)
728 {
729 Sampler sampler;
730 stream->readBool(&sampler.active);
731 stream->readInt(&sampler.logicalTextureUnit);
732 stream->readInt(&sampler.textureType);
733 mSamplersPS.push_back(sampler);
734 }
735 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
736 for (unsigned int i = 0; i < vsSamplerCount; ++i)
737 {
738 Sampler sampler;
739 stream->readBool(&sampler.active);
740 stream->readInt(&sampler.logicalTextureUnit);
741 stream->readInt(&sampler.textureType);
742 mSamplersVS.push_back(sampler);
743 }
744
745 stream->readInt(&mUsedVertexSamplerRange);
746 stream->readInt(&mUsedPixelSamplerRange);
747
748 const unsigned int uniformCount = stream->readInt<unsigned int>();
749 if (stream->error())
750 {
Jamie Madillf6113162015-05-07 11:49:21 -0400751 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500752 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700753 }
754
Jamie Madill62d31cb2015-09-11 13:25:51 -0400755 const auto &linkedUniforms = mData.getUniforms();
756 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700757 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
758 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400759 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700760
Jamie Madill62d31cb2015-09-11 13:25:51 -0400761 D3DUniform *d3dUniform =
762 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
763 linkedUniform.isInDefaultBlock());
764 stream->readInt(&d3dUniform->psRegisterIndex);
765 stream->readInt(&d3dUniform->vsRegisterIndex);
766 stream->readInt(&d3dUniform->registerCount);
767 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700768
Jamie Madill62d31cb2015-09-11 13:25:51 -0400769 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700770 }
771
Jamie Madill4a3c2342015-10-08 12:58:45 -0400772 const unsigned int blockCount = stream->readInt<unsigned int>();
773 if (stream->error())
774 {
775 infoLog << "Invalid program binary.";
776 return LinkResult(false, gl::Error(GL_NO_ERROR));
777 }
778
779 ASSERT(mD3DUniformBlocks.empty());
780 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
781 {
782 D3DUniformBlock uniformBlock;
783 stream->readInt(&uniformBlock.psRegisterIndex);
784 stream->readInt(&uniformBlock.vsRegisterIndex);
785 mD3DUniformBlocks.push_back(uniformBlock);
786 }
787
Jamie Madill9fc36822015-11-18 13:08:07 -0500788 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
789 mStreamOutVaryings.resize(streamOutVaryingCount);
790 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700791 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500792 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700793
Jamie Madill28afae52015-11-09 15:07:57 -0500794 stream->readString(&varying->semanticName);
795 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500796 stream->readInt(&varying->componentCount);
797 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700798 }
799
Brandon Jones22502d52014-08-29 16:58:36 -0700800 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400801 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
802 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700803 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400804 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
805 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700806 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700807 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400808 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700809
810 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
811 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400812 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
813 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700814 {
815 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
816 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
817 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
818 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
819 }
820
Jamie Madill4e31ad52015-10-29 10:32:57 -0400821 stream->readString(&mGeometryShaderPreamble);
822
Jamie Madill334d6152015-10-22 14:00:28 -0400823 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700824
825 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400826 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
827 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700828 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400829 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400830 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700831
Jamie Madilld3dfda22015-07-06 08:28:49 -0400832 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700833 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400834 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700835 }
836
Jamie Madill334d6152015-10-22 14:00:28 -0400837 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700838 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400839
Jamie Madillada9ecc2015-08-17 12:53:37 -0400840 ShaderExecutableD3D *shaderExecutable = nullptr;
841
842 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500843 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400844 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400845 if (error.isError())
846 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500847 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400848 }
849
Brandon Joneseb994362014-09-24 10:27:28 -0700850 if (!shaderExecutable)
851 {
Jamie Madillf6113162015-05-07 11:49:21 -0400852 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500853 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700854 }
855
856 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400857 VertexExecutable::Signature signature;
858 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700859
860 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400861 mVertexExecutables.push_back(
862 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700863
864 stream->skip(vertexShaderSize);
865 }
866
867 const size_t pixelShaderCount = stream->readInt<unsigned int>();
868 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
869 {
870 const size_t outputCount = stream->readInt<unsigned int>();
871 std::vector<GLenum> outputs(outputCount);
872 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
873 {
874 stream->readInt(&outputs[outputIndex]);
875 }
876
Jamie Madill334d6152015-10-22 14:00:28 -0400877 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700878 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400879 ShaderExecutableD3D *shaderExecutable = nullptr;
880
881 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500882 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400883 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400884 if (error.isError())
885 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500886 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400887 }
Brandon Joneseb994362014-09-24 10:27:28 -0700888
889 if (!shaderExecutable)
890 {
Jamie Madillf6113162015-05-07 11:49:21 -0400891 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500892 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700893 }
894
895 // add new binary
896 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
897
898 stream->skip(pixelShaderSize);
899 }
900
Jamie Madill4e31ad52015-10-29 10:32:57 -0400901 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
902 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700903 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400904 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
905 if (geometryShaderSize == 0)
906 {
907 mGeometryExecutables[geometryExeIndex] = nullptr;
908 continue;
909 }
910
Brandon Joneseb994362014-09-24 10:27:28 -0700911 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400912 bool splitAttribs = (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
913
Jamie Madill28afae52015-11-09 15:07:57 -0500914 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500915 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, mStreamOutVaryings,
916 splitAttribs, &mGeometryExecutables[geometryExeIndex]);
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
Jamie Madill4e31ad52015-10-29 10:32:57 -0400922 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700923 {
Jamie Madillf6113162015-05-07 11:49:21 -0400924 infoLog << "Could not create geometry 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 stream->skip(geometryShaderSize);
928 }
929
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700930 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500931 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700932
Geoff Lang7dd2e102014-11-10 15:19:26 -0500933 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700934}
935
Geoff Langb543aff2014-09-30 14:52:54 -0400936gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700937{
Austin Kinross137b1512015-06-17 16:14:53 -0700938 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400939 // When we load the binary again later, we can validate the device identifier before trying to
940 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700941 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400942 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
943 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700944
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500945 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
946
Jamie Madill63805b42015-08-25 13:17:39 -0400947 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
948 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
949 {
950 stream->writeInt(mSemanticIndexes[i]);
951 }
952
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700953 stream->writeInt(mSamplersPS.size());
954 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
955 {
956 stream->writeInt(mSamplersPS[i].active);
957 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
958 stream->writeInt(mSamplersPS[i].textureType);
959 }
960
961 stream->writeInt(mSamplersVS.size());
962 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
963 {
964 stream->writeInt(mSamplersVS[i].active);
965 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
966 stream->writeInt(mSamplersVS[i].textureType);
967 }
968
969 stream->writeInt(mUsedVertexSamplerRange);
970 stream->writeInt(mUsedPixelSamplerRange);
971
Jamie Madill62d31cb2015-09-11 13:25:51 -0400972 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -0400973 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700974 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400975 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madill4a3c2342015-10-08 12:58:45 -0400976 stream->writeInt(uniform->psRegisterIndex);
977 stream->writeInt(uniform->vsRegisterIndex);
978 stream->writeInt(uniform->registerCount);
979 stream->writeInt(uniform->registerElement);
980 }
981
982 stream->writeInt(mD3DUniformBlocks.size());
983 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
984 {
985 stream->writeInt(uniformBlock.psRegisterIndex);
986 stream->writeInt(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700987 }
988
Jamie Madill9fc36822015-11-18 13:08:07 -0500989 stream->writeInt(mStreamOutVaryings.size());
990 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -0700991 {
Brandon Joneseb994362014-09-24 10:27:28 -0700992 stream->writeString(varying.semanticName);
993 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500994 stream->writeInt(varying.componentCount);
995 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700996 }
997
Brandon Jones22502d52014-08-29 16:58:36 -0700998 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400999 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1000 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001001 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001002 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1003 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001004 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001005 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001006 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001007
Brandon Joneseb994362014-09-24 10:27:28 -07001008 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001009 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001010 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1011 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001012 {
Brandon Joneseb994362014-09-24 10:27:28 -07001013 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001014 stream->writeInt(variable.type);
1015 stream->writeString(variable.name);
1016 stream->writeString(variable.source);
1017 stream->writeInt(variable.outputIndex);
1018 }
1019
Jamie Madill4e31ad52015-10-29 10:32:57 -04001020 stream->writeString(mGeometryShaderPreamble);
1021
Brandon Joneseb994362014-09-24 10:27:28 -07001022 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001023 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1024 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001025 {
1026 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1027
Jamie Madilld3dfda22015-07-06 08:28:49 -04001028 const auto &inputLayout = vertexExecutable->inputs();
1029 stream->writeInt(inputLayout.size());
1030
1031 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001032 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001033 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -07001034 }
1035
1036 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1037 stream->writeInt(vertexShaderSize);
1038
1039 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1040 stream->writeBytes(vertexBlob, vertexShaderSize);
1041 }
1042
1043 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001044 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1045 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001046 {
1047 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1048
1049 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1050 stream->writeInt(outputs.size());
1051 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1052 {
1053 stream->writeInt(outputs[outputIndex]);
1054 }
1055
1056 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1057 stream->writeInt(pixelShaderSize);
1058
1059 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1060 stream->writeBytes(pixelBlob, pixelShaderSize);
1061 }
1062
Jamie Madill4e31ad52015-10-29 10:32:57 -04001063 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001064 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001065 if (geometryExe == nullptr)
1066 {
1067 stream->writeInt(0);
1068 continue;
1069 }
1070
1071 size_t geometryShaderSize = geometryExe->getLength();
1072 stream->writeInt(geometryShaderSize);
1073 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001074 }
1075
Geoff Langb543aff2014-09-30 14:52:54 -04001076 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001077}
1078
Jamie Madill334d6152015-10-22 14:00:28 -04001079gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1080 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001081{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001082 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001083
Jamie Madill85a18042015-03-05 15:41:41 -05001084 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill334d6152015-10-22 14:00:28 -04001085 const gl::AttachmentList &colorbuffers =
1086 fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -07001087
1088 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1089 {
1090 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1091
1092 if (colorbuffer)
1093 {
Jamie Madill334d6152015-10-22 14:00:28 -04001094 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1095 ? GL_COLOR_ATTACHMENT0
1096 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001097 }
1098 else
1099 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001100 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001101 }
1102 }
1103
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001104 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001105}
1106
Jamie Madill97399232014-12-23 12:31:15 -05001107gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001108 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001109 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001110{
1111 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1112 {
1113 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1114 {
Geoff Langb543aff2014-09-30 14:52:54 -04001115 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1116 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001117 }
1118 }
1119
Jamie Madill334d6152015-10-22 14:00:28 -04001120 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1121 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001122
1123 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001124 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001125
1126 gl::InfoLog tempInfoLog;
1127 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1128
Jamie Madillada9ecc2015-08-17 12:53:37 -04001129 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001130 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001131 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
1132 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001133 if (error.isError())
1134 {
1135 return error;
1136 }
Brandon Joneseb994362014-09-24 10:27:28 -07001137
Jamie Madill97399232014-12-23 12:31:15 -05001138 if (pixelExecutable)
1139 {
1140 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1141 }
1142 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001143 {
1144 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001145 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001146 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1147 }
Brandon Jones22502d52014-08-29 16:58:36 -07001148
Geoff Langb543aff2014-09-30 14:52:54 -04001149 *outExectuable = pixelExecutable;
1150 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001151}
1152
Jamie Madilld3dfda22015-07-06 08:28:49 -04001153gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001154 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001155 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001156{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001157 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001158
1159 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1160 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001161 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001162 {
Geoff Langb543aff2014-09-30 14:52:54 -04001163 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1164 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001165 }
1166 }
1167
Brandon Jones22502d52014-08-29 16:58:36 -07001168 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001169 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1170 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001171
1172 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001173 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001174
1175 gl::InfoLog tempInfoLog;
1176 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1177
Jamie Madillada9ecc2015-08-17 12:53:37 -04001178 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001179 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001180 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1181 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001182 if (error.isError())
1183 {
1184 return error;
1185 }
1186
Jamie Madill97399232014-12-23 12:31:15 -05001187 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001188 {
Jamie Madill334d6152015-10-22 14:00:28 -04001189 mVertexExecutables.push_back(
1190 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001191 }
Jamie Madill97399232014-12-23 12:31:15 -05001192 else if (!infoLog)
1193 {
1194 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001195 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001196 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1197 }
Brandon Jones22502d52014-08-29 16:58:36 -07001198
Geoff Langb543aff2014-09-30 14:52:54 -04001199 *outExectuable = vertexExecutable;
1200 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001201}
1202
Jamie Madill4e31ad52015-10-29 10:32:57 -04001203gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Data &data,
1204 GLenum drawMode,
1205 ShaderExecutableD3D **outExecutable,
1206 gl::InfoLog *infoLog)
1207{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001208 if (outExecutable)
1209 {
1210 *outExecutable = nullptr;
1211 }
1212
1213 // We only uses a geometry shader for point sprite emulation, or for fixing the provoking
1214 // vertex problem. Otherwise, return a null shader.
1215 if (drawMode != GL_POINTS && !mUsesFlatInterpolation)
1216 {
1217 return gl::Error(GL_NO_ERROR);
1218 }
1219
Jamie Madill4e31ad52015-10-29 10:32:57 -04001220 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1221
1222 if (mGeometryExecutables[geometryShaderType] != nullptr)
1223 {
1224 if (outExecutable)
1225 {
1226 *outExecutable = mGeometryExecutables[geometryShaderType];
1227 }
1228 return gl::Error(GL_NO_ERROR);
1229 }
1230
1231 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
1232 geometryShaderType, data, mData, mGeometryShaderPreamble);
1233
1234 gl::InfoLog tempInfoLog;
1235 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1236
1237 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001238 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001239 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
1240 &mGeometryExecutables[geometryShaderType]);
1241
1242 if (!infoLog && error.isError())
1243 {
1244 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1245 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1246 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1247 }
1248
1249 if (outExecutable)
1250 {
1251 *outExecutable = mGeometryExecutables[geometryShaderType];
1252 }
1253 return error;
1254}
1255
1256LinkResult ProgramD3D::compileProgramExecutables(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001257{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001258 const gl::InputLayout &defaultInputLayout =
1259 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001260 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001261 gl::Error error =
1262 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001263 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001264 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001265 return LinkResult(false, error);
1266 }
Austin Kinross434953e2015-02-20 10:49:51 -08001267
Jamie Madill334d6152015-10-22 14:00:28 -04001268 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001269 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001270 error =
1271 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001272 if (error.isError())
1273 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001274 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001275 }
Brandon Jones44151a92014-09-10 11:32:25 -07001276
Jamie Madill4e31ad52015-10-29 10:32:57 -04001277 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001278 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001279 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001280 getGeometryExecutableForPrimitiveType(data, GL_POINTS, nullptr, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001281 }
1282
Brandon Jones091540d2014-10-29 11:32:04 -07001283#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Jamie Madillca03b352015-09-02 12:38:13 -04001284 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001285 if (usesGeometryShader() && mGeometryExecutable)
1286 {
Jamie Madill334d6152015-10-22 14:00:28 -04001287 // Geometry shaders are currently only used internally, so there is no corresponding shader
1288 // object at the interface level. For now the geometry shader debug info is prepended to
1289 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001290 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1291 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1292 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1293 }
1294
1295 if (defaultVertexExecutable)
1296 {
1297 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1298 }
1299
1300 if (defaultPixelExecutable)
1301 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001302 const ShaderD3D *fragmentShaderD3D =
1303 GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001304 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1305 }
1306#endif
1307
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001308 bool linkSuccess =
1309 (defaultVertexExecutable && defaultPixelExecutable &&
1310 (!usesGeometryShader(GL_POINTS) || mGeometryExecutables[gl::PRIMITIVE_POINTS]));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001311 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001312}
1313
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001314LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001315{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001316 reset();
1317
1318 // TODO(jmadill): structures containing samplers
1319 for (const gl::LinkedUniform &linkedUniform : mData.getUniforms())
1320 {
1321 if (linkedUniform.isSampler() && linkedUniform.isField())
1322 {
1323 infoLog << "Structures containing samplers not currently supported in D3D.";
1324 return LinkResult(false, gl::Error(GL_NO_ERROR));
1325 }
1326 }
1327
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001328 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1329 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001330
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001331 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1332 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1333
Jamie Madill63069df2015-09-01 17:26:41 +00001334 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001335 mSamplersPS.resize(data.caps->maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001336
Arun Patole44efa0b2015-03-04 17:11:05 +05301337 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001338 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1339
Austin Kinross02df7962015-07-01 10:03:42 -07001340 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1341 {
1342 if (fragmentShaderD3D->usesFrontFacing())
1343 {
1344 infoLog << "The current renderer doesn't support gl_FrontFacing";
1345 return LinkResult(false, gl::Error(GL_NO_ERROR));
1346 }
1347 }
1348
Jamie Madillca03b352015-09-02 12:38:13 -04001349 std::vector<PackedVarying> packedVaryings =
1350 MergeVaryings(*vertexShader, *fragmentShader, mData.getTransformFeedbackVaryingNames());
1351
Brandon Jones22502d52014-08-29 16:58:36 -07001352 // Map the varyings to the register file
Jamie Madill9fc36822015-11-18 13:08:07 -05001353 VaryingPacking varyingPacking(data.caps->maxVaryingVectors);
1354 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
1355 mData.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001356 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001357 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001358 }
1359
Jamie Madille39a3f02015-11-17 20:42:15 -05001360 ProgramD3DMetadata metadata(mRenderer->getMajorShaderModel(), mRenderer->getShaderModelSuffix(),
1361 usesInstancedPointSpriteEmulation(), vertexShaderD3D,
1362 fragmentShaderD3D);
1363
Jamie Madill9fc36822015-11-18 13:08:07 -05001364 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1365 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1366
1367 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.caps->maxVaryingVectors)
1368 {
1369 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1370 return LinkResult(false, gl::Error(GL_NO_ERROR));
1371 }
1372
1373 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1374 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1375 // intelligently, but D3D9 assumes one semantic per register.
1376 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1377 varyingPacking.getMaxSemanticIndex() > data.caps->maxVaryingVectors)
1378 {
1379 infoLog << "Cannot pack these varyings on D3D9.";
1380 return LinkResult(false, gl::Error(GL_NO_ERROR));
1381 }
1382
1383 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mData, metadata, varyingPacking, &mPixelHLSL,
1384 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001385 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001386 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001387 }
1388
Brandon Jones44151a92014-09-10 11:32:25 -07001389 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madille39a3f02015-11-17 20:42:15 -05001390 mDynamicHLSL->getPixelShaderOutputKey(data, mData, metadata, &mPixelShaderKey);
1391 mUsesFragDepth = metadata.usesFragDepth(mData);
Brandon Jones44151a92014-09-10 11:32:25 -07001392
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001393 // Cache if we use flat shading
1394 for (const auto &varying : packedVaryings)
1395 {
1396 if (varying.varying->interpolation == sh::INTERPOLATION_FLAT)
1397 {
1398 mUsesFlatInterpolation = true;
1399 break;
1400 }
1401 }
1402
Jamie Madill4e31ad52015-10-29 10:32:57 -04001403 if (mRenderer->getMajorShaderModel() >= 4)
1404 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001405 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1406 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001407 }
1408
Jamie Madill63805b42015-08-25 13:17:39 -04001409 initSemanticIndex();
Jamie Madill437d2662014-12-05 14:23:35 -05001410
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001411 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001412
Jamie Madill9fc36822015-11-18 13:08:07 -05001413 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001414
Jamie Madill4e31ad52015-10-29 10:32:57 -04001415 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001416 if (result.error.isError() || !result.linkSuccess)
1417 {
1418 infoLog << "Failed to create D3D shaders.";
1419 return result;
1420 }
1421
Jamie Madill4a3c2342015-10-08 12:58:45 -04001422 initUniformBlockInfo();
1423
Geoff Lang7dd2e102014-11-10 15:19:26 -05001424 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001425}
1426
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001427GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001428{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001429 // TODO(jmadill): Do something useful here?
1430 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001431}
1432
Jamie Madill4a3c2342015-10-08 12:58:45 -04001433void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001434{
1435 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1436
Jamie Madill62d31cb2015-09-11 13:25:51 -04001437 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1438 {
1439 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1440 continue;
1441
Jamie Madill4a3c2342015-10-08 12:58:45 -04001442 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001443 continue;
1444
Jamie Madill4a3c2342015-10-08 12:58:45 -04001445 size_t dataSize = getUniformBlockInfo(vertexBlock);
1446 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001447 }
1448
1449 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1450
1451 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1452 {
1453 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1454 continue;
1455
Jamie Madill4a3c2342015-10-08 12:58:45 -04001456 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001457 continue;
1458
Jamie Madill4a3c2342015-10-08 12:58:45 -04001459 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1460 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001461 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001462}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001463
Jamie Madill4a3c2342015-10-08 12:58:45 -04001464void ProgramD3D::assignUniformBlockRegisters()
1465{
1466 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001467
1468 // Assign registers and update sizes.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001469 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1470 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001471
Jamie Madill4a3c2342015-10-08 12:58:45 -04001472 for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001473 {
1474 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1475
Jamie Madill4a3c2342015-10-08 12:58:45 -04001476 D3DUniformBlock d3dUniformBlock;
1477
Jamie Madill62d31cb2015-09-11 13:25:51 -04001478 if (uniformBlock.vertexStaticUse)
1479 {
1480 unsigned int baseRegister =
1481 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001482 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001483 }
1484
1485 if (uniformBlock.fragmentStaticUse)
1486 {
1487 unsigned int baseRegister =
1488 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001489 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001490 }
1491
Jamie Madill4a3c2342015-10-08 12:58:45 -04001492 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001493 }
1494}
1495
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001496void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001497{
1498 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001499 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001500 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001501 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001502 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001503 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001504 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001505 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001506 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001507 vertexRegisters = std::max(vertexRegisters,
1508 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001509 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001510 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001511 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001512 fragmentRegisters = std::max(
1513 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001514 }
1515 }
1516 }
1517
Jamie Madill334d6152015-10-22 14:00:28 -04001518 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001519 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1520}
1521
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001522gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001523{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001524 updateSamplerMapping();
1525
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001526 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001527 if (error.isError())
1528 {
1529 return error;
1530 }
1531
Jamie Madill62d31cb2015-09-11 13:25:51 -04001532 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001533 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001534 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001535 }
1536
1537 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001538}
1539
Jamie Madilld1fe1642015-08-21 16:26:04 -04001540gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001541{
Jamie Madill4a3c2342015-10-08 12:58:45 -04001542 if (mData.getUniformBlocks().empty())
1543 {
1544 return gl::Error(GL_NO_ERROR);
1545 }
1546
1547 // Lazy init.
1548 if (mD3DUniformBlocks.empty())
1549 {
1550 assignUniformBlockRegisters();
1551 }
1552
Jamie Madill03260fa2015-06-22 13:57:22 -04001553 mVertexUBOCache.clear();
1554 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001555
1556 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1557 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1558
Jamie Madill4a3c2342015-10-08 12:58:45 -04001559 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001560 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001561 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001562 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
1563 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001564
Brandon Jones18bd4102014-09-22 14:21:44 -07001565 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001566 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001567 {
1568 continue;
1569 }
1570
Jamie Madill4a3c2342015-10-08 12:58:45 -04001571 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001572 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001573 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001574 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001575
Jamie Madill969194d2015-07-20 14:36:56 -04001576 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001577 {
1578 mVertexUBOCache.resize(registerIndex + 1, -1);
1579 }
1580
1581 ASSERT(mVertexUBOCache[registerIndex] == -1);
1582 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001583 }
1584
Jamie Madill4a3c2342015-10-08 12:58:45 -04001585 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001586 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001587 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001588 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001589
1590 if (mFragmentUBOCache.size() <= registerIndex)
1591 {
1592 mFragmentUBOCache.resize(registerIndex + 1, -1);
1593 }
1594
1595 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1596 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001597 }
1598 }
1599
Jamie Madill03260fa2015-06-22 13:57:22 -04001600 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001601}
1602
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001603void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001604{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001605 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001606 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001607 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001608 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001609}
1610
Jamie Madill334d6152015-10-22 14:00:28 -04001611void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001612{
1613 setUniform(location, count, v, GL_FLOAT);
1614}
1615
1616void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1617{
1618 setUniform(location, count, v, GL_FLOAT_VEC2);
1619}
1620
1621void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1622{
1623 setUniform(location, count, v, GL_FLOAT_VEC3);
1624}
1625
1626void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1627{
1628 setUniform(location, count, v, GL_FLOAT_VEC4);
1629}
1630
Jamie Madill334d6152015-10-22 14:00:28 -04001631void ProgramD3D::setUniformMatrix2fv(GLint location,
1632 GLsizei count,
1633 GLboolean transpose,
1634 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001635{
1636 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1637}
1638
Jamie Madill334d6152015-10-22 14:00:28 -04001639void ProgramD3D::setUniformMatrix3fv(GLint location,
1640 GLsizei count,
1641 GLboolean transpose,
1642 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001643{
1644 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1645}
1646
Jamie Madill334d6152015-10-22 14:00:28 -04001647void ProgramD3D::setUniformMatrix4fv(GLint location,
1648 GLsizei count,
1649 GLboolean transpose,
1650 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001651{
1652 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1653}
1654
Jamie Madill334d6152015-10-22 14:00:28 -04001655void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1656 GLsizei count,
1657 GLboolean transpose,
1658 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001659{
1660 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1661}
1662
Jamie Madill334d6152015-10-22 14:00:28 -04001663void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1664 GLsizei count,
1665 GLboolean transpose,
1666 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001667{
1668 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1669}
1670
Jamie Madill334d6152015-10-22 14:00:28 -04001671void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1672 GLsizei count,
1673 GLboolean transpose,
1674 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001675{
1676 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1677}
1678
Jamie Madill334d6152015-10-22 14:00:28 -04001679void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1680 GLsizei count,
1681 GLboolean transpose,
1682 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001683{
1684 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1685}
1686
Jamie Madill334d6152015-10-22 14:00:28 -04001687void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1688 GLsizei count,
1689 GLboolean transpose,
1690 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001691{
1692 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1693}
1694
Jamie Madill334d6152015-10-22 14:00:28 -04001695void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1696 GLsizei count,
1697 GLboolean transpose,
1698 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001699{
1700 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1701}
1702
1703void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1704{
1705 setUniform(location, count, v, GL_INT);
1706}
1707
1708void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1709{
1710 setUniform(location, count, v, GL_INT_VEC2);
1711}
1712
1713void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1714{
1715 setUniform(location, count, v, GL_INT_VEC3);
1716}
1717
1718void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1719{
1720 setUniform(location, count, v, GL_INT_VEC4);
1721}
1722
1723void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1724{
1725 setUniform(location, count, v, GL_UNSIGNED_INT);
1726}
1727
1728void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1729{
1730 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1731}
1732
1733void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1734{
1735 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1736}
1737
1738void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1739{
1740 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1741}
1742
Jamie Madill4a3c2342015-10-08 12:58:45 -04001743void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1744 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001745{
1746}
1747
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001748void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001749{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001750 D3DUniformMap uniformMap;
Jamie Madill334d6152015-10-22 14:00:28 -04001751 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001752 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001753
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001754 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001755 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001756 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001757 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001758 }
1759 }
1760
Jamie Madill334d6152015-10-22 14:00:28 -04001761 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001762 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001763 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001764 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001765 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001766 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001767 }
1768 }
1769
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001770 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
1771 for (const gl::LinkedUniform &glUniform : mData.getUniforms())
1772 {
1773 if (!glUniform.isInDefaultBlock())
1774 continue;
1775
1776 auto mapEntry = uniformMap.find(glUniform.name);
1777 ASSERT(mapEntry != uniformMap.end());
1778 mD3DUniforms.push_back(mapEntry->second);
1779 }
1780
Jamie Madill62d31cb2015-09-11 13:25:51 -04001781 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001782 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001783}
1784
Jamie Madill91445bc2015-09-23 16:47:53 -04001785void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001786 const sh::Uniform &uniform,
1787 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001788{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001789 if (uniform.isBuiltIn())
Jamie Madillfb536032015-09-11 13:19:49 -04001790 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001791 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001792 return;
1793 }
1794
Jamie Madill91445bc2015-09-23 16:47:53 -04001795 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1796
1797 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1798 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001799 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001800 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001801
Jamie Madill91445bc2015-09-23 16:47:53 -04001802 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001803}
1804
Jamie Madill62d31cb2015-09-11 13:25:51 -04001805D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1806{
1807 for (D3DUniform *d3dUniform : mD3DUniforms)
1808 {
1809 if (d3dUniform->name == name)
1810 {
1811 return d3dUniform;
1812 }
1813 }
1814
1815 return nullptr;
1816}
1817
Jamie Madill91445bc2015-09-23 16:47:53 -04001818void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001819 const sh::ShaderVariable &uniform,
1820 const std::string &fullName,
1821 sh::HLSLBlockEncoder *encoder,
1822 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001823{
1824 if (uniform.isStruct())
1825 {
1826 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1827 {
1828 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1829
Jamie Madill55def582015-05-04 11:24:57 -04001830 if (encoder)
1831 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001832
1833 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1834 {
Jamie Madill334d6152015-10-22 14:00:28 -04001835 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001836 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1837
Jamie Madill91445bc2015-09-23 16:47:53 -04001838 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001839 }
1840
Jamie Madill55def582015-05-04 11:24:57 -04001841 if (encoder)
1842 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001843 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001844 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001845 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001846
1847 // Not a struct. Arrays are treated as aggregate types.
1848 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001849 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001850 encoder->enterAggregateType();
1851 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001852
Jamie Madill62d31cb2015-09-11 13:25:51 -04001853 // Advance the uniform offset, to track registers allocation for structs
1854 sh::BlockMemberInfo blockInfo =
1855 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1856 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001857
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001858 auto uniformMapEntry = uniformMap->find(fullName);
1859 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001860
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001861 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001862 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001863 d3dUniform = uniformMapEntry->second;
1864 }
1865 else
1866 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001867 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001868 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001869 }
1870
1871 if (encoder)
1872 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001873 d3dUniform->registerElement =
1874 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001875 unsigned int reg =
1876 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001877 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001878 {
1879 d3dUniform->psRegisterIndex = reg;
1880 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001881 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001882 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001883 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001884 d3dUniform->vsRegisterIndex = reg;
1885 }
Jamie Madillfb536032015-09-11 13:19:49 -04001886
1887 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001888 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001889 {
1890 encoder->exitAggregateType();
1891 }
1892 }
1893}
1894
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001895template <typename T>
Jamie Madill334d6152015-10-22 14:00:28 -04001896static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001897{
1898 ASSERT(dest != NULL);
1899 ASSERT(dirtyFlag != NULL);
1900
1901 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
Jamie Madill334d6152015-10-22 14:00:28 -04001902 *dest = source;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001903}
1904
1905template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001906void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001907{
Jamie Madill334d6152015-10-22 14:00:28 -04001908 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001909 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1910
Jamie Madill62d31cb2015-09-11 13:25:51 -04001911 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001912
Jamie Madill62d31cb2015-09-11 13:25:51 -04001913 unsigned int elementCount = targetUniform->elementCount();
1914 unsigned int arrayElement = mData.getUniformLocations()[location].element;
1915 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001916
1917 if (targetUniform->type == targetUniformType)
1918 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001919 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001920
Jamie Madill62d31cb2015-09-11 13:25:51 -04001921 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001922 {
Jamie Madill334d6152015-10-22 14:00:28 -04001923 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001924 const T *source = v + (i * components);
1925
1926 for (int c = 0; c < components; c++)
1927 {
1928 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1929 }
1930 for (int c = components; c < 4; c++)
1931 {
1932 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1933 }
1934 }
1935 }
1936 else if (targetUniform->type == targetBoolType)
1937 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001938 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001939
Jamie Madill62d31cb2015-09-11 13:25:51 -04001940 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001941 {
Jamie Madill334d6152015-10-22 14:00:28 -04001942 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001943 const T *source = v + (i * components);
1944
1945 for (int c = 0; c < components; c++)
1946 {
Jamie Madill334d6152015-10-22 14:00:28 -04001947 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1948 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001949 }
1950 for (int c = components; c < 4; c++)
1951 {
1952 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1953 }
1954 }
1955 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001956 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001957 {
1958 ASSERT(targetUniformType == GL_INT);
1959
Jamie Madill62d31cb2015-09-11 13:25:51 -04001960 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001961
1962 bool wasDirty = targetUniform->dirty;
1963
Jamie Madill62d31cb2015-09-11 13:25:51 -04001964 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001965 {
Jamie Madill334d6152015-10-22 14:00:28 -04001966 GLint *dest = target + (i * 4);
1967 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001968
1969 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1970 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1971 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1972 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1973 }
1974
1975 if (!wasDirty && targetUniform->dirty)
1976 {
1977 mDirtySamplerMapping = true;
1978 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001979 }
Jamie Madill334d6152015-10-22 14:00:28 -04001980 else
1981 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001982}
1983
1984template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001985void ProgramD3D::setUniformMatrixfv(GLint location,
1986 GLsizei countIn,
1987 GLboolean transpose,
1988 const GLfloat *value,
1989 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001990{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001991 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001992
Jamie Madill62d31cb2015-09-11 13:25:51 -04001993 unsigned int elementCount = targetUniform->elementCount();
1994 unsigned int arrayElement = mData.getUniformLocations()[location].element;
1995 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001996
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001997 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001998 GLfloat *target =
1999 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002000
Jamie Madill62d31cb2015-09-11 13:25:51 -04002001 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002002 {
2003 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2004 if (transpose == GL_FALSE)
2005 {
Jamie Madill334d6152015-10-22 14:00:28 -04002006 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2007 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002008 }
2009 else
2010 {
Jamie Madill334d6152015-10-22 14:00:28 -04002011 targetUniform->dirty =
2012 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002013 }
2014 target += targetMatrixStride;
2015 value += cols * rows;
2016 }
2017}
2018
Jamie Madill4a3c2342015-10-08 12:58:45 -04002019size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002020{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002021 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002022
Jamie Madill62d31cb2015-09-11 13:25:51 -04002023 // define member uniforms
2024 sh::Std140BlockEncoder std140Encoder;
2025 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2026 sh::BlockLayoutEncoder *encoder = nullptr;
2027
2028 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002029 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002030 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002031 }
2032 else
2033 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002034 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002035 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002036
2037 GetUniformBlockInfo(interfaceBlock.fields, "", encoder, interfaceBlock.isRowMajorLayout,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002038 &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002039
2040 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002041}
2042
Jamie Madill62d31cb2015-09-11 13:25:51 -04002043void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002044{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002045 for (const D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002046 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002047 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002048 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002049 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002050 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002051 }
2052}
2053
Jamie Madill62d31cb2015-09-11 13:25:51 -04002054void ProgramD3D::assignSamplerRegisters(const D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002055{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002056 ASSERT(d3dUniform->isSampler());
2057 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX ||
2058 d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madillfb536032015-09-11 13:19:49 -04002059
Jamie Madill62d31cb2015-09-11 13:25:51 -04002060 if (d3dUniform->vsRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002061 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002062 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2063 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002064 }
2065
Jamie Madill62d31cb2015-09-11 13:25:51 -04002066 if (d3dUniform->psRegisterIndex != GL_INVALID_INDEX)
Jamie Madillfb536032015-09-11 13:19:49 -04002067 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002068 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2069 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002070 }
2071}
2072
Jamie Madill62d31cb2015-09-11 13:25:51 -04002073// static
2074void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002075 GLenum samplerType,
2076 unsigned int samplerCount,
2077 std::vector<Sampler> &outSamplers,
2078 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002079{
2080 unsigned int samplerIndex = startSamplerIndex;
2081
2082 do
2083 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002084 ASSERT(samplerIndex < outSamplers.size());
2085 Sampler *sampler = &outSamplers[samplerIndex];
2086 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002087 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002088 sampler->logicalTextureUnit = 0;
2089 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002090 samplerIndex++;
2091 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002092}
2093
Brandon Jonesc9610c52014-08-25 17:02:59 -07002094void ProgramD3D::reset()
2095{
Brandon Joneseb994362014-09-24 10:27:28 -07002096 SafeDeleteContainer(mVertexExecutables);
2097 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002098
2099 for (auto &element : mGeometryExecutables)
2100 {
2101 SafeDelete(element);
2102 }
Brandon Joneseb994362014-09-24 10:27:28 -07002103
Brandon Jones22502d52014-08-29 16:58:36 -07002104 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002105 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002106
2107 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002108 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002109 mUsesFragDepth = false;
2110 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002111 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002112 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002113
Jamie Madill62d31cb2015-09-11 13:25:51 -04002114 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002115 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002116
Brandon Jonesc9610c52014-08-25 17:02:59 -07002117 SafeDelete(mVertexUniformStorage);
2118 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002119
2120 mSamplersPS.clear();
2121 mSamplersVS.clear();
2122
2123 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002124 mUsedPixelSamplerRange = 0;
2125 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002126
Jamie Madill63805b42015-08-25 13:17:39 -04002127 std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1);
Jamie Madill437d2662014-12-05 14:23:35 -05002128 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002129
Jamie Madill9fc36822015-11-18 13:08:07 -05002130 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002131
2132 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002133}
2134
Geoff Lang7dd2e102014-11-10 15:19:26 -05002135unsigned int ProgramD3D::getSerial() const
2136{
2137 return mSerial;
2138}
2139
2140unsigned int ProgramD3D::issueSerial()
2141{
2142 return mCurrentSerial++;
2143}
2144
Jamie Madill63805b42015-08-25 13:17:39 -04002145void ProgramD3D::initSemanticIndex()
2146{
2147 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2148 ASSERT(vertexShader != nullptr);
2149
2150 // Init semantic index
2151 for (const sh::Attribute &attribute : mData.getAttributes())
2152 {
2153 int attributeIndex = attribute.location;
2154 int index = vertexShader->getSemanticIndex(attribute.name);
2155 int regs = gl::VariableRegisterCount(attribute.type);
2156
2157 for (int reg = 0; reg < regs; ++reg)
2158 {
2159 mSemanticIndexes[attributeIndex + reg] = index + reg;
2160 }
2161 }
2162
2163 initAttributesByLayout();
2164}
2165
Jamie Madill437d2662014-12-05 14:23:35 -05002166void ProgramD3D::initAttributesByLayout()
2167{
2168 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2169 {
2170 mAttributesByLayout[i] = i;
2171 }
2172
Jamie Madill63805b42015-08-25 13:17:39 -04002173 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS],
2174 AttributeSorter(mSemanticIndexes));
Jamie Madill437d2662014-12-05 14:23:35 -05002175}
2176
Jamie Madill334d6152015-10-22 14:00:28 -04002177void ProgramD3D::sortAttributesByLayout(
2178 const std::vector<TranslatedAttribute> &unsortedAttributes,
2179 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2180 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002181{
Jamie Madill476682e2015-06-30 10:04:29 -04002182 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002183 {
Jamie Madill334d6152015-10-22 14:00:28 -04002184 int oldIndex = mAttributesByLayout[attribIndex];
Jamie Madill63805b42015-08-25 13:17:39 -04002185 sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex];
Jamie Madill334d6152015-10-22 14:00:28 -04002186 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002187 }
2188}
2189
Jamie Madill63805b42015-08-25 13:17:39 -04002190void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002191{
Jamie Madillbd136f92015-08-10 14:51:37 -04002192 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002193 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002194
Dian Xianga4928832015-09-15 10:11:17 -07002195 for (unsigned int attributeIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002196 {
Jamie Madill63805b42015-08-25 13:17:39 -04002197 int semanticIndex = mSemanticIndexes[attributeIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002198
2199 if (semanticIndex != -1)
2200 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002201 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2202 {
2203 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2204 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002205 mCachedInputLayout[semanticIndex] =
2206 GetVertexFormatType(vertexAttributes[attributeIndex],
2207 state.getVertexAttribCurrentValue(attributeIndex).Type);
2208 }
2209 }
2210}
2211
Jamie Madill9fc36822015-11-18 13:08:07 -05002212void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002213{
Jamie Madill9fc36822015-11-18 13:08:07 -05002214 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2215
2216 const std::string &varyingSemantic =
2217 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2218
Jamie Madillccdf74b2015-08-18 10:46:12 -04002219 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002220 mStreamOutVaryings.clear();
2221
2222 const auto &tfVaryingNames = mData.getTransformFeedbackVaryingNames();
2223 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2224 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002225 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002226 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2227 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002228 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002229 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002230 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002231 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2232 builtins.glPosition.index, 4, outputSlot));
2233 }
2234 }
2235 else if (tfVaryingName == "gl_FragCoord")
2236 {
2237 if (builtins.glFragCoord.enabled)
2238 {
2239 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2240 builtins.glFragCoord.index, 4, outputSlot));
2241 }
2242 }
2243 else if (tfVaryingName == "gl_PointSize")
2244 {
2245 if (builtins.glPointSize.enabled)
2246 {
2247 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2248 }
2249 }
2250 else
2251 {
2252 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2253 {
2254 const sh::Varying &varying = *registerInfo.packedVarying->varying;
2255 GLenum transposedType = gl::TransposeMatrixType(varying.type);
2256 int componentCount = gl::VariableColumnCount(transposedType);
2257 ASSERT(!varying.isBuiltIn());
2258
2259 // There can be more than one register assigned to a particular varying, and each
2260 // register needs its own stream out entry.
2261 if (tfVaryingName == varying.name)
2262 {
2263 mStreamOutVaryings.push_back(D3DVarying(
2264 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2265 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002266 }
2267 }
2268 }
2269}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002270
2271D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2272{
2273 return mD3DUniforms[mData.getUniformLocations()[location].index];
2274}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002275
2276bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2277{
2278 std::string baseName = blockName;
2279 gl::ParseAndStripArrayIndex(&baseName);
2280
2281 auto sizeIter = mBlockDataSizes.find(baseName);
2282 if (sizeIter == mBlockDataSizes.end())
2283 {
2284 *sizeOut = 0;
2285 return false;
2286 }
2287
2288 *sizeOut = sizeIter->second;
2289 return true;
2290}
2291
2292bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2293 sh::BlockMemberInfo *memberInfoOut) const
2294{
2295 auto infoIter = mBlockInfo.find(memberUniformName);
2296 if (infoIter == mBlockInfo.end())
2297 {
2298 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2299 return false;
2300 }
2301
2302 *memberInfoOut = infoIter->second;
2303 return true;
2304}
Brandon Jonesc9610c52014-08-25 17:02:59 -07002305}