Skip to content

Commit

Permalink
Merge #3170
Browse files Browse the repository at this point in the history
3170: Support for NDC_Y_UP feature r=msiglreith a=kvark

Paves the way to gfx-rs/wgpu#519
PR checklist:
- [x] `make` succeeds (on *nix)
- [ ] `make reftests` succeeds
- [ ] tested examples with the following backends:
- [ ] `rustfmt` run on changed code

If I undetstand correctly, the only thing missing here is a proper check for supported device extensions in Vulkan backend.

Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
  • Loading branch information
bors[bot] and kvark authored Mar 22, 2020
2 parents bf1c536 + 07543a4 commit b945df3
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 72 deletions.
12 changes: 9 additions & 3 deletions src/backend/dx11/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ struct InputLayout {
pub struct Device {
raw: ComPtr<d3d11::ID3D11Device>,
pub(crate) context: ComPtr<d3d11::ID3D11DeviceContext>,
features: hal::Features,
memory_properties: MemoryProperties,
pub(crate) internal: internal::Internal,
}
Expand Down Expand Up @@ -120,10 +121,12 @@ impl Device {
device: ComPtr<d3d11::ID3D11Device>,
context: ComPtr<d3d11::ID3D11DeviceContext>,
memory_properties: MemoryProperties,
requested_features: hal::Features,
) -> Self {
Device {
raw: device.clone(),
context,
features: hal::Features::empty(),
memory_properties,
internal: internal::Internal::new(&device),
}
Expand Down Expand Up @@ -420,6 +423,7 @@ impl Device {
stage: pso::Stage,
source: &pso::EntryPoint<Backend>,
layout: &PipelineLayout,
features: &hal::Features,
) -> Result<Option<ComPtr<d3dcommon::ID3DBlob>>, device::ShaderError> {
// TODO: entrypoint stuff
match *source.module {
Expand All @@ -429,7 +433,7 @@ impl Device {
// Ok(Some(shader))
}
ShaderModule::Spirv(ref raw_data) => Ok(shader::compile_spirv_entrypoint(
raw_data, stage, source, layout,
raw_data, stage, source, layout, features,
)?),
}
}
Expand Down Expand Up @@ -904,13 +908,14 @@ impl device::Device<Backend> for Device {
desc: &pso::GraphicsPipelineDesc<'a, Backend>,
_cache: Option<&()>,
) -> Result<GraphicsPipeline, pso::CreationError> {
let features = &self.features;
let build_shader = |stage: pso::Stage, source: Option<&pso::EntryPoint<'a, Backend>>| {
let source = match source {
Some(src) => src,
None => return Ok(None),
};

Self::extract_entry_point(stage, source, desc.layout)
Self::extract_entry_point(stage, source, desc.layout, features)
.map_err(|err| pso::CreationError::Shader(err))
};

Expand Down Expand Up @@ -975,13 +980,14 @@ impl device::Device<Backend> for Device {
desc: &pso::ComputePipelineDesc<'a, Backend>,
_cache: Option<&()>,
) -> Result<ComputePipeline, pso::CreationError> {
let features = &self.features;
let build_shader = |stage: pso::Stage, source: Option<&pso::EntryPoint<'a, Backend>>| {
let source = match source {
Some(src) => src,
None => return Ok(None),
};

Self::extract_entry_point(stage, source, desc.layout)
Self::extract_entry_point(stage, source, desc.layout, features)
.map_err(|err| pso::CreationError::Shader(err))
};

Expand Down
11 changes: 9 additions & 2 deletions src/backend/dx11/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,13 @@ fn get_features(
_device: ComPtr<d3d11::ID3D11Device>,
_feature_level: d3dcommon::D3D_FEATURE_LEVEL,
) -> hal::Features {
hal::Features::ROBUST_BUFFER_ACCESS
hal::Features::empty()
| hal::Features::ROBUST_BUFFER_ACCESS
| hal::Features::FULL_DRAW_INDEX_U32
| hal::Features::FORMAT_BC
| hal::Features::INSTANCE_RATE
| hal::Features::SAMPLER_MIP_LOD_BIAS
| hal::Features::NDC_Y_UP
}

fn get_format_properties(
Expand Down Expand Up @@ -583,7 +585,12 @@ impl adapter::PhysicalDevice<Backend> for PhysicalDevice {
(ComPtr::from_raw(device), ComPtr::from_raw(cxt))
};

let device = device::Device::new(device, cxt, self.memory_properties.clone());
let device = device::Device::new(
device,
cxt,
self.memory_properties.clone(),
requested_features,
);

// TODO: deferred context => 1 cxt/queue?
let queue_groups = families
Expand Down
6 changes: 4 additions & 2 deletions src/backend/dx11/src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ pub(crate) fn compile_spirv_entrypoint(
stage: pso::Stage,
source: &pso::EntryPoint<Backend>,
layout: &PipelineLayout,
features: &hal::Features,
) -> Result<Option<ComPtr<d3dcommon::ID3DBlob>>, device::ShaderError> {
let mut ast = parse_spirv(raw_data)?;
spirv_cross_specialize_ast(&mut ast, &source.specialization)?;

patch_spirv_resources(&mut ast, stage, layout)?;
let shader_model = hlsl::ShaderModel::V5_0;
let shader_code = translate_spirv(&mut ast, shader_model, layout, stage)?;
let shader_code = translate_spirv(&mut ast, shader_model, layout, stage, features)?;
log::debug!(
"Generated {:?} shader:\n{:?}",
stage,
Expand Down Expand Up @@ -251,10 +252,11 @@ fn translate_spirv(
shader_model: hlsl::ShaderModel,
_layout: &PipelineLayout,
_stage: pso::Stage,
features: &hal::Features,
) -> Result<String, device::ShaderError> {
let mut compile_options = hlsl::CompilerOptions::default();
compile_options.shader_model = shader_model;
compile_options.vertex.invert_y = true;
compile_options.vertex.invert_y = !features.contains(hal::Features::NDC_Y_UP);

//let stage_flag = stage.into();

Expand Down
11 changes: 7 additions & 4 deletions src/backend/dx12/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,11 @@ impl Device {
shader_model: hlsl::ShaderModel,
layout: &r::PipelineLayout,
stage: pso::Stage,
features: &hal::Features,
) -> Result<String, d::ShaderError> {
let mut compile_options = hlsl::CompilerOptions::default();
compile_options.shader_model = shader_model;
compile_options.vertex.invert_y = true;
compile_options.vertex.invert_y = !features.contains(hal::Features::NDC_Y_UP);

let stage_flag = stage.into();
let root_constant_layout = layout
Expand Down Expand Up @@ -424,6 +425,7 @@ impl Device {
stage: pso::Stage,
source: &pso::EntryPoint<B>,
layout: &r::PipelineLayout,
features: &hal::Features,
) -> Result<(native::Blob, bool), d::ShaderError> {
match *source.module {
r::ShaderModule::Compiled(ref shaders) => {
Expand All @@ -440,7 +442,7 @@ impl Device {
Self::patch_spirv_resources(&mut ast, Some(layout))?;

let shader_model = hlsl::ShaderModel::V5_1;
let shader_code = Self::translate_spirv(&mut ast, shader_model, layout, stage)?;
let shader_code = Self::translate_spirv(&mut ast, shader_model, layout, stage, features)?;
debug!("SPIRV-Cross generated shader:\n{}", shader_code);

let real_name = ast
Expand Down Expand Up @@ -1729,6 +1731,7 @@ impl d::Device<B> for Device {
Borrowed(native::Blob),
None,
}
let features = &self.features;
impl ShaderBc {
pub fn shader(&self) -> native::Shader {
match *self {
Expand All @@ -1746,7 +1749,7 @@ impl d::Device<B> for Device {
None => return Ok(ShaderBc::None),
};

match Self::extract_entry_point(stage, source, desc.layout) {
match Self::extract_entry_point(stage, source, desc.layout, features) {
Ok((shader, true)) => Ok(ShaderBc::Owned(shader)),
Ok((shader, false)) => Ok(ShaderBc::Borrowed(shader)),
Err(err) => Err(pso::CreationError::Shader(err)),
Expand Down Expand Up @@ -2006,7 +2009,7 @@ impl d::Device<B> for Device {
_cache: Option<&()>,
) -> Result<r::ComputePipeline, pso::CreationError> {
let (cs, cs_destroy) =
Self::extract_entry_point(pso::Stage::Compute, &desc.shader, desc.layout)
Self::extract_entry_point(pso::Stage::Compute, &desc.shader, desc.layout, &self.features)
.map_err(|err| pso::CreationError::Shader(err))?;

let (pipeline, hr) = self.raw.create_compute_pipeline_state(
Expand Down
6 changes: 5 additions & 1 deletion src/backend/dx12/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ impl adapter::PhysicalDevice<Backend> for PhysicalDevice {
}

let mut device = Device::new(device_raw, &self, present_queue);
device.features = requested_features;

let queue_groups = families
.into_iter()
Expand Down Expand Up @@ -557,6 +558,7 @@ pub struct Device {
raw: native::Device,
library: Arc<native::D3D12Lib>,
private_caps: Capabilities,
features: Features,
format_properties: Arc<FormatProperties>,
heap_properties: &'static [HeapProperties],
// CPU only pools
Expand Down Expand Up @@ -637,6 +639,7 @@ impl Device {
raw: device,
library: Arc::clone(&physical_device.library),
private_caps: physical_device.private_caps,
features: Features::empty(),
format_properties: physical_device.format_properties.clone(),
heap_properties: physical_device.heap_properties,
rtv_pool: Mutex::new(rtv_pool),
Expand Down Expand Up @@ -1072,7 +1075,8 @@ impl hal::Instance<Backend> for Instance {
Features::FORMAT_BC |
Features::INSTANCE_RATE |
Features::SAMPLER_MIP_LOD_BIAS |
Features::SAMPLER_ANISOTROPY,
Features::SAMPLER_ANISOTROPY |
Features::NDC_Y_UP,
hints:
Hints::BASE_VERTEX_INSTANCE_DRAWING,
limits: Limits { // TODO
Expand Down
28 changes: 12 additions & 16 deletions src/backend/gl/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fn create_fbo_internal(
#[derive(Debug)]
pub struct Device {
pub(crate) share: Starc<Share>,
features: hal::Features,
}

impl Drop for Device {
Expand All @@ -79,8 +80,8 @@ impl Drop for Device {

impl Device {
/// Create a new `Device`.
pub(crate) fn new(share: Starc<Share>) -> Self {
Device { share: share }
pub(crate) fn new(share: Starc<Share>, features: hal::Features,) -> Self {
Device { share: share, features }
}

pub fn create_shader_module_from_source(
Expand Down Expand Up @@ -220,7 +221,7 @@ impl Device {
other => panic!("GLSL version is not recognized: {:?}", other),
}
};
compile_options.vertex.invert_y = true;
compile_options.vertex.invert_y = !self.features.contains(hal::Features::NDC_Y_UP);
debug!("SPIR-V options {:?}", compile_options);

ast.set_compiler_options(&compile_options)
Expand Down Expand Up @@ -403,8 +404,9 @@ impl Device {
}

pub(crate) unsafe fn set_sampler_info<SetParamFloat, SetParamFloatVec, SetParamInt>(
share: &Starc<Share>,
info: &i::SamplerDesc,
features: &hal::Features,
legacy_features: &LegacyFeatures,
mut set_param_float: SetParamFloat,
mut set_param_float_vec: SetParamFloatVec,
mut set_param_int: SetParamInt,
Expand All @@ -416,12 +418,8 @@ pub(crate) unsafe fn set_sampler_info<SetParamFloat, SetParamFloatVec, SetParamI
{
let (min, mag) = conv::filter_to_gl(info.mag_filter, info.min_filter, info.mip_filter);
match info.anisotropic {
i::Anisotropic::On(fac) if fac > 1 => {
if share.private_caps.sampler_anisotropy_ext {
set_param_float(glow::TEXTURE_MAX_ANISOTROPY, fac as f32);
} else if share.features.contains(hal::Features::SAMPLER_ANISOTROPY) {
set_param_float(glow::TEXTURE_MAX_ANISOTROPY, fac as f32);
}
i::Anisotropic::On(fac) if fac > 1 && features.contains(hal::Features::SAMPLER_ANISOTROPY) => {
set_param_float(glow::TEXTURE_MAX_ANISOTROPY, fac as f32);
}
_ => (),
}
Expand All @@ -434,13 +432,10 @@ pub(crate) unsafe fn set_sampler_info<SetParamFloat, SetParamFloatVec, SetParamI
set_param_int(glow::TEXTURE_WRAP_T, conv::wrap_to_gl(t) as i32);
set_param_int(glow::TEXTURE_WRAP_R, conv::wrap_to_gl(r) as i32);

if share.features.contains(hal::Features::SAMPLER_MIP_LOD_BIAS) {
if features.contains(hal::Features::SAMPLER_MIP_LOD_BIAS) {
set_param_float(glow::TEXTURE_LOD_BIAS, info.lod_bias.0);
}
if share
.legacy_features
.contains(LegacyFeatures::SAMPLER_BORDER_COLOR)
{
if legacy_features.contains(LegacyFeatures::SAMPLER_BORDER_COLOR) {
let mut border: [f32; 4] = info.border.into();
set_param_float_vec(glow::TEXTURE_BORDER_COLOR, &mut border);
}
Expand Down Expand Up @@ -1083,8 +1078,9 @@ impl d::Device<B> for Device {

let name = gl.create_sampler().unwrap();
set_sampler_info(
&self.share,
&info,
&self.features,
&self.share.legacy_features,
|a, b| gl.sampler_parameter_f32(name, a, b),
|a, b| gl.sampler_parameter_f32_slice(name, a, b),
|a, b| gl.sampler_parameter_i32(name, a, b),
Expand Down
19 changes: 4 additions & 15 deletions src/backend/gl/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,10 @@ pub struct PrivateCaps {
/// - In OpenGL ES 2 it may be available behind optional extensions
/// - In WebGL 1 and WebGL 2 it is never available
pub emulate_map: bool,
/// Indicates if we only have support via the EXT.
pub sampler_anisotropy_ext: bool,
/// Whether f64 precision is supported for depth ranges
pub depth_range_f64_precision: bool,
/// Whether draw buffers are supported
pub draw_buffers: bool,
/// Whether or not glColorMaski / glBlendEquationi / glBlendFunci are available
pub per_draw_buffer_blending: bool,
}

/// OpenGL implementation information
Expand Down Expand Up @@ -400,7 +396,7 @@ pub(crate) fn query_all(gl: &GlContainer) -> (Info, Features, LegacyFeatures, Hi
}
}

let mut features = Features::empty();
let mut features = Features::NDC_Y_UP;
let mut legacy = LegacyFeatures::empty();

if info.is_supported(&[
Expand All @@ -420,6 +416,9 @@ pub(crate) fn query_all(gl: &GlContainer) -> (Info, Features, LegacyFeatures, Hi
// TODO: extension
features |= Features::SAMPLER_MIP_LOD_BIAS;
}
if info.is_supported(&[Core(4, 0), Es(3, 2), Ext("GL_EXT_draw_buffers2")]) && !info.is_webgl() {
features |= Features::INDEPENDENT_BLENDING;
}

// TODO
if false && info.is_supported(&[Core(4, 3), Es(3, 1)]) {
Expand Down Expand Up @@ -480,12 +479,6 @@ pub(crate) fn query_all(gl: &GlContainer) -> (Info, Features, LegacyFeatures, Hi
legacy |= LegacyFeatures::INSTANCED_ATTRIBUTE_BINDING;
}

let per_draw_buffer_blending =
info.is_supported(&[Core(4, 0), Es(3, 2), Ext("GL_EXT_draw_buffers2")]) && !info.is_webgl();
if per_draw_buffer_blending {
features |= Features::INDEPENDENT_BLENDING;
}

let mut hints = Hints::empty();
if info.is_supported(&[Core(4, 2)]) {
// TODO: extension
Expand All @@ -508,13 +501,9 @@ pub(crate) fn query_all(gl: &GlContainer) -> (Info, Features, LegacyFeatures, Hi
frag_data_location: !info.version.is_embedded,
sync: !info.is_webgl() && info.is_supported(&[Core(3, 2), Es(3, 0), Ext("GL_ARB_sync")]), // TODO
map: !info.version.is_embedded, //TODO: OES extension
sampler_anisotropy_ext: !info
.is_supported(&[Core(4, 6), Ext("GL_ARB_texture_filter_anisotropic")])
&& info.is_supported(&[Ext("GL_EXT_texture_filter_anisotropic")]),
emulate_map, // TODO
depth_range_f64_precision: !info.version.is_embedded, // TODO
draw_buffers: info.is_supported(&[Core(2, 0), Es(3, 0)]),
per_draw_buffer_blending,
};

(info, features, legacy, hints, limits, private)
Expand Down
Loading

0 comments on commit b945df3

Please sign in to comment.