Skip to content

Commit

Permalink
Enable PrimitiveCulling capability if enabled.
Browse files Browse the repository at this point in the history
For DXR 1.1, this capability must be enabled in case the shader uses ray
flags 0x100 or 0x200.
  • Loading branch information
HansKristian-Work committed Oct 11, 2021
1 parent 62595e3 commit 447723b
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 2 deletions.
8 changes: 8 additions & 0 deletions dxil_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4286,6 +4286,8 @@ bool Converter::Impl::emit_execution_modes_ray_tracing(spv::ExecutionModel model
{
auto &builder = spirv_module.get_builder();
builder.addCapability(spv::CapabilityRayTracingKHR);
if (options.ray_tracing_primitive_culling_enabled && shader_analysis.can_require_primitive_culling)
builder.addCapability(spv::CapabilityRayTraversalPrimitiveCullingKHR);
builder.addExtension("SPV_KHR_ray_tracing");
builder.addExtension("SPV_EXT_descriptor_indexing");

Expand Down Expand Up @@ -4982,6 +4984,11 @@ void Converter::Impl::set_option(const OptionBase &cap)
options.shader_i8_dot_enabled = static_cast<const OptionShaderI8Dot &>(cap).supported;
break;

case Option::ShaderRayTracingPrimitiveCulling:
options.ray_tracing_primitive_culling_enabled =
static_cast<const OptionShaderRayTracingPrimitiveCulling &>(cap).supported;
break;

default:
break;
}
Expand Down Expand Up @@ -5029,6 +5036,7 @@ bool Converter::recognizes_option(Option cap)
case Option::DescriptorQA:
case Option::MinPrecisionNative16Bit:
case Option::ShaderI8Dot:
case Option::ShaderRayTracingPrimitiveCulling:
return true;

default:
Expand Down
13 changes: 12 additions & 1 deletion dxil_converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ enum class Option : uint32_t
StorageInputOutput16 = 14,
DescriptorQA = 15,
MinPrecisionNative16Bit = 16,
ShaderI8Dot = 17
ShaderI8Dot = 17,
ShaderRayTracingPrimitiveCulling = 18
};

enum class ResourceClass : uint32_t
Expand Down Expand Up @@ -402,6 +403,16 @@ struct OptionShaderI8Dot : OptionBase
bool supported = false;
};

struct OptionShaderRayTracingPrimitiveCulling : OptionBase
{
OptionShaderRayTracingPrimitiveCulling()
: OptionBase(Option::ShaderRayTracingPrimitiveCulling)
{
}

bool supported = false;
};

struct DescriptorTableEntry
{
ResourceClass type;
Expand Down
7 changes: 7 additions & 0 deletions dxil_spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,13 @@ int main(int argc, char **argv)
dxil_spv_converter_add_option(converter, &helper.base);
}

{
const dxil_spv_option_shader_ray_tracing_primitive_culling helper =
{ { DXIL_SPV_OPTION_SHADER_RAY_TRACING_PRIMITIVE_CULLING },
DXIL_SPV_TRUE };
dxil_spv_converter_add_option(converter, &helper.base);
}

if (args.dual_source_blending)
{
const dxil_spv_option_dual_source_blending helper = { { DXIL_SPV_OPTION_DUAL_SOURCE_BLENDING }, DXIL_SPV_TRUE };
Expand Down
9 changes: 9 additions & 0 deletions dxil_spirv_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,15 @@ dxil_spv_result dxil_spv_converter_add_option(dxil_spv_converter converter, cons
break;
}

case DXIL_SPV_OPTION_SHADER_RAY_TRACING_PRIMITIVE_CULLING:
{
OptionShaderRayTracingPrimitiveCulling helper;
helper.supported = bool(reinterpret_cast<const dxil_spv_option_shader_ray_tracing_primitive_culling *>(option)->supported);

converter->options.emplace_back(duplicate(helper));
break;
}

default:
return DXIL_SPV_ERROR_UNSUPPORTED_FEATURE;
}
Expand Down
9 changes: 8 additions & 1 deletion dxil_spirv_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern "C" {
#endif

#define DXIL_SPV_API_VERSION_MAJOR 2
#define DXIL_SPV_API_VERSION_MINOR 11
#define DXIL_SPV_API_VERSION_MINOR 12
#define DXIL_SPV_API_VERSION_PATCH 0

#define DXIL_SPV_DESCRIPTOR_QA_INTERFACE_VERSION 1
Expand Down Expand Up @@ -278,6 +278,7 @@ typedef enum dxil_spv_option
DXIL_SPV_OPTION_DESCRIPTOR_QA = 15,
DXIL_SPV_OPTION_MIN_PRECISION_NATIVE_16BIT = 16,
DXIL_SPV_OPTION_SHADER_I8_DOT = 17,
DXIL_SPV_OPTION_SHADER_RAY_TRACING_PRIMITIVE_CULLING = 18,
DXIL_SPV_OPTION_INT_MAX = 0x7fffffff
} dxil_spv_option;

Expand Down Expand Up @@ -415,6 +416,12 @@ typedef struct dxil_spv_option_shader_i8_dot
dxil_spv_bool supported;
} dxil_spv_option_shader_i8_dot;

typedef struct dxil_spv_option_shader_ray_tracing_primitive_culling
{
dxil_spv_option_base base;
dxil_spv_bool supported;
} dxil_spv_option_shader_ray_tracing_primitive_culling;

/* Gets the ABI version used to build this library. Used to detect API/ABI mismatches. */
DXIL_SPV_PUBLIC_API void dxil_spv_get_version(unsigned *major, unsigned *minor, unsigned *patch);

Expand Down
2 changes: 2 additions & 0 deletions opcodes/converter_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ struct Converter::Impl
bool descriptor_qa_sink_handles = true;
bool min_precision_prefer_native_16bit = false;
bool shader_i8_dot_enabled = false;
bool ray_tracing_primitive_culling_enabled = false;
} options;

struct BindlessInfo
Expand Down Expand Up @@ -462,6 +463,7 @@ struct Converter::Impl
// Only relevant for fragment shaders.
bool has_side_effects = false;
bool discards = false;
bool can_require_primitive_culling = false;
} shader_analysis;

// For descriptor QA, we need to rewrite how resource handles are emitted.
Expand Down
15 changes: 15 additions & 0 deletions opcodes/opcodes_dxil_builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,21 @@ bool analyze_dxil_instruction(Converter::Impl &impl, const llvm::CallInst *instr
impl.handle_to_storage_class[alloca_inst] = spv::StorageClassRayPayloadKHR;
}
}

if (const auto *flags_inst = llvm::dyn_cast<llvm::ConstantInt>(instruction->getOperand(2)))
{
if ((flags_inst->getUniqueInteger().getZExtValue() & (spv::RayFlagsSkipTrianglesKHRMask |
spv::RayFlagsSkipAABBsKHRMask)) != 0)
{
impl.shader_analysis.can_require_primitive_culling = true;
}
}
else
{
// Non constant flags, so we must be conservative.
impl.shader_analysis.can_require_primitive_culling = true;
}

break;
}

Expand Down
128 changes: 128 additions & 0 deletions reference/shaders/dxil-builtin/trace-ray-flags-2.rgen

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions reference/shaders/dxil-builtin/trace-ray-flags.rgen

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 447723b

Please sign in to comment.