blob: b3a065f19f808f034ab6c48a4da4d2c5e7df1723 [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_";
176 default:
177 // All other types are identified by their group suffix
178 return TextureGroupSuffix(type);
179 }
180}
181
Olli Etuaho96963162016-03-21 11:54:33 +0200182TString DecorateUniform(const TName &name, const TType &type)
Jamie Madill033dae62014-06-18 12:56:28 -0400183{
Olli Etuaho96963162016-03-21 11:54:33 +0200184 return DecorateIfNeeded(name);
Jamie Madill033dae62014-06-18 12:56:28 -0400185}
186
187TString DecorateField(const TString &string, const TStructure &structure)
188{
189 if (structure.name().compare(0, 3, "gl_") != 0)
190 {
191 return Decorate(string);
192 }
193
194 return string;
195}
196
197TString DecoratePrivate(const TString &privateText)
198{
199 return "dx_" + privateText;
200}
201
202TString Decorate(const TString &string)
203{
Jamie Madilld5512cd2014-07-10 17:50:08 -0400204 if (string.compare(0, 3, "gl_") != 0)
Jamie Madill033dae62014-06-18 12:56:28 -0400205 {
206 return "_" + string;
207 }
208
209 return string;
210}
211
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300212TString DecorateIfNeeded(const TName &name)
213{
214 if (name.isInternal())
215 {
216 return name.getString();
217 }
218 else
219 {
220 return Decorate(name.getString());
221 }
222}
223
Olli Etuaho59f9a642015-08-06 20:38:26 +0300224TString DecorateFunctionIfNeeded(const TName &name)
225{
226 if (name.isInternal())
227 {
228 return TFunction::unmangleName(name.getString());
229 }
230 else
231 {
232 return Decorate(TFunction::unmangleName(name.getString()));
233 }
234}
235
Jamie Madill033dae62014-06-18 12:56:28 -0400236TString TypeString(const TType &type)
237{
238 const TStructure* structure = type.getStruct();
239 if (structure)
240 {
241 const TString& typeName = structure->name();
242 if (typeName != "")
243 {
244 return StructNameString(*structure);
245 }
246 else // Nameless structure, define in place
247 {
Jamie Madill8daaba12014-06-13 10:04:33 -0400248 return StructureHLSL::defineNameless(*structure);
Jamie Madill033dae62014-06-18 12:56:28 -0400249 }
250 }
251 else if (type.isMatrix())
252 {
253 int cols = type.getCols();
254 int rows = type.getRows();
255 return "float" + str(cols) + "x" + str(rows);
256 }
257 else
258 {
259 switch (type.getBasicType())
260 {
261 case EbtFloat:
262 switch (type.getNominalSize())
263 {
264 case 1: return "float";
265 case 2: return "float2";
266 case 3: return "float3";
267 case 4: return "float4";
268 }
269 case EbtInt:
270 switch (type.getNominalSize())
271 {
272 case 1: return "int";
273 case 2: return "int2";
274 case 3: return "int3";
275 case 4: return "int4";
276 }
277 case EbtUInt:
278 switch (type.getNominalSize())
279 {
280 case 1: return "uint";
281 case 2: return "uint2";
282 case 3: return "uint3";
283 case 4: return "uint4";
284 }
285 case EbtBool:
286 switch (type.getNominalSize())
287 {
288 case 1: return "bool";
289 case 2: return "bool2";
290 case 3: return "bool3";
291 case 4: return "bool4";
292 }
293 case EbtVoid:
294 return "void";
295 case EbtSampler2D:
296 case EbtISampler2D:
297 case EbtUSampler2D:
298 case EbtSampler2DArray:
299 case EbtISampler2DArray:
300 case EbtUSampler2DArray:
301 return "sampler2D";
302 case EbtSamplerCube:
303 case EbtISamplerCube:
304 case EbtUSamplerCube:
305 return "samplerCUBE";
306 case EbtSamplerExternalOES:
307 return "sampler2D";
308 default:
309 break;
310 }
311 }
312
313 UNREACHABLE();
314 return "<unknown type>";
315}
316
317TString StructNameString(const TStructure &structure)
318{
319 if (structure.name().empty())
320 {
321 return "";
322 }
323
Jamie Madill9b820842015-02-12 10:40:10 -0500324 // For structures at global scope we use a consistent
325 // translation so that we can link between shader stages.
326 if (structure.atGlobalScope())
327 {
328 return Decorate(structure.name());
329 }
330
Jamie Madill8daaba12014-06-13 10:04:33 -0400331 return "ss" + str(structure.uniqueId()) + "_" + structure.name();
Jamie Madill033dae62014-06-18 12:56:28 -0400332}
333
334TString QualifiedStructNameString(const TStructure &structure, bool useHLSLRowMajorPacking,
335 bool useStd140Packing)
336{
337 if (structure.name() == "")
338 {
339 return "";
340 }
341
342 TString prefix = "";
343
344 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
345 // GLSL column-major maps to HLSL row-major, and the converse is true
346
347 if (useStd140Packing)
348 {
Jamie Madill8daaba12014-06-13 10:04:33 -0400349 prefix += "std_";
Jamie Madill033dae62014-06-18 12:56:28 -0400350 }
351
352 if (useHLSLRowMajorPacking)
353 {
Jamie Madill8daaba12014-06-13 10:04:33 -0400354 prefix += "rm_";
Jamie Madill033dae62014-06-18 12:56:28 -0400355 }
356
357 return prefix + StructNameString(structure);
358}
359
360TString InterpolationString(TQualifier qualifier)
361{
362 switch (qualifier)
363 {
364 case EvqVaryingIn: return "";
365 case EvqFragmentIn: return "";
Jamie Madill033dae62014-06-18 12:56:28 -0400366 case EvqSmoothIn: return "linear";
367 case EvqFlatIn: return "nointerpolation";
368 case EvqCentroidIn: return "centroid";
369 case EvqVaryingOut: return "";
370 case EvqVertexOut: return "";
Jamie Madill033dae62014-06-18 12:56:28 -0400371 case EvqSmoothOut: return "linear";
372 case EvqFlatOut: return "nointerpolation";
373 case EvqCentroidOut: return "centroid";
374 default: UNREACHABLE();
375 }
376
377 return "";
378}
379
380TString QualifierString(TQualifier qualifier)
381{
382 switch (qualifier)
383 {
384 case EvqIn: return "in";
385 case EvqOut: return "inout"; // 'out' results in an HLSL error if not all fields are written, for GLSL it's undefined
386 case EvqInOut: return "inout";
387 case EvqConstReadOnly: return "const";
388 default: UNREACHABLE();
389 }
390
391 return "";
392}
393
Olli Etuahobe59c2f2016-03-07 11:32:34 +0200394TString DisambiguateFunctionName(const TIntermSequence *parameters)
395{
396 TString disambiguatingString;
397 for (auto parameter : *parameters)
398 {
399 const TType &paramType = parameter->getAsTyped()->getType();
400 // Disambiguation is needed for float2x2 and float4 parameters. These are the only parameter
401 // types that HLSL thinks are identical. float2x3 and float3x2 are different types, for
402 // example. Other parameter types are not added to function names to avoid making function
403 // names longer.
404 if (paramType.getObjectSize() == 4 && paramType.getBasicType() == EbtFloat)
405 {
406 disambiguatingString += "_" + TypeString(paramType);
407 }
408 }
409 return disambiguatingString;
Jamie Madill033dae62014-06-18 12:56:28 -0400410}
Olli Etuahobe59c2f2016-03-07 11:32:34 +0200411
412} // namespace sh