Skip to content

Commit

Permalink
Merge #2944
Browse files Browse the repository at this point in the history
2944: Dynamic PSO states refactor r=grovesNL a=kvark

Fixes #2905
PR checklist:
- [x] `make` succeeds (on *nix)
- [x] `make reftests` succeeds
- [ ] tested examples with the following backends:
- [ ] `rustfmt` run on changed code


Co-authored-by: Dzmitry Malyshau <dmalyshau@mozilla.com>
  • Loading branch information
bors[bot] and kvark committed Aug 8, 2019
2 parents c7b4f28 + e6c4169 commit bcfb6d7
Show file tree
Hide file tree
Showing 27 changed files with 397 additions and 405 deletions.
8 changes: 4 additions & 4 deletions examples/colour-uniform/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,10 +1399,10 @@ impl<B: Backend> PipelineState<B> {
&pipeline_layout,
subpass,
);
pipeline_desc.blender.targets.push(pso::ColorBlendDesc(
pso::ColorMask::ALL,
pso::BlendState::ALPHA,
));
pipeline_desc.blender.targets.push(pso::ColorBlendDesc {
mask: pso::ColorMask::ALL,
blend: Some(pso::BlendState::ALPHA),
});
pipeline_desc.vertex_buffers.push(pso::VertexBufferDesc {
binding: 0,
stride: size_of::<Vertex>() as u32,
Expand Down
8 changes: 4 additions & 4 deletions examples/quad/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,10 @@ fn main() {
&pipeline_layout,
subpass,
);
pipeline_desc.blender.targets.push(pso::ColorBlendDesc(
pso::ColorMask::ALL,
pso::BlendState::ALPHA,
));
pipeline_desc.blender.targets.push(pso::ColorBlendDesc {
mask: pso::ColorMask::ALL,
blend: Some(pso::BlendState::ALPHA),
});
pipeline_desc.vertex_buffers.push(pso::VertexBufferDesc {
binding: 0,
stride: std::mem::size_of::<Vertex>() as u32,
Expand Down
4 changes: 2 additions & 2 deletions reftests/scenes/basic.ron
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
kind: D2(1, 1, 1, 1),
num_levels: 1,
format: Rgba8Unorm,
usage: (bits: 0x14), //COLOR_ATTACHMENT | SAMPLED (temporary for GL)
usage: (bits: 0x15), //COLOR_ATTACHMENT | TRANSFER_SRC (for reading) | SAMPLED (temporary for GL)
),
"pass": RenderPass(
attachments: {
Expand Down Expand Up @@ -71,7 +71,7 @@
alpha_coverage: false,
logic_op: None,
targets: [
((bits: 15), Off),
(mask: (bits: 15), blend: None),
],
),
layout: "pipe-layout",
Expand Down
6 changes: 3 additions & 3 deletions reftests/scenes/vertex-offset.ron
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
kind: D2(1, 1, 1, 1),
num_levels: 1,
format: Rgba32Uint,
usage: (bits: 0x14), //COLOR_ATTACHMENT | SAMPLED (temporary for GL)
usage: (bits: 0x15), //COLOR_ATTACHMENT | TRANSFER_SRC (for reading back) | SAMPLED (temporary for GL)
),
"pass": RenderPass(
attachments: {
Expand Down Expand Up @@ -93,7 +93,7 @@
alpha_coverage: false,
logic_op: None,
targets: [
((bits: 15), Off),
(mask: (bits: 15), blend: None),
],
),
layout: "pipe-layout",
Expand Down Expand Up @@ -140,7 +140,7 @@
alpha_coverage: false,
logic_op: None,
targets: [
((bits: 15), Off),
(mask: (bits: 15), blend: None),
],
),
layout: "pipe-layout",
Expand Down
63 changes: 32 additions & 31 deletions src/backend/dx11/src/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ use hal::image::{Anisotropic, Filter, WrapMode};
use hal::pso::{
BlendDesc,
BlendOp,
BlendState,
ColorBlendDesc,
Comparison,
DepthBias,
DepthStencilDesc,
DepthTest,
Face,
Factor,
FrontFace,
PolygonMode,
Rasterizer,
Rect,
Sided,
Stage,
State,
StencilFace,
StencilOp,
StencilTest,
StencilValue,
Viewport,
};
Expand Down Expand Up @@ -624,13 +622,13 @@ fn map_blend_targets(
) -> [D3D11_RENDER_TARGET_BLEND_DESC; 8] {
let mut targets: [D3D11_RENDER_TARGET_BLEND_DESC; 8] = [unsafe { mem::zeroed() }; 8];

for (mut target, &ColorBlendDesc(mask, blend)) in
for (mut target, color_desc) in
targets.iter_mut().zip(render_target_blends.iter())
{
target.RenderTargetWriteMask = mask.bits() as _;
if let BlendState::On { color, alpha } = blend {
let (color_op, color_src, color_dst) = map_blend_op(color);
let (alpha_op, alpha_src, alpha_dst) = map_alpha_blend_op(alpha);
target.RenderTargetWriteMask = color_desc.mask.bits() as _;
if let Some(ref blend) = color_desc.blend {
let (color_op, color_src, color_dst) = map_blend_op(blend.color);
let (alpha_op, alpha_src, alpha_dst) = map_alpha_blend_op(blend.alpha);
target.BlendEnable = TRUE;
target.BlendOp = color_op;
target.SrcBlend = color_src;
Expand Down Expand Up @@ -692,32 +690,41 @@ pub(crate) fn map_depth_stencil_desc(
desc: &DepthStencilDesc,
) -> (D3D11_DEPTH_STENCIL_DESC, State<StencilValue>) {
let (depth_on, depth_write, depth_func) = match desc.depth {
DepthTest::On { fun, write } => (TRUE, write, map_comparison(fun)),
DepthTest::Off => unsafe { mem::zeroed() },
Some(ref depth) => (TRUE, depth.write, map_comparison(depth.fun)),
None => unsafe { mem::zeroed() },
};

let (stencil_on, front, back, read_mask, write_mask, stencil_ref) = match desc.stencil {
StencilTest::On {
ref front,
ref back,
} => {
Some(ref stencil) => {
let read_masks = stencil.read_masks.static_or(Sided::new(!0));
let write_masks = stencil.read_masks.static_or(Sided::new(!0));
let reference_value = match stencil.reference_values {
State::Static(ref values) => {
if values.front != values.back {
error!("Different reference values for front ({}) and back ({}) of the stencil",
values.front, values.back);
}
State::Static(values.front)
}
State::Dynamic => State::Dynamic,
};
// TODO: cascade to create_pipeline
if front.mask_read != back.mask_read || front.mask_write != back.mask_write {
if read_masks.front != read_masks.back || write_masks.front != write_masks.back {
error!(
"Different masks on stencil front ({:?}) and back ({:?}) are not supported",
front, back
"Different sides are specified for read ({:?} and write ({:?}) stencil masks",
read_masks, write_masks
);
}
(
TRUE,
map_stencil_side(front),
map_stencil_side(back),
front.mask_read,
front.mask_write,
front.reference,
map_stencil_side(&stencil.faces.front),
map_stencil_side(&stencil.faces.back),
read_masks.front,
write_masks.front,
reference_value,
)
}
StencilTest::Off => unsafe { mem::zeroed() },
None => unsafe { mem::zeroed() },
};

(
Expand All @@ -730,14 +737,8 @@ pub(crate) fn map_depth_stencil_desc(
},
DepthFunc: depth_func,
StencilEnable: stencil_on,
StencilReadMask: match read_mask {
State::Static(rm) => rm as _,
State::Dynamic => !0,
},
StencilWriteMask: match write_mask {
State::Static(wm) => wm as _,
State::Dynamic => !0,
},
StencilReadMask: read_mask as _,
StencilWriteMask: write_mask as _,
FrontFace: front,
BackFace: back,
},
Expand Down
5 changes: 1 addition & 4 deletions src/backend/dx11/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,12 @@ impl Instance {
fn get_features(
_device: ComPtr<d3d11::ID3D11Device>,
_feature_level: d3dcommon::D3D_FEATURE_LEVEL,
) -> hal::Features {
use hal::Features;

) -> Features {
let features = Features::ROBUST_BUFFER_ACCESS
| Features::FULL_DRAW_INDEX_U32
| Features::FORMAT_BC
| Features::INSTANCE_RATE
| Features::SAMPLER_MIP_LOD_BIAS;

features
}

Expand Down
2 changes: 1 addition & 1 deletion src/backend/dx11/src/shader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ffi, mem, ptr, slice};
use std::{ffi, ptr, slice};

use spirv_cross::{hlsl, spirv, ErrorCode as SpirvErrorCode};

Expand Down
51 changes: 23 additions & 28 deletions src/backend/dx12/src/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ pub fn map_rasterizer(rasterizer: &pso::Rasterizer) -> D3D12_RASTERIZER_DESC {
D3D12_FILL_MODE_WIREFRAME
}
Line(width) => {
validate_line_width(width);
if let pso::State::Static(w) = width {
validate_line_width(w);
}
D3D12_FILL_MODE_WIREFRAME
}
Fill => D3D12_FILL_MODE_SOLID,
Expand Down Expand Up @@ -302,12 +304,12 @@ pub fn map_render_targets(
};
let mut targets = [dummy_target; 8];

for (target, &pso::ColorBlendDesc(mask, blend)) in targets.iter_mut().zip(color_targets.iter())
for (target, color_desc) in targets.iter_mut().zip(color_targets.iter())
{
target.RenderTargetWriteMask = mask.bits() as UINT8;
if let pso::BlendState::On { color, alpha } = blend {
let (color_op, color_src, color_dst) = map_blend_op(color);
let (alpha_op, alpha_src, alpha_dst) = map_blend_op(alpha);
target.RenderTargetWriteMask = color_desc.mask.bits() as UINT8;
if let Some(ref blend) = color_desc.blend {
let (color_op, color_src, color_dst) = map_blend_op(blend.color);
let (alpha_op, alpha_src, alpha_dst) = map_blend_op(blend.alpha);
target.BlendEnable = TRUE;
target.BlendOp = color_op;
target.SrcBlend = color_src;
Expand All @@ -323,30 +325,29 @@ pub fn map_render_targets(

pub fn map_depth_stencil(dsi: &pso::DepthStencilDesc) -> D3D12_DEPTH_STENCIL_DESC {
let (depth_on, depth_write, depth_func) = match dsi.depth {
pso::DepthTest::On { fun, write } => (TRUE, write, map_comparison(fun)),
pso::DepthTest::Off => unsafe { mem::zeroed() },
Some(ref depth) => (TRUE, depth.write, map_comparison(depth.fun)),
None => unsafe { mem::zeroed() },
};

let (stencil_on, front, back, read_mask, write_mask) = match dsi.stencil {
pso::StencilTest::On {
ref front,
ref back,
} => {
if front.mask_read != back.mask_read || front.mask_write != back.mask_write {
Some(ref stencil) => {
let read_masks = stencil.read_masks.static_or(pso::Sided::new(!0));
let write_masks = stencil.write_masks.static_or(pso::Sided::new(!0));
if read_masks.front != read_masks.back || write_masks.front != write_masks.back {
error!(
"Different masks on stencil front ({:?}) and back ({:?}) are not supported",
front, back
"Different sides are specified for read ({:?} and write ({:?}) stencil masks",
read_masks, write_masks
);
}
(
TRUE,
map_stencil_side(front),
map_stencil_side(back),
front.mask_read,
front.mask_write,
map_stencil_side(&stencil.faces.front),
map_stencil_side(&stencil.faces.back),
read_masks.front,
write_masks.front,
)
}
pso::StencilTest::Off => unsafe { mem::zeroed() },
None => unsafe { mem::zeroed() },
};

D3D12_DEPTH_STENCIL_DESC {
Expand All @@ -358,14 +359,8 @@ pub fn map_depth_stencil(dsi: &pso::DepthStencilDesc) -> D3D12_DEPTH_STENCIL_DES
},
DepthFunc: depth_func,
StencilEnable: stencil_on,
StencilReadMask: match read_mask {
pso::State::Static(rm) => rm as _,
pso::State::Dynamic => !0,
},
StencilWriteMask: match write_mask {
pso::State::Static(wm) => wm as _,
pso::State::Dynamic => !0,
},
StencilReadMask: read_mask as _,
StencilWriteMask: write_mask as _,
FrontFace: front,
BackFace: back,
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/gl/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub enum Command {
rasterizer: pso::Rasterizer,
},
BindDepth {
depth: pso::DepthTest,
depth: Option<pso::DepthTest>,
},
SetViewports {
first_viewport: u32,
Expand Down
2 changes: 1 addition & 1 deletion src/backend/gl/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub struct GraphicsPipeline {
pub(crate) vertex_buffers: Vec<Option<pso::VertexBufferDesc>>,
pub(crate) uniforms: Vec<UniformDesc>,
pub(crate) rasterizer: pso::Rasterizer,
pub(crate) depth: pso::DepthTest,
pub(crate) depth: Option<pso::DepthTest>,
}

#[derive(Clone, Debug)]
Expand Down
12 changes: 7 additions & 5 deletions src/backend/gl/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,9 @@ impl CommandQueue {
let (gl_draw, gl_offset) = match rasterizer.polygon_mode {
Point => (glow::POINT, glow::POLYGON_OFFSET_POINT),
Line(width) => {
unsafe { gl.line_width(width) };
if let hal::pso::State::Static(w) = width {
unsafe { gl.line_width(w) };
}
(glow::LINE, glow::POLYGON_OFFSET_LINE)
}
Fill => (glow::FILL, glow::POLYGON_OFFSET_FILL),
Expand Down Expand Up @@ -945,10 +947,10 @@ impl CommandQueue {
let gl = &self.share.context;

match depth {
hal::pso::DepthTest::On { fun, write } => unsafe {
Some(depth) => unsafe {
gl.enable(glow::DEPTH_TEST);

let cmp = match fun {
let cmp = match depth.fun {
Never => glow::NEVER,
Less => glow::LESS,
LessEqual => glow::LEQUAL,
Expand All @@ -960,9 +962,9 @@ impl CommandQueue {
};

gl.depth_func(cmp);
gl.depth_mask(write as _);
gl.depth_mask(depth.write as _);
},
hal::pso::DepthTest::Off => unsafe {
None => unsafe {
gl.disable(glow::DEPTH_TEST);
},
}
Expand Down

0 comments on commit bcfb6d7

Please sign in to comment.