Merge remote-tracking branch 'fdo-personal/wip/nir-vtn' into vulkan

This adds the SPIR-V -> NIR translator.
This commit is contained in:
Jason Ekstrand 2015-05-16 12:43:16 -07:00
commit a924ea0c75
26 changed files with 3712 additions and 35 deletions

View file

@ -77,7 +77,7 @@ check_PROGRAMS = \
tests/sampler-types-test \
tests/uniform-initializer-test
noinst_PROGRAMS = glsl_compiler
noinst_PROGRAMS = glsl_compiler spirv2nir
tests_blob_test_SOURCES = \
tests/blob_test.c
@ -162,6 +162,16 @@ glsl_compiler_LDADD = \
$(top_builddir)/src/libglsl_util.la \
$(PTHREAD_LIBS)
spirv2nir_SOURCES = \
standalone_scaffolding.cpp \
standalone_scaffolding.h \
nir/spirv2nir.c
spirv2nir_LDADD = \
libglsl.la \
$(top_builddir)/src/libglsl_util.la \
$(PTHREAD_LIBS)
glsl_test_SOURCES = \
standalone_scaffolding.cpp \
tests/common.c \

View file

@ -59,6 +59,7 @@ NIR_FILES = \
nir/nir_remove_dead_variables.c \
nir/nir_search.c \
nir/nir_search.h \
nir/nir_spirv.h \
nir/nir_split_var_copies.c \
nir/nir_sweep.c \
nir/nir_to_ssa.c \
@ -68,6 +69,8 @@ NIR_FILES = \
nir/nir_worklist.c \
nir/nir_worklist.h \
nir/nir_types.cpp \
nir/spirv_to_nir.c \
nir/spirv_glsl450_to_nir.c \
$(NIR_GENERATED_FILES)
# libglsl

View file

