From a7e2a69a6fbcd1a4bdea845f08004358998f66bc Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Fri, 24 Mar 2017 14:13:59 +0100 Subject: [PATCH] Add bit casting to HLSL --- spirv_glsl.hpp | 2 +- spirv_hlsl.cpp | 30 ++++++++++++++++++++++++++++++ spirv_hlsl.hpp | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/spirv_glsl.hpp b/spirv_glsl.hpp index 83883a3..63d9c9b 100644 --- a/spirv_glsl.hpp +++ b/spirv_glsl.hpp @@ -371,7 +371,7 @@ protected: uint32_t type_to_std430_size(const SPIRType &type, uint64_t flags); std::string bitcast_glsl(const SPIRType &result_type, uint32_t arg); - std::string bitcast_glsl_op(const SPIRType &result_type, const SPIRType &argument_type); + virtual std::string bitcast_glsl_op(const SPIRType &result_type, const SPIRType &argument_type); std::string build_composite_combiner(const uint32_t *elems, uint32_t length); bool remove_duplicate_swizzle(std::string &op); bool remove_unity_swizzle(uint32_t base, std::string &op); diff --git a/spirv_hlsl.cpp b/spirv_hlsl.cpp index 3451abf..cab1fbf 100644 --- a/spirv_hlsl.cpp +++ b/spirv_hlsl.cpp @@ -1242,6 +1242,36 @@ void CompilerHLSL::emit_uniform(const SPIRVariable &var) statement(variable_decl(var), ";"); } +string CompilerHLSL::bitcast_glsl_op(const SPIRType &out_type, const SPIRType &in_type) +{ + if (out_type.basetype == SPIRType::UInt && in_type.basetype == SPIRType::Int) + return type_to_glsl(out_type); + else if (out_type.basetype == SPIRType::UInt64 && in_type.basetype == SPIRType::Int64) + return type_to_glsl(out_type); + else if (out_type.basetype == SPIRType::UInt && in_type.basetype == SPIRType::Float) + return "asuint"; + else if (out_type.basetype == SPIRType::Int && in_type.basetype == SPIRType::UInt) + return type_to_glsl(out_type); + else if (out_type.basetype == SPIRType::Int64 && in_type.basetype == SPIRType::UInt64) + return type_to_glsl(out_type); + else if (out_type.basetype == SPIRType::Int && in_type.basetype == SPIRType::Float) + return "asint"; + else if (out_type.basetype == SPIRType::Float && in_type.basetype == SPIRType::UInt) + return "asfloat"; + else if (out_type.basetype == SPIRType::Float && in_type.basetype == SPIRType::Int) + return "asfloat"; + else if (out_type.basetype == SPIRType::Int64 && in_type.basetype == SPIRType::Double) + SPIRV_CROSS_THROW("Double to Int64 is not supported in HLSL."); + else if (out_type.basetype == SPIRType::UInt64 && in_type.basetype == SPIRType::Double) + SPIRV_CROSS_THROW("Double to UInt64 is not supported in HLSL."); + else if (out_type.basetype == SPIRType::Double && in_type.basetype == SPIRType::Int64) + return "asdouble"; + else if (out_type.basetype == SPIRType::Double && in_type.basetype == SPIRType::UInt64) + return "asdouble"; + else + return ""; +} + void CompilerHLSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop, const uint32_t *args, uint32_t count) { GLSLstd450 op = static_cast(eop); diff --git a/spirv_hlsl.hpp b/spirv_hlsl.hpp index 2d4961c..1b53550 100644 --- a/spirv_hlsl.hpp +++ b/spirv_hlsl.hpp @@ -69,6 +69,7 @@ private: void emit_uniform(const SPIRVariable &var) override; std::string layout_for_member(const SPIRType &type, uint32_t index) override; std::string to_interpolation_qualifiers(uint64_t flags) override; + std::string bitcast_glsl_op(const SPIRType &result_type, const SPIRType &argument_type) override; const char *to_storage_qualifiers_glsl(const SPIRVariable &var) override;