blob: 221d5d9b56ab805ce54bfbf06747a43fcebacc57 [file] [log] [blame]
Jamie Madill033dae62014-06-18 12:56:28 -04001//
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// UtilsHLSL.cpp:
7// Utility methods for GLSL to HLSL translation.
8//
9
10#include "compiler/translator/UtilsHLSL.h"
Olli Etuahof5cfc8d2015-08-06 16:36:39 +030011#include "compiler/translator/IntermNode.h"
Jamie Madill8daaba12014-06-13 10:04:33 -040012#include "compiler/translator/StructureHLSL.h"
Jamie Madill033dae62014-06-18 12:56:28 -040013#include "compiler/translator/SymbolTable.h"
14
15namespace sh
16{
17
Olli Etuaho9b4e8622015-12-22 15:53:22 +020018TString SamplerString(const TBasicType type)
Jamie Madill033dae62014-06-18 12:56:28 -040019{
Olli Etuaho9b4e8622015-12-22 15:53:22 +020020 if (IsShadowSampler(type))
Jamie Madill033dae62014-06-18 12:56:28 -040021 {
22 return "SamplerComparisonState";
23 }
24 else
25 {
26 return "SamplerState";
27 }
28}
29
Olli Etuaho9b4e8622015-12-22 15:53:22 +020030TString SamplerString(HLSLTextureSamplerGroup type)
Jamie Madill033dae62014-06-18 12:56:28 -040031{
Olli Etuaho9b4e8622015-12-22 15:53:22 +020032 if (type >= HLSL_COMPARISON_SAMPLER_GROUP_BEGIN && type <= HLSL_COMPARISON_SAMPLER_GROUP_END)
Jamie Madill033dae62014-06-18 12:56:28 -040033 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +020034 return "SamplerComparisonState";
35 }
36 else
37 {
38 return "SamplerState";
39 }
40}
41
42HLSLTextureSamplerGroup TextureGroup(const TBasicType type)
43{
44 switch (type)
45 {
46 case EbtSampler2D:
47 return HLSL_TEXTURE_2D;
48 case EbtSamplerCube:
49 return HLSL_TEXTURE_CUBE;
50 case EbtSamplerExternalOES:
51 return HLSL_TEXTURE_2D;
52 case EbtSampler2DArray:
53 return HLSL_TEXTURE_2D_ARRAY;
54 case EbtSampler3D:
55 return HLSL_TEXTURE_3D;
56 case EbtISampler2D:
57 return HLSL_TEXTURE_2D_INT4;
58 case EbtISampler3D:
59 return HLSL_TEXTURE_3D_INT4;
60 case EbtISamplerCube:
61 return HLSL_TEXTURE_2D_ARRAY_INT4;
62 case EbtISampler2DArray:
63 return HLSL_TEXTURE_2D_ARRAY_INT4;
64 case EbtUSampler2D:
65 return HLSL_TEXTURE_2D_UINT4;
66 case EbtUSampler3D:
67 return HLSL_TEXTURE_3D_UINT4;
68 case EbtUSamplerCube:
69 return HLSL_TEXTURE_2D_ARRAY_UINT4;
70 case EbtUSampler2DArray:
71 return HLSL_TEXTURE_2D_ARRAY_UINT4;
72 case EbtSampler2DShadow:
73 return HLSL_TEXTURE_2D_COMPARISON;
74 case EbtSamplerCubeShadow:
75 return HLSL_TEXTURE_CUBE_COMPARISON;
76 case EbtSampler2DArrayShadow:
77 return HLSL_TEXTURE_2D_ARRAY_COMPARISON;
78 default:
79 UNREACHABLE();
80 }
81 return HLSL_TEXTURE_UNKNOWN;
82}
83
84TString TextureString(const HLSLTextureSamplerGroup type)
85{
86 switch (type)
87 {
88 case HLSL_TEXTURE_2D:
89 return "Texture2D";
90 case HLSL_TEXTURE_CUBE:
91 return "TextureCube";
92 case HLSL_TEXTURE_2D_ARRAY:
93 return "Texture2DArray";
94 case HLSL_TEXTURE_3D:
95 return "Texture3D";
96 case HLSL_TEXTURE_2D_INT4:
97 return "Texture2D<int4>";
98 case HLSL_TEXTURE_3D_INT4:
99 return "Texture3D<int4>";
100 case HLSL_TEXTURE_2D_ARRAY_INT4:
101 return "Texture2DArray<int4>";
102 case HLSL_TEXTURE_2D_UINT4:
103 return "Texture2D<uint4>";
104 case HLSL_TEXTURE_3D_UINT4:
105 return "Texture3D<uint4>";
106 case HLSL_TEXTURE_2D_ARRAY_UINT4:
107 return "Texture2DArray<uint4>";
108 case HLSL_TEXTURE_2D_COMPARISON:
109 return "Texture2D";
110 case HLSL_TEXTURE_CUBE_COMPARISON:
111 return "TextureCube";
112 case HLSL_TEXTURE_2D_ARRAY_COMPARISON:
113 return "Texture2DArray";
114 default:
115 UNREACHABLE();
Jamie Madill033dae62014-06-18 12:56:28 -0400116 }
117
118 return "<unknown texture type>";
119}
120
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200121TString TextureString(const TBasicType type)
122{
123 return TextureString(TextureGroup(type));
124}
125
126TString TextureGroupSuffix(const HLSLTextureSamplerGroup type)
127{
128 switch (type)
129 {
130 case HLSL_TEXTURE_2D:
131 return "2D";
132 case HLSL_TEXTURE_CUBE:
133 return "Cube";
134 case HLSL_TEXTURE_2D_ARRAY:
135 return "2DArray";
136 case HLSL_TEXTURE_3D:
137 return "3D";
138 case HLSL_TEXTURE_2D_INT4:
139 return "2D_int4_";
140 case HLSL_TEXTURE_3D_INT4:
141 return "3D_int4_";
142 case HLSL_TEXTURE_2D_ARRAY_INT4:
143 return "2DArray_int4_";
144 case HLSL_TEXTURE_2D_UINT4:
145 return "2D_uint4_";
146 case HLSL_TEXTURE_3D_UINT4:
147 return "3D_uint4_";
148 case HLSL_TEXTURE_2D_ARRAY_UINT4:
149 return "2DArray_uint4_";
150 case HLSL_TEXTURE_2D_COMPARISON:
151 return "2D_comparison";
152 case HLSL_TEXTURE_CUBE_COMPARISON:
153 return "Cube_comparison";
154 case HLSL_TEXTURE_2D_ARRAY_COMPARISON:
155 return "2DArray_comparison";
156 default:
157 UNREACHABLE();
158 }
159
160 return "<unknown texture type>";
161}
162
163TString TextureGroupSuffix(const TBasicType type)
164{
165 return TextureGroupSuffix(TextureGroup(type));
166}
167
168TString TextureTypeSuffix(const TBasicType type)
169{
170 switch (type)
171 {
172 case EbtISamplerCube:
173 return "Cube_int4_";
174 case EbtUSamplerCube:
175 return "Cube_uint4_";
Geoff Lang28a97ee2016-09-22 13:01:26 -0400176 case EbtSamplerExternalOES:
177 return "_External";
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200178 default:
179 // All other types are identified by their group suffix
180 return TextureGroupSuffix(type);
181 }
182}
183
Olli Etuaho96963162016-03-21 11:54:33 +0200184TString DecorateUniform(const TName &name, const TType &type)
Jamie Madill033dae62014-06-18 12:56:28 -0400185{
Olli Etuaho96963162016-03-21 11:54:33 +0200186 return DecorateIfNeeded(name);
Jamie Madill033dae62014-06-18 12:56:28 -0400187}
188
189TString DecorateField(const TString &string, const TStructure &structure)
190{
191 if (structure.name().compare(0, 3, "gl_") != 0)
192 {
193 return Decorate(string);
194 }
195
196 return string;
197}
198
199TString DecoratePrivate(const TString &privateText)
200{
201 return "dx_" + privateText;
202}
203
204TString Decorate(const TString &string)
205{
Jamie Madilld5512cd2014-07-10 17:50:08 -0400206 if (string.compare(0, 3, "gl_") != 0)
Jamie Madill033dae62014-06-18 12:56:28 -0400207 {
208 return "_" + string;
209 }
210
211 return string;
212}
213
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300214TString DecorateIfNeeded(const TName &name)
215{
216 if (name.isInternal())
217 {
218 return name.getString();
219 }
220 else
221 {
222 return Decorate(name.getString());
223 }
224}
225
Olli Etuaho59f9a642015-08-06 20:38:26 +0300226TString DecorateFunctionIfNeeded(const TName &name)
227{
228 if (name.isInternal())
229 {
230 return TFunction::unmangleName(name.getString());
231 }
232 else
233 {
234 return Decorate(TFunction::unmangleName(name.getString()));
235 }
236}
237
Jamie Madill033dae62014-06-18 12:56:28 -0400238TString TypeString(const TType &type)
239{
240 const TStructure* structure = type.getStruct();
241 if (structure)
242 {
243 const TString& typeName = structure->name();
244 if (typeName != "")
245 {
246 return StructNameString(*structure);
247 }
248 else // Nameless structure, define in place
249 {
Jamie Madill8daaba12014-06-13 10:04:33 -0400250 return StructureHLSL::defineNameless(*structure);
Jamie Madill033dae62014-06-18 12:56:28 -0400251 }
252 }
253 else if (type.isMatrix())
254 {
255 int cols = type.getCols();
256 int rows = type.getRows();
257 return "float" + str(cols) + "x" + str(rows);
258 }
259 else
260 {
261 switch (type.getBasicType())
262 {
263 case EbtFloat:
264 switch (type.getNominalSize())
265 {
266 case 1: return "float";
267 case 2: return "float2";
268 case 3: return "float3";
269 case 4: return "float4";
270 }
271 case EbtInt:
272 switch (type.getNominalSize())
273 {
274 case 1: return "int";
275 case 2: return "int2";
276 case 3: return "int3";
277 case 4: return "int4";
278 }
279 case EbtUInt:
280 switch (type.getNominalSize())
281 {
282 case 1: return "uint";
283 case 2: return "uint2";
284 case 3: return "uint3";
285 case 4: return "uint4";
286 }
287 case EbtBool:
288 switch (type.getNominalSize())
289 {
290 case 1: return "bool";
291 case 2: return "bool2";
292 case 3: return "bool3";
293 case 4: return "bool4";
294 }
295 case EbtVoid:
296 return "void";
297 case EbtSampler2D:
298 case EbtISampler2D:
299 case EbtUSampler2D:
300 case EbtSampler2DArray:
301 case EbtISampler2DArray:
302 case EbtUSampler2DArray:
303 return "sampler2D";
304 case EbtSamplerCube:
305 case EbtISamplerCube:
306 case EbtUSamplerCube:
307 return "samplerCUBE";
308 case EbtSamplerExternalOES:
309 return "sampler2D";
310 default:
311 break;
312 }
313 }
314
315 UNREACHABLE();
316 return "<unknown type>";
317}
318
319TString StructNameString(const TStructure &structure)
320{
321 if (structure.name().empty())
322 {
323 return "";
324 }
325
Jamie Madill9b820842015-02-12 10:40:10 -0500326 // For structures at global scope we use a consistent
327 // translation so that we can link between shader stages.
328 if (structure.atGlobalScope())
329 {
330 return Decorate(structure.name());
331 }
332
Jamie Madill8daaba12014-06-13 10:04:33 -0400333 return "ss" + str(structure.uniqueId()) + "_" + structure.name();
Jamie Madill033dae62014-06-18 12:56:28 -0400334}
335
336TString QualifiedStructNameString(const TStructure &structure, bool useHLSLRowMajorPacking,
337 bool useStd140Packing)
338{
339 if (structure.name() == "")
340 {
341 return "";
342 }
343
344 TString prefix = "";
345
346 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
347 // GLSL column-major maps to HLSL row-major, and the converse is true
348
349 if (useStd140Packing)
350 {
Jamie Madill8daaba12014-06-13 10:04:33 -0400351 prefix += "std_";
Jamie Madill033dae62014-06-18 12:56:28 -0400352 }
353
354 if (useHLSLRowMajorPacking)
355 {
Jamie Madill8daaba12014-06-13 10:04:33 -0400356 prefix += "rm_";
Jamie Madill033dae62014-06-18 12:56:28 -0400357 }
358
359 return prefix + StructNameString(structure);
360}
361
362TString InterpolationString(TQualifier qualifier)
363{
364 switch (qualifier)
365 {
366 case EvqVaryingIn: return "";
367 case EvqFragmentIn: return "";
Jamie Madill033dae62014-06-18 12:56:28 -0400368 case EvqSmoothIn: return "linear";
369 case EvqFlatIn: return "nointerpolation";
370 case EvqCentroidIn: return "centroid";
371 case EvqVaryingOut: return "";
372 case EvqVertexOut: return "";
Jamie Madill033dae62014-06-18 12:56:28 -0400373 case EvqSmoothOut: return "linear";
374 case EvqFlatOut: return "nointerpolation";
375 case EvqCentroidOut: return "centroid";
376 default: UNREACHABLE();
377 }
378
379 return "";
380}
381
382TString QualifierString(TQualifier qualifier)
383{
384 switch (qualifier)
385 {
386 case EvqIn: return "in";
387 case EvqOut: return "inout"; // 'out' results in an HLSL error if not all fields are written, for GLSL it's undefined
388 case EvqInOut: return "inout";
389 case EvqConstReadOnly: return "const";
390 default: UNREACHABLE();
391 }
392
393 return "";
394}
395
Olli Etuahobe59c2f2016-03-07 11:32:34 +0200396TString DisambiguateFunctionName(const TIntermSequence *parameters)
397{
398 TString disambiguatingString;
399 for (auto parameter : *parameters)
400 {
401 const TType &paramType = parameter->getAsTyped()->getType();
402 // Disambiguation is needed for float2x2 and float4 parameters. These are the only parameter
403 // types that HLSL thinks are identical. float2x3 and float3x2 are different types, for
404 // example. Other parameter types are not added to function names to avoid making function
405 // names longer.
406 if (paramType.getObjectSize() == 4 && paramType.getBasicType() == EbtFloat)
407 {
408 disambiguatingString += "_" + TypeString(paramType);
409 }
410 }
411 return disambiguatingString;
Jamie Madill033dae62014-06-18 12:56:28 -0400412}
Olli Etuahobe59c2f2016-03-07 11:32:34 +0200413
414} // namespace sh