SPIR-V: Fix a couple of errors in translation

This commit is contained in:
Dr. Chat 2016-06-27 11:08:45 -05:00
parent f2ad6b8cb8
commit 75908a9865
2 changed files with 51 additions and 39 deletions

View file

@ -64,10 +64,13 @@ void SpirvShaderTranslator::StartTranslation() {
float_type_ = b.makeFloatType(32); float_type_ = b.makeFloatType(32);
int_type_ = b.makeIntType(32); int_type_ = b.makeIntType(32);
uint_type_ = b.makeUintType(32); uint_type_ = b.makeUintType(32);
vec2_uint_type_ = b.makeVectorType(uint_type_, 2);
vec2_float_type_ = b.makeVectorType(float_type_, 2); vec2_float_type_ = b.makeVectorType(float_type_, 2);
vec3_float_type_ = b.makeVectorType(float_type_, 3); vec3_float_type_ = b.makeVectorType(float_type_, 3);
vec4_float_type_ = b.makeVectorType(float_type_, 4); vec4_float_type_ = b.makeVectorType(float_type_, 4);
vec4_uint_type_ = b.makeVectorType(uint_type_, 4); vec4_uint_type_ = b.makeVectorType(uint_type_, 4);
vec2_bool_type_ = b.makeVectorType(bool_type_, 2);
vec3_bool_type_ = b.makeVectorType(bool_type_, 3);
vec4_bool_type_ = b.makeVectorType(bool_type_, 4); vec4_bool_type_ = b.makeVectorType(bool_type_, 4);
vec4_float_one_ = b.makeCompositeConstant( vec4_float_one_ = b.makeCompositeConstant(
@ -374,6 +377,7 @@ void SpirvShaderTranslator::StartTranslation() {
auto cond = b.createBinOp(spv::Op::OpIEqual, bool_type_, ps_param_gen_idx, auto cond = b.createBinOp(spv::Op::OpIEqual, bool_type_, ps_param_gen_idx,
b.makeUintConstant(i)); b.makeUintConstant(i));
cond = b.smearScalar(spv::NoPrecision, cond, vec4_bool_type_);
auto reg = b.createTriOp(spv::Op::OpSelect, vec4_float_type_, cond, param, auto reg = b.createTriOp(spv::Op::OpSelect, vec4_float_type_, cond, param,
b.createLoad(reg_ptr)); b.createLoad(reg_ptr));
b.createStore(reg, reg_ptr); b.createStore(reg, reg_ptr);
@ -876,9 +880,6 @@ void SpirvShaderTranslator::ProcessVertexFetchInstruction(
vertex_id = b.createCompositeExtract(vertex_id, float_type_, 0); vertex_id = b.createCompositeExtract(vertex_id, float_type_, 0);
vertex_id = b.createUnaryOp(spv::Op::OpConvertFToS, int_type_, vertex_id); vertex_id = b.createUnaryOp(spv::Op::OpConvertFToS, int_type_, vertex_id);
auto shader_vertex_id = b.createLoad(vertex_id_); auto shader_vertex_id = b.createLoad(vertex_id_);
auto cond =
b.createBinOp(spv::Op::OpIEqual, bool_type_, vertex_id, shader_vertex_id);
cond = b.smearScalar(spv::NoPrecision, cond, vec4_bool_type_);
// Skip loading if it's an indexed fetch. // Skip loading if it's an indexed fetch.
auto vertex_ptr = vertex_binding_map_[instr.operands[1].storage_index] auto vertex_ptr = vertex_binding_map_[instr.operands[1].storage_index]
@ -886,6 +887,39 @@ void SpirvShaderTranslator::ProcessVertexFetchInstruction(
assert_not_zero(vertex_ptr); assert_not_zero(vertex_ptr);
auto vertex = b.createLoad(vertex_ptr); auto vertex = b.createLoad(vertex_ptr);
auto cond =
b.createBinOp(spv::Op::OpIEqual, bool_type_, vertex_id, shader_vertex_id);
auto vertex_components = b.getNumComponents(vertex);
Id alt_vertex = 0;
switch (vertex_components) {
case 1:
alt_vertex = b.makeFloatConstant(0.f);
break;
case 2:
alt_vertex = b.makeCompositeConstant(
vec2_float_type_, std::vector<Id>({b.makeFloatConstant(0.f),
b.makeFloatConstant(1.f)}));
cond = b.smearScalar(spv::NoPrecision, cond, vec2_bool_type_);
break;
case 3:
alt_vertex = b.makeCompositeConstant(
vec3_float_type_,
std::vector<Id>({b.makeFloatConstant(0.f), b.makeFloatConstant(0.f),
b.makeFloatConstant(1.f)}));
cond = b.smearScalar(spv::NoPrecision, cond, vec3_bool_type_);
break;
case 4:
alt_vertex = b.makeCompositeConstant(
vec4_float_type_,
std::vector<Id>({b.makeFloatConstant(0.f), b.makeFloatConstant(0.f),
b.makeFloatConstant(0.f),
b.makeFloatConstant(1.f)}));
cond = b.smearScalar(spv::NoPrecision, cond, vec4_bool_type_);
break;
default:
assert_unhandled_case(vertex_components);
}
switch (instr.attributes.data_format) { switch (instr.attributes.data_format) {
case VertexFormat::k_8_8_8_8: case VertexFormat::k_8_8_8_8:
case VertexFormat::k_16_16: case VertexFormat::k_16_16:
@ -910,34 +944,6 @@ void SpirvShaderTranslator::ProcessVertexFetchInstruction(
} break; } break;
} }
auto vertex_components = b.getNumComponents(vertex);
Id alt_vertex = 0;
switch (vertex_components) {
case 1:
alt_vertex = b.makeFloatConstant(0.f);
break;
case 2:
alt_vertex = b.makeCompositeConstant(
vec2_float_type_, std::vector<Id>({b.makeFloatConstant(0.f),
b.makeFloatConstant(1.f)}));
break;
case 3:
alt_vertex = b.makeCompositeConstant(
vec3_float_type_,
std::vector<Id>({b.makeFloatConstant(0.f), b.makeFloatConstant(0.f),
b.makeFloatConstant(1.f)}));
break;
case 4:
alt_vertex = b.makeCompositeConstant(
vec4_float_type_,
std::vector<Id>({b.makeFloatConstant(0.f), b.makeFloatConstant(0.f),
b.makeFloatConstant(0.f),
b.makeFloatConstant(1.f)}));
break;
default:
assert_unhandled_case(vertex_components);
}
vertex = b.createTriOp(spv::Op::OpSelect, b.getTypeId(vertex), cond, vertex, vertex = b.createTriOp(spv::Op::OpSelect, b.getTypeId(vertex), cond, vertex,
alt_vertex); alt_vertex);
StoreToResult(vertex, instr.result); StoreToResult(vertex, instr.result);
@ -976,7 +982,7 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
// Operand 0 is the offset // Operand 0 is the offset
// Operand 1 is the sampler index // Operand 1 is the sampler index
Id dest = 0; Id dest = vec4_float_zero_;
Id src = LoadFromOperand(instr.operands[0]); Id src = LoadFromOperand(instr.operands[0]);
assert_not_zero(src); assert_not_zero(src);
@ -1023,13 +1029,15 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
auto texture_ptr = auto texture_ptr =
b.createAccessChain(spv::StorageClass::StorageClassUniformConstant, b.createAccessChain(spv::StorageClass::StorageClassUniformConstant,
tex_[dim_idx], std::vector<Id>({texture_index})); tex_[dim_idx], std::vector<Id>({texture_index}));
auto texture = b.createLoad(texture_ptr); auto image = b.createUnaryOp(spv::Op::OpImage, image_type,
auto image = b.createUnaryOp(spv::Op::OpImage, image_type, texture); b.createLoad(texture_ptr));
/*
switch (instr.dimension) { switch (instr.dimension) {
case TextureDimension::k1D: { case TextureDimension::k1D: {
auto size = auto size =
b.createUnaryOp(spv::Op::OpImageQuerySize, float_type_, image); b.createUnaryOp(spv::Op::OpImageQuerySize, uint_type_, image);
size = b.createUnaryOp(spv::Op::OpConvertUToF, float_type_, size);
auto tex_coord = b.createCompositeExtract(src, float_type_, 0); auto tex_coord = b.createCompositeExtract(src, float_type_, 0);
auto weight = auto weight =
@ -1043,7 +1051,9 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
case TextureDimension::k2D: { case TextureDimension::k2D: {
auto size = b.createUnaryOp(spv::Op::OpImageQuerySize, auto size = b.createUnaryOp(spv::Op::OpImageQuerySize,
vec2_float_type_, image); vec2_uint_type_, image);
size =
b.createUnaryOp(spv::Op::OpConvertUToF, vec2_float_type_, size);
auto tex_coord = b.createOp(spv::Op::OpVectorShuffle, auto tex_coord = b.createOp(spv::Op::OpVectorShuffle,
vec2_float_type_, {src, src, 0, 1}); vec2_float_type_, {src, src, 0, 1});
@ -1062,6 +1072,7 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
assert_always(); assert_always();
break; break;
} }
*/
} break; } break;
default: default:
@ -1286,7 +1297,7 @@ void SpirvShaderTranslator::ProcessVectorAluInstruction(
// TODO: If we have identical operands, reuse previous one. // TODO: If we have identical operands, reuse previous one.
Id sources[3] = {0}; Id sources[3] = {0};
Id dest = 0; Id dest = vec4_float_zero_;
for (size_t i = 0; i < instr.operand_count; i++) { for (size_t i = 0; i < instr.operand_count; i++) {
sources[i] = LoadFromOperand(instr.operands[i]); sources[i] = LoadFromOperand(instr.operands[i]);
} }
@ -1698,7 +1709,7 @@ void SpirvShaderTranslator::ProcessScalarAluInstruction(
// TODO: If we have identical operands, reuse previous one. // TODO: If we have identical operands, reuse previous one.
Id sources[3] = {0}; Id sources[3] = {0};
Id dest = 0; Id dest = b.makeFloatConstant(0);
for (size_t i = 0, x = 0; i < instr.operand_count; i++) { for (size_t i = 0, x = 0; i < instr.operand_count; i++) {
auto src = LoadFromOperand(instr.operands[i]); auto src = LoadFromOperand(instr.operands[i]);

View file

@ -117,9 +117,10 @@ class SpirvShaderTranslator : public ShaderTranslator {
// Types. // Types.
spv::Id float_type_ = 0, bool_type_ = 0, int_type_ = 0, uint_type_ = 0; spv::Id float_type_ = 0, bool_type_ = 0, int_type_ = 0, uint_type_ = 0;
spv::Id vec2_uint_type_ = 0;
spv::Id vec2_float_type_ = 0, vec3_float_type_ = 0, vec4_float_type_ = 0; spv::Id vec2_float_type_ = 0, vec3_float_type_ = 0, vec4_float_type_ = 0;
spv::Id vec4_uint_type_ = 0; spv::Id vec4_uint_type_ = 0;
spv::Id vec4_bool_type_ = 0; spv::Id vec2_bool_type_ = 0, vec3_bool_type_ = 0, vec4_bool_type_ = 0;
spv::Id image_1d_type_ = 0, image_2d_type_ = 0, image_3d_type_ = 0, spv::Id image_1d_type_ = 0, image_2d_type_ = 0, image_3d_type_ = 0,
image_cube_type_ = 0; image_cube_type_ = 0;