From e149912961d3c792c362efd33c4e301623b055c5 Mon Sep 17 00:00:00 2001 From: Zhixing Zhang Date: Sat, 30 Jan 2021 16:54:57 -0500 Subject: [PATCH] push constant ranges # Conflicts: # crates/bevy_render/src/pipeline/pipeline_layout.rs # crates/bevy_wgpu/src/wgpu_type_converter.rs --- crates/bevy_render/src/draw.rs | 6 +++--- crates/bevy_render/src/pass/render_pass.rs | 3 +-- .../src/pipeline/pipeline_layout.rs | 17 ++++++++++++++-- .../renderer/wgpu_render_resource_context.rs | 7 ++++++- crates/bevy_wgpu/src/wgpu_render_pass.rs | 3 +-- crates/bevy_wgpu/src/wgpu_type_converter.rs | 20 +++++++++++++------ 6 files changed, 40 insertions(+), 16 deletions(-) diff --git a/crates/bevy_render/src/draw.rs b/crates/bevy_render/src/draw.rs index 49e718f77da7b..4e0e000181c86 100644 --- a/crates/bevy_render/src/draw.rs +++ b/crates/bevy_render/src/draw.rs @@ -1,6 +1,7 @@ use crate::{ pipeline::{ - IndexFormat, PipelineCompiler, PipelineDescriptor, PipelineLayout, PipelineSpecialization, + BindingShaderStage, IndexFormat, PipelineCompiler, PipelineDescriptor, PipelineLayout, + PipelineSpecialization, }, renderer::{ AssetRenderResourceBindings, BindGroup, BindGroupId, BufferId, RenderResource, @@ -16,7 +17,6 @@ use bevy_ecs::{ use bevy_reflect::Reflect; use std::{ops::Range, sync::Arc}; use thiserror::Error; -use crate::pipeline::BindingShaderStage; /// A queued command for the renderer #[derive(Debug, Clone, Eq, PartialEq)] @@ -42,7 +42,7 @@ pub enum RenderCommand { SetPushConstants { stages: BindingShaderStage, offset: u32, - data: Arc<[u8]> + data: Arc<[u8]>, }, DrawIndexed { indices: Range, diff --git a/crates/bevy_render/src/pass/render_pass.rs b/crates/bevy_render/src/pass/render_pass.rs index c63ca88c71b55..393f295a49da7 100644 --- a/crates/bevy_render/src/pass/render_pass.rs +++ b/crates/bevy_render/src/pass/render_pass.rs @@ -1,10 +1,9 @@ use crate::{ - pipeline::{BindGroupDescriptorId, IndexFormat, PipelineDescriptor}, + pipeline::{BindGroupDescriptorId, BindingShaderStage, IndexFormat, PipelineDescriptor}, renderer::{BindGroupId, BufferId, RenderContext}, }; use bevy_asset::Handle; use std::ops::Range; -use crate::pipeline::BindingShaderStage; pub trait RenderPass { fn get_render_context(&self) -> &dyn RenderContext; diff --git a/crates/bevy_render/src/pipeline/pipeline_layout.rs b/crates/bevy_render/src/pipeline/pipeline_layout.rs index 86702f4dadd8c..6b9caf0f73d19 100644 --- a/crates/bevy_render/src/pipeline/pipeline_layout.rs +++ b/crates/bevy_render/src/pipeline/pipeline_layout.rs @@ -1,12 +1,13 @@ use super::{BindGroupDescriptor, VertexBufferLayout}; -use crate::shader::ShaderLayout; +use crate::{pipeline::BindingShaderStage, shader::ShaderLayout}; use bevy_utils::HashMap; -use std::hash::Hash; +use std::{hash::Hash, ops::Range}; #[derive(Clone, Debug, Default)] pub struct PipelineLayout { pub bind_groups: Vec, pub vertex_buffer_descriptors: Vec, + pub push_constant_ranges: Vec, } impl PipelineLayout { @@ -65,6 +66,8 @@ impl PipelineLayout { PipelineLayout { bind_groups: bind_groups_result, vertex_buffer_descriptors, + // TODO: get push constant ranges from shader layout + push_constant_ranges: vec![], } } } @@ -103,3 +106,13 @@ impl UniformProperty { } } } + +#[derive(Hash, Clone, Debug)] +pub struct PushConstantRange { + /// Stage push constant range is visible from. Each stage can only be served by at most one range. + /// One range can serve multiple stages however. + pub stages: BindingShaderStage, + /// Range in push constant memory to use for the stage. Must be less than [`Limits::max_push_constant_size`]. + /// Start and end must be aligned to the 4s. + pub range: Range, +} diff --git a/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs b/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs index e7c5b79936279..b66b342a9f4db 100644 --- a/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs +++ b/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs @@ -444,13 +444,18 @@ impl RenderResourceContext for WgpuRenderResourceContext { .iter() .map(|bind_group| bind_group_layouts.get(&bind_group.id).unwrap()) .collect::>(); + let push_constant_ranges: Vec = layout + .push_constant_ranges + .iter() + .map(|range| range.clone().wgpu_into()) + .collect(); let pipeline_layout = self .device .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: bind_group_layouts.as_slice(), - push_constant_ranges: &[], + push_constant_ranges: &push_constant_ranges, }); let owned_vertex_buffer_descriptors = layout diff --git a/crates/bevy_wgpu/src/wgpu_render_pass.rs b/crates/bevy_wgpu/src/wgpu_render_pass.rs index 91c8aad4fa5b2..2f6cc7cf9713f 100644 --- a/crates/bevy_wgpu/src/wgpu_render_pass.rs +++ b/crates/bevy_wgpu/src/wgpu_render_pass.rs @@ -2,12 +2,11 @@ use crate::{renderer::WgpuRenderContext, wgpu_type_converter::WgpuInto, WgpuReso use bevy_asset::Handle; use bevy_render::{ pass::RenderPass, - pipeline::{BindGroupDescriptorId, IndexFormat, PipelineDescriptor}, + pipeline::{BindGroupDescriptorId, BindingShaderStage, IndexFormat, PipelineDescriptor}, renderer::{BindGroupId, BufferId, RenderContext}, }; use bevy_utils::tracing::trace; use std::ops::Range; -use bevy_render::pipeline::BindingShaderStage; #[derive(Debug)] pub struct WgpuRenderPass<'a> { diff --git a/crates/bevy_wgpu/src/wgpu_type_converter.rs b/crates/bevy_wgpu/src/wgpu_type_converter.rs index 5a80ab9c003c4..8b3957b14b7aa 100644 --- a/crates/bevy_wgpu/src/wgpu_type_converter.rs +++ b/crates/bevy_wgpu/src/wgpu_type_converter.rs @@ -3,11 +3,11 @@ use bevy_render::{ color::Color, pass::{LoadOp, Operations}, pipeline::{ - BindType, BlendFactor, BlendOperation, BlendState, ColorTargetState, ColorWrite, - CompareFunction, CullMode, DepthBiasState, DepthStencilState, FrontFace, IndexFormat, - InputStepMode, MultisampleState, PolygonMode, PrimitiveState, PrimitiveTopology, - StencilFaceState, StencilOperation, StencilState, VertexAttribute, VertexBufferLayout, - VertexFormat, + BindType, BindingShaderStage, BlendFactor, BlendOperation, BlendState, ColorTargetState, + ColorWrite, CompareFunction, CullMode, DepthBiasState, DepthStencilState, FrontFace, + IndexFormat, InputStepMode, MultisampleState, PolygonMode, PrimitiveState, + PrimitiveTopology, PushConstantRange, StencilFaceState, StencilOperation, StencilState, + VertexAttribute, VertexBufferLayout, VertexFormat, }, renderer::BufferUsage, texture::{ @@ -18,7 +18,6 @@ use bevy_render::{ }; use bevy_window::Window; use wgpu::BufferBindingType; -use bevy_render::pipeline::BindingShaderStage; pub trait WgpuFrom { fn from(val: T) -> Self; @@ -247,6 +246,15 @@ impl WgpuFrom for wgpu::ShaderStage { } } +impl WgpuFrom for wgpu::PushConstantRange { + fn from(val: PushConstantRange) -> Self { + wgpu::PushConstantRange { + stages: val.stages.wgpu_into(), + range: val.range, + } + } +} + impl WgpuFrom for wgpu::TextureSampleType { fn from(texture_component_type: TextureSampleType) -> Self { match texture_component_type {