@ -970,6 +970,7 @@ do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
case GLSL_TYPE_SAMPLER:
case GLSL_TYPE_IMAGE:
case GLSL_TYPE_INTERFACE:
case GLSL_TYPE_FUNCTION:
case GLSL_TYPE_ATOMIC_UINT:
/* I assume a comparison of a struct containing a sampler just
* ignores the sampler present in the type.

View file

@ -32,6 +32,7 @@ mtx_t glsl_type::mutex = _MTX_INITIALIZER_NP;
hash_table *glsl_type::array_types = NULL;
hash_table *glsl_type::record_types = NULL;
hash_table *glsl_type::interface_types = NULL;
hash_table *glsl_type::function_types = NULL;
void *glsl_type::mem_ctx = NULL;
void
@ -159,6 +160,39 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
mtx_unlock(&glsl_type::mutex);
}
glsl_type::glsl_type(const glsl_type *return_type,
const glsl_function_param *params, unsigned num_params) :
gl_type(0),
base_type(GLSL_TYPE_FUNCTION),
sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
sampler_type(0), interface_packing(0),
vector_elements(0), matrix_columns(0),
length(num_params)
{
unsigned int i;
mtx_lock(&glsl_type::mutex);
init_ralloc_type_ctx();
this->fields.parameters = rzalloc_array(this->mem_ctx,
glsl_function_param, num_params + 1);
/* We store the return type as the first parameter */
this->fields.parameters[0].type = return_type;
this->fields.parameters[0].in = false;
this->fields.parameters[0].out = true;
/* We store the i'th parameter in slot i+1 */
for (i = 0; i < length; i++) {
this->fields.parameters[i + 1].type = params[i].type;
this->fields.parameters[i + 1].in = params[i].in;
this->fields.parameters[i + 1].out = params[i].out;
}
mtx_unlock(&glsl_type::mutex);
}
bool
glsl_type::contains_sampler() const
@ -827,6 +861,72 @@ glsl_type::get_interface_instance(const glsl_struct_field *fields,
}
static int
function_key_compare(const void *a, const void *b)
{
const glsl_type *const key1 = (glsl_type *) a;
const glsl_type *const key2 = (glsl_type *) b;
if (key1->length != key2->length)
return 1;
return memcmp(key1->fields.parameters, key2->fields.parameters,
(key1->length + 1) * sizeof(*key1->fields.parameters));
}
static unsigned
function_key_hash(const void *a)
{
const glsl_type *const key = (glsl_type *) a;
char hash_key[128];
unsigned size = 0;
size = snprintf(hash_key, sizeof(hash_key), "%08x", key->length);
for (unsigned i = 0; i < key->length; i++) {
if (size >= sizeof(hash_key))
break;
size += snprintf(& hash_key[size], sizeof(hash_key) - size,
"%p", (void *) key->fields.structure[i].type);
}
return hash_table_string_hash(& hash_key);
}
const glsl_type *
glsl_type::get_function_instance(const glsl_type *return_type,
const glsl_function_param *params,
unsigned num_params)
{
const glsl_type key(return_type, params, num_params);
mtx_lock(&glsl_type::mutex);
if (function_types == NULL) {
function_types = hash_table_ctor(64, function_key_hash,
function_key_compare);
}
const glsl_type *t = (glsl_type *) hash_table_find(function_types, &key);
if (t == NULL) {
mtx_unlock(&glsl_type::mutex);
t = new glsl_type(return_type, params, num_params);
mtx_lock(&glsl_type::mutex);
hash_table_insert(function_types, (void *) t, t);
}
assert(t->base_type == GLSL_TYPE_FUNCTION);
assert(t->length == num_params);
mtx_unlock(&glsl_type::mutex);
return t;
}
const glsl_type *
glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
{
@ -955,6 +1055,7 @@ glsl_type::component_slots() const
case GLSL_TYPE_IMAGE:
return 1;
case GLSL_TYPE_FUNCTION:
case GLSL_TYPE_SAMPLER:
case GLSL_TYPE_ATOMIC_UINT:
case GLSL_TYPE_VOID:
@ -1326,6 +1427,7 @@ glsl_type::count_attribute_slots() const
case GLSL_TYPE_ARRAY:
return this->length * this->fields.array->count_attribute_slots();
case GLSL_TYPE_FUNCTION:
case GLSL_TYPE_SAMPLER:
case GLSL_TYPE_IMAGE:
case GLSL_TYPE_ATOMIC_UINT:

View file

@ -56,6 +56,7 @@ enum glsl_base_type {
GLSL_TYPE_IMAGE,
GLSL_TYPE_ATOMIC_UINT,
GLSL_TYPE_STRUCT,
GLSL_TYPE_FUNCTION,
GLSL_TYPE_INTERFACE,
GLSL_TYPE_ARRAY,
GLSL_TYPE_VOID,
@ -178,7 +179,7 @@ struct glsl_type {
*/
union {
const struct glsl_type *array; /**< Type of array elements. */
const struct glsl_type *parameters; /**< Parameters to function. */
struct glsl_function_param *parameters; /**< Parameters to function. */
struct glsl_struct_field *structure; /**< List of struct fields. */
} fields;
@ -275,6 +276,13 @@ struct glsl_type {
enum glsl_interface_packing packing,
const char *block_name);
/**
* Get the instance of a function type
*/
static const glsl_type *get_function_instance(const struct glsl_type *return_type,
const glsl_function_param *parameters,
unsigned num_params);
/**
* Get the type resulting from a multiplication of \p type_a * \p type_b
*/
@ -688,6 +696,10 @@ private:
glsl_type(const glsl_struct_field *fields, unsigned num_fields,
enum glsl_interface_packing packing, const char *name);
/** Constructor for interface types */
glsl_type(const glsl_type *return_type,
const glsl_function_param *params, unsigned num_params);
/** Constructor for array types */
glsl_type(const glsl_type *array, unsigned length);
@ -700,6 +712,9 @@ private:
/** Hash table containing the known interface types. */
static struct hash_table *interface_types;
/** Hash table containing the known function types. */
static struct hash_table *function_types;
static int record_key_compare(const void *a, const void *b);
static unsigned record_key_hash(const void *key);
@ -727,6 +742,10 @@ private:
/*@}*/
};
#undef DECL_TYPE
#undef STRUCT_TYPE
#endif /* __cplusplus */
struct glsl_struct_field {
const struct glsl_type *type;
const char *name;
@ -770,14 +789,17 @@ struct glsl_struct_field {
int stream;
};
struct glsl_function_param {
const struct glsl_type *type;
bool in;
bool out;
};
static inline unsigned int
glsl_align(unsigned int a, unsigned int align)
{
return (a + align - 1) / align * align;
}
#undef DECL_TYPE
#undef STRUCT_TYPE
#endif /* __cplusplus */
#endif /* GLSL_TYPES_H */

View file

@ -357,6 +357,7 @@ ir_constant::clone(void *mem_ctx, struct hash_table *ht) const
return c;
}
case GLSL_TYPE_FUNCTION:
case GLSL_TYPE_SAMPLER:
case GLSL_TYPE_IMAGE:
case GLSL_TYPE_ATOMIC_UINT:

View file

@ -88,6 +88,7 @@ copy_constant_to_storage(union gl_constant_value *storage,
case GLSL_TYPE_IMAGE:
case GLSL_TYPE_ATOMIC_UINT:
case GLSL_TYPE_INTERFACE:
case GLSL_TYPE_FUNCTION:
case GLSL_TYPE_VOID:
case GLSL_TYPE_ERROR:
/* All other types should have already been filtered by other

View file

@ -41,12 +41,6 @@
static int glsl_version = 330;
extern "C" void
_mesa_error_no_memory(const char *caller)
{
fprintf(stderr, "Mesa error: out of memory in %s", caller);
}
static void
initialize_context(struct gl_context *ctx, gl_api api)
{

View file

@ -782,6 +782,15 @@ NIR_DEFINE_CAST(nir_deref_as_var, nir_deref, nir_deref_var, deref)
NIR_DEFINE_CAST(nir_deref_as_array, nir_deref, nir_deref_array, deref)
NIR_DEFINE_CAST(nir_deref_as_struct, nir_deref, nir_deref_struct, deref)
/** Returns the tail of a deref chain */
static inline nir_deref *
nir_deref_tail(nir_deref *deref)
{
while (deref->child)
deref = deref->child;
return deref;
}
typedef struct {
nir_instr instr;

View file

@ -67,6 +67,7 @@ type_size(const struct glsl_type *type)
return 0;
case GLSL_TYPE_IMAGE:
return 0;
case GLSL_TYPE_FUNCTION:
case GLSL_TYPE_VOID:
case GLSL_TYPE_ERROR:
case GLSL_TYPE_DOUBLE:

View file

@ -53,17 +53,6 @@ deref_next_wildcard_parent(nir_deref *deref)
return NULL;
}
/* Returns the last deref in the chain.
*/
static nir_deref *
get_deref_tail(nir_deref *deref)
{
while (deref->child)
deref = deref->child;
return deref;
}
/* This function recursively walks the given deref chain and replaces the
* given copy instruction with an equivalent sequence load/store
* operations.
@ -121,8 +110,8 @@ emit_copy_load_store(nir_intrinsic_instr *copy_instr,
} else {
/* In this case, we have no wildcards anymore, so all we have to do
* is just emit the load and store operations. */
src_tail = get_deref_tail(src_tail);
dest_tail = get_deref_tail(dest_tail);
src_tail = nir_deref_tail(src_tail);
dest_tail = nir_deref_tail(dest_tail);
assert(src_tail->type == dest_tail->type);

38
src/glsl/nir/nir_spirv.h Normal file
View file

@ -0,0 +1,38 @@
/*
* Copyright © 2015 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Jason Ekstrand (jason@jlekstrand.net)
*
*/
#pragma once
#ifndef _NIR_SPIRV_H_
#define _NIR_SPIRV_H_
#include "nir.h"
nir_shader *spirv_to_nir(const uint32_t *words, size_t word_count,
const nir_shader_compiler_options *options);
#endif /* _NIR_SPIRV_H_ */

View file

@ -66,14 +66,6 @@ struct split_var_copies_state {
void *dead_ctx;
};
static nir_deref *
get_deref_tail(nir_deref *deref)
{
while (deref->child != NULL)
deref = deref->child;
return deref;
}
/* Recursively constructs deref chains to split a copy instruction into
* multiple (if needed) copy instructions with full-length deref chains.
* External callers of this function should pass the tail and head of the
@ -225,8 +217,8 @@ split_var_copies_block(nir_block *block, void *void_state)
nir_deref *dest_head = &intrinsic->variables[0]->deref;
nir_deref *src_head = &intrinsic->variables[1]->deref;
nir_deref *dest_tail = get_deref_tail(dest_head);
nir_deref *src_tail = get_deref_tail(src_head);
nir_deref *dest_tail = nir_deref_tail(dest_head);
nir_deref *src_tail = nir_deref_tail(src_head);
switch (glsl_get_base_type(src_tail->type)) {
case GLSL_TYPE_ARRAY:

View file

@ -70,6 +70,18 @@ glsl_get_struct_field(const glsl_type *type, unsigned index)
return type->fields.structure[index].type;
}
const glsl_type *
glsl_get_function_return_type(const glsl_type *type)
{
return type->fields.parameters[0].type;
}
const glsl_function_param *
glsl_get_function_param(const glsl_type *type, unsigned index)
{
return &type->fields.parameters[index + 1];
}
const struct glsl_type *
glsl_get_column_type(const struct glsl_type *type)
{
@ -112,6 +124,20 @@ glsl_get_struct_elem_name(const struct glsl_type *type, unsigned index)
return type->fields.structure[index].name;
}
glsl_sampler_dim
glsl_get_sampler_dim(const struct glsl_type *type)
{
assert(glsl_type_is_sampler(type));
return (glsl_sampler_dim)type->sampler_dimensionality;
}
glsl_base_type
glsl_get_sampler_result_type(const struct glsl_type *type)
{
assert(glsl_type_is_sampler(type));
return (glsl_base_type)type->sampler_type;
}
bool
glsl_type_is_void(const glsl_type *type)
{
@ -130,12 +156,38 @@ glsl_type_is_scalar(const struct glsl_type *type)
return type->is_scalar();
}
bool
glsl_type_is_vector_or_scalar(const struct glsl_type *type)
{
return type->is_vector() || type->is_scalar();
}
bool
glsl_type_is_matrix(const struct glsl_type *type)
{
return type->is_matrix();
}
bool
glsl_type_is_sampler(const struct glsl_type *type)
{
return type->is_sampler();
}
bool
glsl_sampler_type_is_shadow(const struct glsl_type *type)
{
assert(glsl_type_is_sampler(type));
return type->sampler_shadow;
}
bool
glsl_sampler_type_is_array(const struct glsl_type *type)
{
assert(glsl_type_is_sampler(type));
return type->sampler_array;
}
const glsl_type *
glsl_void_type(void)
{
@ -148,14 +200,73 @@ glsl_float_type(void)
return glsl_type::float_type;
}
const glsl_type *
glsl_int_type(void)
{
return glsl_type::int_type;
}
const glsl_type *
glsl_uint_type(void)
{
return glsl_type::uint_type;
}
const glsl_type *
glsl_bool_type(void)
{
return glsl_type::bool_type;
}
const glsl_type *
glsl_vec4_type(void)
{
return glsl_type::vec4_type;
}
const glsl_type *
glsl_scalar_type(enum glsl_base_type base_type)
{
return glsl_type::get_instance(base_type, 1, 1);
}
const glsl_type *
glsl_vector_type(enum glsl_base_type base_type, unsigned components)
{
assert(components > 1 && components <= 4);
return glsl_type::get_instance(base_type, components, 1);
}
const glsl_type *
glsl_matrix_type(enum glsl_base_type base_type, unsigned rows, unsigned columns)
{
assert(rows > 1 && rows <= 4 && columns > 1 && columns <= 4);
return glsl_type::get_instance(base_type, rows, columns);
}
const glsl_type *
glsl_array_type(const glsl_type *base, unsigned elements)
{
return glsl_type::get_array_instance(base, elements);
}
const glsl_type *
glsl_struct_type(const glsl_struct_field *fields,
unsigned num_fields, const char *name)
{
return glsl_type::get_record_instance(fields, num_fields, name);
}
const struct glsl_type *
glsl_sampler_type(enum glsl_sampler_dim dim, bool is_shadow, bool is_array,
enum glsl_base_type base_type)
{
return glsl_type::get_sampler_instance(dim, is_shadow, is_array, base_type);
}
const glsl_type *
glsl_function_type(const glsl_type *return_type,
const glsl_function_param *params, unsigned num_params)
{
return glsl_type::get_function_instance(return_type, params, num_params);
}

View file

@ -49,6 +49,12 @@ const struct glsl_type *glsl_get_array_element(const struct glsl_type *type);
const struct glsl_type *glsl_get_column_type(const struct glsl_type *type);
const struct glsl_type *
glsl_get_function_return_type(const struct glsl_type *type);
const struct glsl_function_param *
glsl_get_function_param(const struct glsl_type *type, unsigned index);
enum glsl_base_type glsl_get_base_type(const struct glsl_type *type);
unsigned glsl_get_vector_elements(const struct glsl_type *type);
@ -62,17 +68,40 @@ unsigned glsl_get_length(const struct glsl_type *type);
const char *glsl_get_struct_elem_name(const struct glsl_type *type,
unsigned index);
enum glsl_sampler_dim glsl_get_sampler_dim(const struct glsl_type *type);
enum glsl_base_type glsl_get_sampler_result_type(const struct glsl_type *type);
bool glsl_type_is_void(const struct glsl_type *type);
bool glsl_type_is_vector(const struct glsl_type *type);
bool glsl_type_is_scalar(const struct glsl_type *type);
bool glsl_type_is_vector_or_scalar(const struct glsl_type *type);
bool glsl_type_is_matrix(const struct glsl_type *type);
bool glsl_type_is_sampler(const struct glsl_type *type);
bool glsl_sampler_type_is_shadow(const struct glsl_type *type);
bool glsl_sampler_type_is_array(const struct glsl_type *type);
const struct glsl_type *glsl_void_type(void);
const struct glsl_type *glsl_float_type(void);
const struct glsl_type *glsl_int_type(void);
const struct glsl_type *glsl_uint_type(void);
const struct glsl_type *glsl_bool_type(void);
const struct glsl_type *glsl_vec4_type(void);
const struct glsl_type *glsl_scalar_type(enum glsl_base_type base_type);
const struct glsl_type *glsl_vector_type(enum glsl_base_type base_type,
unsigned components);
const struct glsl_type *glsl_matrix_type(enum glsl_base_type base_type,
unsigned rows, unsigned columns);
const struct glsl_type *glsl_array_type(const struct glsl_type *base,
unsigned elements);
const struct glsl_type *glsl_struct_type(const struct glsl_struct_field *fields,
unsigned num_fields, const char *name);
const struct glsl_type *glsl_sampler_type(enum glsl_sampler_dim dim,
bool is_shadow, bool is_array,
enum glsl_base_type base_type);
const struct glsl_type * glsl_function_type(const struct glsl_type *return_type,
const struct glsl_function_param *params,
unsigned num_params);
#ifdef __cplusplus
}

1304
src/glsl/nir/spirv.h Normal file

File diff suppressed because it is too large Load diff

54
src/glsl/nir/spirv2nir.c Normal file
View file

@ -0,0 +1,54 @@
/*
* Copyright © 2015 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Jason Ekstrand (jason@jlekstrand.net)
*
*/
/*
* A simple executable that opens a SPIR-V shader, converts it to NIR, and
* dumps out the result. This should be useful for testing the
* spirv_to_nir code.
*/
#include "nir_spirv.h"
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd = open(argv[1], O_RDONLY);
off_t len = lseek(fd, 0, SEEK_END);
assert(len % 4 == 0);
size_t word_count = len / 4;
const void *map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
assert(map != NULL);
nir_shader *shader = spirv_to_nir(map, word_count, NULL);
nir_print_shader(shader, stderr);
}

View file

@ -0,0 +1,284 @@
/*
* Copyright © 2015 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Jason Ekstrand (jason@jlekstrand.net)
*
*/
#include "spirv_to_nir_private.h"
enum GLSL450Entrypoint {
Round = 0,
RoundEven = 1,
Trunc = 2,
Abs = 3,
Sign = 4,
Floor = 5,
Ceil = 6,
Fract = 7,
Radians = 8,
Degrees = 9,
Sin = 10,
Cos = 11,
Tan = 12,
Asin = 13,
Acos = 14,
Atan = 15,
Sinh = 16,
Cosh = 17,
Tanh = 18,
Asinh = 19,
Acosh = 20,
Atanh = 21,
Atan2 = 22,
Pow = 23,
Exp = 24,
Log = 25,
Exp2 = 26,
Log2 = 27,
Sqrt = 28,
InverseSqrt = 29,
Determinant = 30,
MatrixInverse = 31,
Modf = 32, // second argument needs the OpVariable = , not an OpLoad
Min = 33,
Max = 34,
Clamp = 35,
Mix = 36,
Step = 37,
SmoothStep = 38,
FloatBitsToInt = 39,
FloatBitsToUint = 40,
IntBitsToFloat = 41,
UintBitsToFloat = 42,
Fma = 43,
Frexp = 44,
Ldexp = 45,
PackSnorm4x8 = 46,
PackUnorm4x8 = 47,
PackSnorm2x16 = 48,
PackUnorm2x16 = 49,
PackHalf2x16 = 50,
PackDouble2x32 = 51,
UnpackSnorm2x16 = 52,
UnpackUnorm2x16 = 53,
UnpackHalf2x16 = 54,
UnpackSnorm4x8 = 55,
UnpackUnorm4x8 = 56,
UnpackDouble2x32 = 57,
Length = 58,
Distance = 59,
Cross = 60,
Normalize = 61,
Ftransform = 62,
FaceForward = 63,
Reflect = 64,
Refract = 65,
UaddCarry = 66,
UsubBorrow = 67,
UmulExtended = 68,
ImulExtended = 69,
BitfieldExtract = 70,
BitfieldInsert = 71,
BitfieldReverse = 72,
BitCount = 73,
FindLSB = 74,
FindMSB = 75,
InterpolateAtCentroid = 76,
InterpolateAtSample = 77,
InterpolateAtOffset = 78,
Count
};
static nir_ssa_def*
build_length(nir_builder *b, nir_ssa_def *vec)
{
switch (vec->num_components) {
case 1: return nir_fsqrt(b, nir_fmul(b, vec, vec));
case 2: return nir_fsqrt(b, nir_fdot2(b, vec, vec));
case 3: return nir_fsqrt(b, nir_fdot3(b, vec, vec));
case 4: return nir_fsqrt(b, nir_fdot4(b, vec, vec));
default:
unreachable("Invalid number of components");
}
}
static void
handle_glsl450_alu(struct vtn_builder *b, enum GLSL450Entrypoint entrypoint,
const uint32_t *w, unsigned count)
{
struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
/* Collect the various SSA sources */
unsigned num_inputs = count - 5;
nir_ssa_def *src[3];
for (unsigned i = 0; i < num_inputs; i++)
src[i] = vtn_ssa_value(b, w[i + 5]);
nir_op op;
switch (entrypoint) {
case Round: op = nir_op_fround_even; break; /* TODO */
case RoundEven: op = nir_op_fround_even; break;
case Trunc: op = nir_op_ftrunc; break;
case Abs: op = nir_op_fabs; break;
case Sign: op = nir_op_fsign; break;
case Floor: op = nir_op_ffloor; break;
case Ceil: op = nir_op_fceil; break;
case Fract: op = nir_op_ffract; break;
case Radians:
val->ssa = nir_fmul(&b->nb, src[0], nir_imm_float(&b->nb, 0.01745329251));
return;
case Degrees:
val->ssa = nir_fmul(&b->nb, src[0], nir_imm_float(&b->nb, 57.2957795131));
return;
case Sin: op = nir_op_fsin; break;
case Cos: op = nir_op_fcos; break;
case Tan:
val->ssa = nir_fdiv(&b->nb, nir_fsin(&b->nb, src[0]),
nir_fcos(&b->nb, src[0]));
return;
case Pow: op = nir_op_fpow; break;
case Exp2: op = nir_op_fexp2; break;
case Log2: op = nir_op_flog2; break;
case Sqrt: op = nir_op_fsqrt; break;
case InverseSqrt: op = nir_op_frsq; break;
case Modf: op = nir_op_fmod; break;
case Min: op = nir_op_fmin; break;
case Max: op = nir_op_fmax; break;
case Mix: op = nir_op_flrp; break;
case Step:
val->ssa = nir_sge(&b->nb, src[1], src[0]);
return;
case FloatBitsToInt:
case FloatBitsToUint:
case IntBitsToFloat:
case UintBitsToFloat:
/* Probably going to be removed from the final version of the spec. */
val->ssa = src[0];
return;
case Fma: op = nir_op_ffma; break;
case Ldexp: op = nir_op_ldexp; break;
/* Packing/Unpacking functions */
case PackSnorm4x8: op = nir_op_pack_snorm_4x8; break;
case PackUnorm4x8: op = nir_op_pack_unorm_4x8; break;
case PackSnorm2x16: op = nir_op_pack_snorm_2x16; break;
case PackUnorm2x16: op = nir_op_pack_unorm_2x16; break;
case PackHalf2x16: op = nir_op_pack_half_2x16; break;
case UnpackSnorm4x8: op = nir_op_unpack_snorm_4x8; break;
case UnpackUnorm4x8: op = nir_op_unpack_unorm_4x8; break;
case UnpackSnorm2x16: op = nir_op_unpack_snorm_2x16; break;
case UnpackUnorm2x16: op = nir_op_unpack_unorm_2x16; break;
case UnpackHalf2x16: op = nir_op_unpack_half_2x16; break;
case Length:
val->ssa = build_length(&b->nb, src[0]);
return;
case Distance:
val->ssa = build_length(&b->nb, nir_fsub(&b->nb, src[0], src[1]));
return;
case Normalize:
val->ssa = nir_fdiv(&b->nb, src[0], build_length(&b->nb, src[0]));
return;
case UaddCarry: op = nir_op_uadd_carry; break;
case UsubBorrow: op = nir_op_usub_borrow; break;
case BitfieldExtract: op = nir_op_ubitfield_extract; break; /* TODO */
case BitfieldInsert: op = nir_op_bitfield_insert; break;
case BitfieldReverse: op = nir_op_bitfield_reverse; break;
case BitCount: op = nir_op_bit_count; break;
case FindLSB: op = nir_op_find_lsb; break;
case FindMSB: op = nir_op_ufind_msb; break; /* TODO */
case Exp:
case Log:
case Clamp:
case Asin:
case Acos:
case Atan:
case Atan2:
case Sinh:
case Cosh:
case Tanh:
case Asinh:
case Acosh:
case Atanh:
case SmoothStep:
case Frexp:
case PackDouble2x32:
case UnpackDouble2x32:
case Cross:
case Ftransform:
case FaceForward:
case Reflect:
case Refract:
case UmulExtended:
case ImulExtended:
default:
unreachable("Unhandled opcode");
}
nir_alu_instr *instr = nir_alu_instr_create(b->shader, op);
nir_ssa_dest_init(&instr->instr, &instr->dest.dest,
glsl_get_vector_elements(val->type), val->name);
val->ssa = &instr->dest.dest.ssa;
for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++)
instr->src[i].src = nir_src_for_ssa(src[i]);
nir_builder_instr_insert(&b->nb, &instr->instr);
}
bool
vtn_handle_glsl450_instruction(struct vtn_builder *b, uint32_t ext_opcode,
const uint32_t *words, unsigned count)
{
switch ((enum GLSL450Entrypoint)ext_opcode) {
case Determinant:
case MatrixInverse:
case InterpolateAtCentroid:
case InterpolateAtSample:
case InterpolateAtOffset:
unreachable("Unhandled opcode");
default:
handle_glsl450_alu(b, (enum GLSL450Entrypoint)ext_opcode, words, count);
}
return true;
}

1572
src/glsl/nir/spirv_to_nir.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,148 @@
/*
* Copyright © 2015 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Jason Ekstrand (jason@jlekstrand.net)
*
*/
#include "nir_spirv.h"
#include "nir_builder.h"
#include "spirv.h"
struct vtn_builder;
struct vtn_decoration;
enum vtn_value_type {
vtn_value_type_invalid = 0,
vtn_value_type_undef,
vtn_value_type_string,
vtn_value_type_decoration_group,
vtn_value_type_type,
vtn_value_type_constant,
vtn_value_type_deref,
vtn_value_type_function,
vtn_value_type_block,
vtn_value_type_ssa,
vtn_value_type_extension,
};
struct vtn_block {
/* Merge opcode if this block contains a merge; SpvOpNop otherwise. */
SpvOp merge_op;
uint32_t merge_block_id;
const uint32_t *label;
const uint32_t *branch;
nir_block *block;
};
struct vtn_function {
struct exec_node node;
nir_function_overload *overload;
struct vtn_block *start_block;
};
typedef bool (*vtn_instruction_handler)(struct vtn_builder *, uint32_t,
const uint32_t *, unsigned);
struct vtn_value {
enum vtn_value_type value_type;
const char *name;
struct vtn_decoration *decoration;
const struct glsl_type *type;
union {
void *ptr;
char *str;
nir_constant *constant;
nir_deref_var *deref;
struct vtn_function *func;
struct vtn_block *block;
nir_ssa_def *ssa;
vtn_instruction_handler ext_handler;
};
};
struct vtn_decoration {
struct vtn_decoration *next;
const uint32_t *literals;
struct vtn_value *group;
SpvDecoration decoration;
};
struct vtn_builder {
nir_builder nb;
nir_shader *shader;
nir_function_impl *impl;
struct vtn_block *block;
unsigned value_id_bound;
struct vtn_value *values;
SpvExecutionModel execution_model;
struct vtn_value *entry_point;
struct vtn_function *func;
struct exec_list functions;
};
static inline struct vtn_value *
vtn_push_value(struct vtn_builder *b, uint32_t value_id,
enum vtn_value_type value_type)
{
assert(value_id < b->value_id_bound);
assert(b->values[value_id].value_type == vtn_value_type_invalid);
b->values[value_id].value_type = value_type;
return &b->values[value_id];
}
static inline struct vtn_value *
vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
{
assert(value_id < b->value_id_bound);
return &b->values[value_id];
}
static inline struct vtn_value *
vtn_value(struct vtn_builder *b, uint32_t value_id,
enum vtn_value_type value_type)
{
struct vtn_value *val = vtn_untyped_value(b, value_id);
assert(val->value_type == value_type);
return val;
}
nir_ssa_def *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);
typedef void (*vtn_decoration_foreach_cb)(struct vtn_builder *,
struct vtn_value *,
const struct vtn_decoration *,
void *);
void vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
vtn_decoration_foreach_cb cb, void *data);
bool vtn_handle_glsl450_instruction(struct vtn_builder *b, uint32_t ext_opcode,
const uint32_t *words, unsigned count);

