17ec681f3Smrg/*
27ec681f3Smrg * Copyright © Microsoft Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include "dxil_enums.h"
257ec681f3Smrg
267ec681f3Smrg#include "nir.h"
277ec681f3Smrg#include "nir_types.h"
287ec681f3Smrg
297ec681f3Smrg#include "util/u_debug.h"
307ec681f3Smrg
317ec681f3Smrgenum dxil_prog_sig_comp_type dxil_get_prog_sig_comp_type(const struct glsl_type *type)
327ec681f3Smrg{
337ec681f3Smrg   type = glsl_without_array(type);
347ec681f3Smrg
357ec681f3Smrg   switch (glsl_get_base_type(type)) {
367ec681f3Smrg   case GLSL_TYPE_UINT: return DXIL_PROG_SIG_COMP_TYPE_UINT32;
377ec681f3Smrg   case GLSL_TYPE_INT: return DXIL_PROG_SIG_COMP_TYPE_SINT32;
387ec681f3Smrg   case GLSL_TYPE_FLOAT: return DXIL_PROG_SIG_COMP_TYPE_FLOAT32;
397ec681f3Smrg   case GLSL_TYPE_FLOAT16: return DXIL_PROG_SIG_COMP_TYPE_FLOAT16;
407ec681f3Smrg   case GLSL_TYPE_DOUBLE: return DXIL_PROG_SIG_COMP_TYPE_FLOAT64;
417ec681f3Smrg   case GLSL_TYPE_UINT16: return DXIL_PROG_SIG_COMP_TYPE_UINT16;
427ec681f3Smrg   case GLSL_TYPE_INT16: return DXIL_PROG_SIG_COMP_TYPE_SINT16;
437ec681f3Smrg   case GLSL_TYPE_UINT64: return DXIL_PROG_SIG_COMP_TYPE_UINT64;
447ec681f3Smrg   case GLSL_TYPE_INT64: return DXIL_PROG_SIG_COMP_TYPE_SINT64;
457ec681f3Smrg   case GLSL_TYPE_BOOL: return DXIL_PROG_SIG_COMP_TYPE_UINT32;
467ec681f3Smrg   default:
477ec681f3Smrg      debug_printf("unexpected type: %s\n", glsl_get_type_name(type));
487ec681f3Smrg      return DXIL_PROG_SIG_COMP_TYPE_UNKNOWN;
497ec681f3Smrg   }
507ec681f3Smrg}
517ec681f3Smrg
527ec681f3Smrgenum dxil_component_type dxil_get_comp_type(const struct glsl_type *type)
537ec681f3Smrg{
547ec681f3Smrg   type = glsl_without_array(type);
557ec681f3Smrg
567ec681f3Smrg   enum glsl_base_type base_type = glsl_get_base_type(type);
577ec681f3Smrg   if (glsl_type_is_sampler(type) || glsl_type_is_image(type))
587ec681f3Smrg      base_type = glsl_get_sampler_result_type(type);
597ec681f3Smrg   switch (base_type) {
607ec681f3Smrg   case GLSL_TYPE_UINT: return DXIL_COMP_TYPE_U32;
617ec681f3Smrg   case GLSL_TYPE_INT: return DXIL_COMP_TYPE_I32;
627ec681f3Smrg   case GLSL_TYPE_FLOAT: return DXIL_COMP_TYPE_F32;
637ec681f3Smrg   case GLSL_TYPE_FLOAT16: return DXIL_COMP_TYPE_F16;
647ec681f3Smrg   case GLSL_TYPE_DOUBLE: return DXIL_COMP_TYPE_F64;
657ec681f3Smrg   case GLSL_TYPE_UINT16: return DXIL_COMP_TYPE_U16;
667ec681f3Smrg   case GLSL_TYPE_INT16: return DXIL_COMP_TYPE_I16;
677ec681f3Smrg   case GLSL_TYPE_UINT64: return DXIL_COMP_TYPE_U64;
687ec681f3Smrg   case GLSL_TYPE_INT64: return DXIL_COMP_TYPE_I64;
697ec681f3Smrg   case GLSL_TYPE_BOOL: return DXIL_COMP_TYPE_I1;
707ec681f3Smrg
717ec681f3Smrg   default:
727ec681f3Smrg      debug_printf("type: %s\n", glsl_get_type_name(type));
737ec681f3Smrg      unreachable("unexpected glsl type");
747ec681f3Smrg   }
757ec681f3Smrg}
767ec681f3Smrg
777ec681f3Smrgenum dxil_resource_kind dxil_get_resource_kind(const struct glsl_type *type)
787ec681f3Smrg{
797ec681f3Smrg   type = glsl_without_array(type);
807ec681f3Smrg
817ec681f3Smrg   /* This looks weird, we strip the arrays but then we still test whether it's
827ec681f3Smrg    * an array, key is the first refers to sampler[] and the second to samplerArray */
837ec681f3Smrg   bool is_array = glsl_sampler_type_is_array(type);
847ec681f3Smrg
857ec681f3Smrg   if (glsl_type_is_sampler(type) || glsl_type_is_image(type)) {
867ec681f3Smrg      switch (glsl_get_sampler_dim(type)) {
877ec681f3Smrg         case GLSL_SAMPLER_DIM_1D:
887ec681f3Smrg            return is_array ? DXIL_RESOURCE_KIND_TEXTURE1D_ARRAY
897ec681f3Smrg                            : DXIL_RESOURCE_KIND_TEXTURE1D;
907ec681f3Smrg         case GLSL_SAMPLER_DIM_2D:
917ec681f3Smrg            return is_array ? DXIL_RESOURCE_KIND_TEXTURE2D_ARRAY
927ec681f3Smrg                            : DXIL_RESOURCE_KIND_TEXTURE2D;
937ec681f3Smrg         case GLSL_SAMPLER_DIM_3D:
947ec681f3Smrg            return DXIL_RESOURCE_KIND_TEXTURE3D;
957ec681f3Smrg         case GLSL_SAMPLER_DIM_CUBE:
967ec681f3Smrg            return is_array ? DXIL_RESOURCE_KIND_TEXTURECUBE_ARRAY
977ec681f3Smrg                            : DXIL_RESOURCE_KIND_TEXTURECUBE;
987ec681f3Smrg         case GLSL_SAMPLER_DIM_RECT:
997ec681f3Smrg            return DXIL_RESOURCE_KIND_TEXTURE2D;
1007ec681f3Smrg         case GLSL_SAMPLER_DIM_BUF:
1017ec681f3Smrg            return DXIL_RESOURCE_KIND_TYPED_BUFFER;
1027ec681f3Smrg         case GLSL_SAMPLER_DIM_MS:
1037ec681f3Smrg            return is_array ? DXIL_RESOURCE_KIND_TEXTURE2DMS_ARRAY
1047ec681f3Smrg                            : DXIL_RESOURCE_KIND_TEXTURE2DMS;
1057ec681f3Smrg
1067ec681f3Smrg         default:
1077ec681f3Smrg            debug_printf("type: %s\n", glsl_get_type_name(type));
1087ec681f3Smrg            unreachable("unexpected sampler type");
1097ec681f3Smrg      }
1107ec681f3Smrg   }
1117ec681f3Smrg
1127ec681f3Smrg   debug_printf("type: %s\n", glsl_get_type_name(type));
1137ec681f3Smrg   unreachable("unexpected glsl type");
1147ec681f3Smrg}
1157ec681f3Smrg
1167ec681f3Smrgenum dxil_input_primitive dxil_get_input_primitive(unsigned primitive)
1177ec681f3Smrg{
1187ec681f3Smrg   switch (primitive) {
1197ec681f3Smrg   case GL_POINTS:
1207ec681f3Smrg      return DXIL_INPUT_PRIMITIVE_POINT;
1217ec681f3Smrg   case GL_LINES:
1227ec681f3Smrg      return DXIL_INPUT_PRIMITIVE_LINE;
1237ec681f3Smrg   case GL_LINES_ADJACENCY:
1247ec681f3Smrg      return DXIL_INPUT_PRIMITIVE_LINES_ADJENCY;
1257ec681f3Smrg   case GL_TRIANGLES:
1267ec681f3Smrg      return DXIL_INPUT_PRIMITIVE_TRIANGLE;
1277ec681f3Smrg   case GL_TRIANGLES_ADJACENCY:
1287ec681f3Smrg      return DXIL_INPUT_PRIMITIVE_TRIANGLES_ADJENCY;
1297ec681f3Smrg   default:
1307ec681f3Smrg      unreachable("unhandled primitive topology");
1317ec681f3Smrg   }
1327ec681f3Smrg}
1337ec681f3Smrg
1347ec681f3Smrgenum dxil_primitive_topology dxil_get_primitive_topology(unsigned topology)
1357ec681f3Smrg{
1367ec681f3Smrg   switch (topology) {
1377ec681f3Smrg   case GL_POINTS:
1387ec681f3Smrg      return DXIL_PRIMITIVE_TOPOLOGY_POINT_LIST;
1397ec681f3Smrg   case GL_LINES:
1407ec681f3Smrg      return DXIL_PRIMITIVE_TOPOLOGY_LINE_LIST;
1417ec681f3Smrg   case GL_LINE_STRIP:
1427ec681f3Smrg      return DXIL_PRIMITIVE_TOPOLOGY_LINE_STRIP;
1437ec681f3Smrg   case GL_TRIANGLE_STRIP:
1447ec681f3Smrg      return DXIL_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
1457ec681f3Smrg   default:
1467ec681f3Smrg      unreachable("unhandled primitive topology");
1477ec681f3Smrg   }
1487ec681f3Smrg}
1497ec681f3Smrg
1507ec681f3Smrgstatic const char *overload_str[DXIL_NUM_OVERLOADS] = {
1517ec681f3Smrg   [DXIL_NONE] = "",
1527ec681f3Smrg   [DXIL_I16] = "i16",
1537ec681f3Smrg   [DXIL_I32] = "i32",
1547ec681f3Smrg   [DXIL_I64] = "i64",
1557ec681f3Smrg   [DXIL_F16] = "f16",
1567ec681f3Smrg   [DXIL_F32] = "f32",
1577ec681f3Smrg   [DXIL_F64] = "f64",
1587ec681f3Smrg};
1597ec681f3Smrg
1607ec681f3Smrgconst char *dxil_overload_suffix( enum overload_type overload)
1617ec681f3Smrg{
1627ec681f3Smrg   assert(overload < DXIL_NUM_OVERLOADS);
1637ec681f3Smrg   return overload_str[overload];
1647ec681f3Smrg}
165