Skip to content

Commit

Permalink
Merge pull request KhronosGroup#721 from KhronosGroup/fix-720
Browse files Browse the repository at this point in the history
Refactor MSL to use SPIRCombinedImageSampler.
  • Loading branch information
HansKristian-Work authored Oct 5, 2018
2 parents 519565b + a697299 commit d2928bd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
2 changes: 0 additions & 2 deletions spirv_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1236,8 +1236,6 @@ struct Meta

Decoration decoration;
std::vector<Decoration> members;
uint32_t sampler = 0;
uint32_t image = 0;

std::unordered_map<uint32_t, uint32_t> decoration_word_offset;

Expand Down
15 changes: 13 additions & 2 deletions spirv_hlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3933,10 +3933,21 @@ void CompilerHLSL::emit_instruction(const Instruction &instruction)
uint32_t result_type = ops[0];
uint32_t id = ops[1];
auto *combined = maybe_get<SPIRCombinedImageSampler>(ops[2]);

if (combined)
emit_op(result_type, id, to_expression(combined->image), true, true);
{
auto &e = emit_op(result_type, id, to_expression(combined->image), true, true);
auto *var = maybe_get_backing_variable(combined->image);
if (var)
e.loaded_from = var->self;
}
else
emit_op(result_type, id, to_expression(ops[2]), true, true);
{
auto &e = emit_op(result_type, id, to_expression(ops[2]), true, true);
auto *var = maybe_get_backing_variable(ops[2]);
if (var)
e.loaded_from = var->self;
}
break;
}

Expand Down
50 changes: 32 additions & 18 deletions spirv_msl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2345,18 +2345,23 @@ void CompilerMSL::emit_instruction(const Instruction &instruction)

case OpImage:
{
uint32_t result_type = ops[0];
uint32_t id = ops[1];
uint32_t arg = ops[2];
auto *combined = maybe_get<SPIRCombinedImageSampler>(ops[2]);

CompilerGLSL::emit_instruction(instruction);
auto &expr = get<SPIRExpression>(id);
if (expr.loaded_from == 0)
if (combined)
{
auto &e = emit_op(result_type, id, to_expression(combined->image), true, true);
auto *var = maybe_get_backing_variable(combined->image);
if (var)
e.loaded_from = var->self;
}
else
{
// This means the operand is the result of an OpSampledImage.
// In that case, we need to get the original image from the
// OpSampledImage.
auto *var = maybe_get_backing_variable(meta[arg].image);
expr.loaded_from = var ? var->self : 0;
auto &e = emit_op(result_type, id, to_expression(ops[2]), true, true);
auto *var = maybe_get_backing_variable(ops[2]);
if (var)
e.loaded_from = var->self;
}
break;
}
Expand Down Expand Up @@ -2939,8 +2944,10 @@ string CompilerMSL::to_function_name(uint32_t img, const SPIRType &imgtype, bool
return fname;
}

auto *combined = maybe_get<SPIRCombinedImageSampler>(img);

// Texture reference
string fname = to_expression(img) + ".";
string fname = to_expression(combined ? combined->image : img) + ".";
if (msl_options.swizzle_texture_samples && !is_gather && is_sampled_image_type(imgtype))
fname = "spvTextureSwizzle(" + fname;

Expand Down Expand Up @@ -2972,7 +2979,9 @@ string CompilerMSL::to_function_args(uint32_t img, const SPIRType &imgtype, bool
{
if (!farg_str.empty())
farg_str += ", ";
farg_str += to_expression(img);

auto *combined = maybe_get<SPIRCombinedImageSampler>(img);
farg_str += to_expression(combined ? combined->image : img);
}

// Texture coordinates
Expand Down Expand Up @@ -3230,8 +3239,11 @@ string CompilerMSL::to_function_args(uint32_t img, const SPIRType &imgtype, bool
farg_str += ")";
// Get the original input variable for this image.
uint32_t img_var = img;
if (meta[img].image)
img_var = meta[img].image;

auto *combined = maybe_get<SPIRCombinedImageSampler>(img_var);
if (combined)
img_var = combined->image;

if (auto *var = maybe_get_backing_variable(img_var))
{
if (var->parameter && !var->parameter->alias_global_variable)
Expand Down Expand Up @@ -3287,9 +3299,7 @@ string CompilerMSL::to_component_argument(uint32_t id)
// Establish sampled image as expression object and assign the sampler to it.
void CompilerMSL::emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id)
{
set<SPIRExpression>(result_id, to_expression(image_id), result_type, true);
meta[result_id].sampler = samp_id;
meta[result_id].image = image_id;
set<SPIRCombinedImageSampler>(result_id, result_type, image_id, samp_id);
}

// Returns a string representation of the ID, usable as a function arg.
Expand All @@ -3311,9 +3321,13 @@ string CompilerMSL::to_func_call_arg(uint32_t id)
// by appending a suffix to the expression constructed from the ID.
string CompilerMSL::to_sampler_expression(uint32_t id)
{
auto expr = to_expression(id);
auto *combined = maybe_get<SPIRCombinedImageSampler>(id);
auto expr = to_expression(combined ? combined->image : id);
auto index = expr.find_first_of('[');
uint32_t samp_id = meta[id].sampler;

uint32_t samp_id = 0;
if (combined)
samp_id = combined->sampler;

if (index == string::npos)
return samp_id ? to_expression(samp_id) : expr + sampler_name_suffix;
Expand Down

0 comments on commit d2928bd

Please sign in to comment.