View file

@ -34,6 +34,12 @@
#include <string.h>
#include "util/ralloc.h"
extern "C" void
_mesa_error_no_memory(const char *caller)
{
fprintf(stderr, "Mesa error: out of memory in %s", caller);
}
void
_mesa_warning(struct gl_context *ctx, const char *fmt, ...)
{

View file

@ -671,6 +671,7 @@ fs_visitor::type_size(const struct glsl_type *type)
case GLSL_TYPE_ERROR:
case GLSL_TYPE_INTERFACE:
case GLSL_TYPE_DOUBLE:
case GLSL_TYPE_FUNCTION:
unreachable("not reached");
}

View file

@ -1354,6 +1354,7 @@ fs_visitor::emit_assignment_writes(fs_reg &l, fs_reg &r,
case GLSL_TYPE_VOID:
case GLSL_TYPE_ERROR:
case GLSL_TYPE_INTERFACE:
case GLSL_TYPE_FUNCTION:
unreachable("not reached");
}
}

View file

@ -351,6 +351,7 @@ brw_type_for_base_type(const struct glsl_type *type)
case GLSL_TYPE_ERROR:
case GLSL_TYPE_INTERFACE:
case GLSL_TYPE_DOUBLE:
case GLSL_TYPE_FUNCTION:
unreachable("not reached");
}

View file

@ -615,6 +615,7 @@ type_size(const struct glsl_type *type)
case GLSL_TYPE_DOUBLE:
case GLSL_TYPE_ERROR:
case GLSL_TYPE_INTERFACE:
case GLSL_TYPE_FUNCTION:
unreachable("not reached");
}

View file

@ -541,6 +541,7 @@ type_size(const struct glsl_type *type)
case GLSL_TYPE_VOID:
case GLSL_TYPE_ERROR:
case GLSL_TYPE_INTERFACE:
case GLSL_TYPE_FUNCTION:
assert(!"Invalid type in type_size");
break;
}
@ -2448,6 +2449,7 @@ _mesa_associate_uniform_storage(struct gl_context *ctx,
case GLSL_TYPE_STRUCT:
case GLSL_TYPE_ERROR:
case GLSL_TYPE_INTERFACE:
case GLSL_TYPE_FUNCTION:
assert(!"Should not get here.");
break;
}