Refactor out access chain reads.

Prepare for matrix, array and struct loads.
This commit is contained in:
Hans-Kristian Arntzen 2017-08-15 10:12:08 +02:00
Родитель af2b233582
Коммит 4375aa3fd2
2 изменённых файлов: 59 добавлений и 37 удалений

Просмотреть файл

@ -17,6 +17,7 @@
#include "spirv_hlsl.hpp" #include "spirv_hlsl.hpp"
#include "GLSL.std.450.h" #include "GLSL.std.450.h"
#include <algorithm> #include <algorithm>
#include <assert.h>
using namespace spv; using namespace spv;
using namespace spirv_cross; using namespace spirv_cross;
@ -1849,17 +1850,9 @@ void CompilerHLSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop,
} }
} }
void CompilerHLSL::emit_load(const Instruction &instruction) string CompilerHLSL::read_access_chain(const SPIRAccessChain &chain)
{ {
auto ops = stream(instruction); auto &type = get<SPIRType>(chain.basetype);
auto *chain = maybe_get<SPIRAccessChain>(ops[2]);
if (chain)
{
auto &type = get<SPIRType>(chain->basetype);
uint32_t result_type = ops[0];
uint32_t id = ops[1];
uint32_t ptr = ops[2];
SPIRType target_type; SPIRType target_type;
target_type.basetype = SPIRType::UInt; target_type.basetype = SPIRType::UInt;
@ -1892,11 +1885,27 @@ void CompilerHLSL::emit_load(const Instruction &instruction)
SPIRV_CROSS_THROW("Unknown vector size."); SPIRV_CROSS_THROW("Unknown vector size.");
} }
auto load_expr = join(chain->base, ".", load_op, "(", chain->dynamic_index, chain->static_index, ")"); auto load_expr = join(chain.base, ".", load_op, "(", chain.dynamic_index, chain.static_index, ")");
auto bitcast_op = bitcast_glsl_op(type, target_type); auto bitcast_op = bitcast_glsl_op(type, target_type);
if (!bitcast_op.empty()) if (!bitcast_op.empty())
load_expr = join(bitcast_op, "(", load_expr, ")"); load_expr = join(bitcast_op, "(", load_expr, ")");
return load_expr;
}
void CompilerHLSL::emit_load(const Instruction &instruction)
{
auto ops = stream(instruction);
auto *chain = maybe_get<SPIRAccessChain>(ops[2]);
if (chain)
{
uint32_t result_type = ops[0];
uint32_t id = ops[1];
uint32_t ptr = ops[2];
auto load_expr = read_access_chain(*chain);
bool forward = should_forward(ptr) && forced_temporaries.find(id) == end(forced_temporaries); bool forward = should_forward(ptr) && forced_temporaries.find(id) == end(forced_temporaries);
auto &e = emit_op(result_type, id, load_expr, forward, true); auto &e = emit_op(result_type, id, load_expr, forward, true);
e.need_transpose = false; // TODO: Forward this somehow. e.need_transpose = false; // TODO: Forward this somehow.
@ -1994,8 +2003,20 @@ void CompilerHLSL::emit_access_chain(const Instruction &instruction)
base = to_expression(ops[2]); base = to_expression(ops[2]);
auto *basetype = &type; auto *basetype = &type;
// Start traversing type hierarchy at the proper non-pointer types.
while (basetype->pointer)
{
assert(basetype->parent_type);
basetype = &get<SPIRType>(basetype->parent_type);
}
// Traverse the type hierarchy down to the actual buffer types.
for (uint32_t i = 0; i < to_plain_buffer_length; i++) for (uint32_t i = 0; i < to_plain_buffer_length; i++)
basetype = &get<SPIRType>(type.parent_type); {
assert(basetype->parent_type);
basetype = &get<SPIRType>(basetype->parent_type);
}
uint32_t matrix_stride = 0; uint32_t matrix_stride = 0;
bool need_transpose = false; bool need_transpose = false;

Просмотреть файл

@ -90,6 +90,7 @@ private:
void emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id) override; void emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id) override;
void emit_access_chain(const Instruction &instruction); void emit_access_chain(const Instruction &instruction);
void emit_load(const Instruction &instruction); void emit_load(const Instruction &instruction);
std::string read_access_chain(const SPIRAccessChain &chain);
void emit_store(const Instruction &instruction); void emit_store(const Instruction &instruction);
const char *to_storage_qualifiers_glsl(const SPIRVariable &var) override; const char *to_storage_qualifiers_glsl(const SPIRVariable &var) override;