mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 22:11:01 +01:00
Blend color and the first bits of validation
This commit is contained in:
1 parent
d4126d723d
commit
6ddaedf0fb
7 files changed
+84
-5
No files matched your search
@@ -617,6 +617,8 @@ typedef struct {
|
||||
|
||||
#define WGPUColor_WHITE (WGPUColor){ .r = 1, .g = 1, .b = 1, .a = 1 }
|
||||
|
||||
#define WGPUPipelineFlags_BLEND_COLOR 1
|
||||
|
||||
#define WGPUShaderStageFlags_COMPUTE 4
|
||||
|
||||
#define WGPUShaderStageFlags_FRAGMENT 2
|
||||
@@ -792,6 +794,8 @@ void wgpu_render_pass_set_bind_group(WGPURenderPassId pass_id,
|
||||
uint32_t index,
|
||||
WGPUBindGroupId bind_group_id);
|
||||
|
||||
void wgpu_render_pass_set_blend_color(WGPURenderPassId pass_id, const WGPUColor *color);
|
||||
|
||||
void wgpu_render_pass_set_index_buffer(WGPURenderPassId pass_id,
|
||||
WGPUBufferId buffer_id,
|
||||
uint32_t offset);
|
||||
|
||||
@@ -316,7 +316,7 @@ pub fn command_encoder_begin_render_pass(
|
||||
.iter()
|
||||
.map(|at| {
|
||||
//TODO: integer types?
|
||||
let value = hal::command::ClearColor::Float(conv::map_color(at.clear_color));
|
||||
let value = hal::command::ClearColor::Float(conv::map_color(&at.clear_color));
|
||||
hal::command::ClearValueRaw::from(hal::command::ClearValue::Color(value))
|
||||
})
|
||||
.chain(depth_stencil_attachment.map(|at| {
|
||||
|
||||
@@ -1,22 +1,32 @@
|
||||
use crate::conv;
|
||||
use crate::command::bind::Binder;
|
||||
use crate::device::RenderPassContext;
|
||||
use crate::hub::HUB;
|
||||
use crate::pipeline::PipelineFlags;
|
||||
use crate::resource::BufferUsageFlags;
|
||||
use crate::track::{Stitch, TrackerSet};
|
||||
use crate::{
|
||||
BindGroupId, BufferId, CommandBuffer, CommandBufferId, RenderPassId, RenderPipelineId, Stored,
|
||||
BindGroupId, BufferId, CommandBuffer, CommandBufferId, Color,
|
||||
RenderPassId, RenderPipelineId, Stored,
|
||||
};
|
||||
|
||||
use hal::command::RawCommandBuffer;
|
||||
|
||||
use std::{iter, slice};
|
||||
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum DrawError {
|
||||
MissingBlendColor,
|
||||
}
|
||||
|
||||
pub struct RenderPass<B: hal::Backend> {
|
||||
raw: B::CommandBuffer,
|
||||
cmb_id: Stored<CommandBufferId>,
|
||||
context: RenderPassContext,
|
||||
binder: Binder,
|
||||
trackers: TrackerSet,
|
||||
blend_color_set: i8,
|
||||
}
|
||||
|
||||
impl<B: hal::Backend> RenderPass<B> {
|
||||
@@ -31,8 +41,18 @@ impl<B: hal::Backend> RenderPass<B> {
|
||||
context,
|
||||
binder: Binder::default(),
|
||||
trackers: TrackerSet::new(),
|
||||
blend_color_set: -1,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_ready(&self) -> Result<(), DrawError> {
|
||||
//TODO: vertex buffers
|
||||
//TODO: bind groups
|
||||
if self.blend_color_set == 0 {
|
||||
return Err(DrawError::MissingBlendColor)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -131,8 +151,12 @@ pub extern "C" fn wgpu_render_pass_draw(
|
||||
first_vertex: u32,
|
||||
first_instance: u32,
|
||||
) {
|
||||
let mut pass_guard = HUB.render_passes.write();
|
||||
let pass = &mut pass_guard[pass_id];
|
||||
pass.is_ready().unwrap();
|
||||
|
||||
unsafe {
|
||||
HUB.render_passes.write()[pass_id].raw.draw(
|
||||
pass.raw.draw(
|
||||
first_vertex..first_vertex + vertex_count,
|
||||
first_instance..first_instance + instance_count,
|
||||
);
|
||||
@@ -148,8 +172,12 @@ pub extern "C" fn wgpu_render_pass_draw_indexed(
|
||||
base_vertex: i32,
|
||||
first_instance: u32,
|
||||
) {
|
||||
let mut pass_guard = HUB.render_passes.write();
|
||||
let pass = &mut pass_guard[pass_id];
|
||||
pass.is_ready().unwrap();
|
||||
|
||||
unsafe {
|
||||
HUB.render_passes.write()[pass_id].raw.draw_indexed(
|
||||
pass.raw.draw_indexed(
|
||||
first_index..first_index + index_count,
|
||||
base_vertex,
|
||||
first_instance..first_instance + instance_count,
|
||||
@@ -202,6 +230,10 @@ pub extern "C" fn wgpu_render_pass_set_pipeline(
|
||||
"The render pipeline is not compatible with the pass!"
|
||||
);
|
||||
|
||||
if pipeline.flags.contains(PipelineFlags::BLEND_COLOR) && pass.blend_color_set < 0 {
|
||||
pass.blend_color_set = 0;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
pass.raw.bind_graphics_pipeline(&pipeline.raw);
|
||||
}
|
||||
@@ -238,3 +270,18 @@ pub extern "C" fn wgpu_render_pass_set_pipeline(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wgpu_render_pass_set_blend_color(
|
||||
pass_id: RenderPassId,
|
||||
color: &Color,
|
||||
) {
|
||||
let mut pass_guard = HUB.render_passes.write();
|
||||
let pass = &mut pass_guard[pass_id];
|
||||
|
||||
pass.blend_color_set = 1;
|
||||
|
||||
unsafe {
|
||||
pass.raw.set_blend_constants(conv::map_color(color));
|
||||
}
|
||||
}
|
||||
@@ -567,7 +567,7 @@ pub fn map_load_store_ops(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn map_color(color: Color) -> hal::pso::ColorValue {
|
||||
pub fn map_color(color: &Color) -> hal::pso::ColorValue {
|
||||
[color.r, color.g, color.b, color.a]
|
||||
}
|
||||
|
||||
|
||||
@@ -1427,10 +1427,16 @@ pub fn device_create_render_pipeline(
|
||||
depth_stencil: depth_stencil_state.map(|state| state.format),
|
||||
};
|
||||
|
||||
let mut flags = pipeline::PipelineFlags::empty();
|
||||
if color_states.iter().any(|state| state.color.uses_color() | state.alpha.uses_color()) {
|
||||
flags |= pipeline::PipelineFlags::BLEND_COLOR;
|
||||
}
|
||||
|
||||
pipeline::RenderPipeline {
|
||||
raw: pipeline,
|
||||
layout_id: desc.layout,
|
||||
pass_context,
|
||||
flags,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,16 @@ impl BlendDescriptor {
|
||||
dst_factor: BlendFactor::Zero,
|
||||
operation: BlendOperation::Add,
|
||||
};
|
||||
|
||||
pub fn uses_color(&self) -> bool {
|
||||
match (self.src_factor, self.dst_factor) {
|
||||
(BlendFactor::BlendColor, _) |
|
||||
(BlendFactor::OneMinusBlendColor, _) |
|
||||
(_, BlendFactor::BlendColor) |
|
||||
(_, BlendFactor::OneMinusBlendColor) => true,
|
||||
(_, _) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
@@ -276,8 +286,16 @@ pub struct RenderPipelineDescriptor {
|
||||
pub sample_count: u32,
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
#[repr(transparent)]
|
||||
pub struct PipelineFlags: u32 {
|
||||
const BLEND_COLOR = 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RenderPipeline<B: hal::Backend> {
|
||||
pub(crate) raw: B::GraphicsPipeline,
|
||||
pub(crate) layout_id: PipelineLayoutId,
|
||||
pub(crate) pass_context: RenderPassContext,
|
||||
pub(crate) flags: PipelineFlags,
|
||||
}
|
||||
@@ -784,6 +784,10 @@ impl<'a> RenderPass<'a> {
|
||||
wgn::wgpu_render_pass_set_pipeline(self.id, pipeline.id);
|
||||
}
|
||||
|
||||
pub fn set_blend_color(&mut self, color: Color) {
|
||||
wgn::wgpu_render_pass_set_blend_color(self.id, &color);
|
||||
}
|
||||
|
||||
pub fn set_index_buffer(&mut self, buffer: &Buffer, offset: u32) {
|
||||
wgn::wgpu_render_pass_set_index_buffer(self.id, buffer.id, offset);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user