diff --git a/src/render/render_graph_2/renderers/wgpu_renderer.rs b/src/render/render_graph_2/renderers/wgpu_renderer.rs index f4479a8368..76906af750 100644 --- a/src/render/render_graph_2/renderers/wgpu_renderer.rs +++ b/src/render/render_graph_2/renderers/wgpu_renderer.rs @@ -1,11 +1,11 @@ use crate::{ legion::prelude::*, - render::render_graph_2::{ + render::{Shader, render_graph_2::{ resource_name, update_shader_assignments, BindGroup, BindType, DynamicUniformBufferInfo, PassDescriptor, PipelineDescriptor, RenderGraph, RenderPass, RenderPassColorAttachmentDescriptor, RenderPassDepthStencilAttachmentDescriptor, Renderer, ResourceInfo, ShaderUniforms, TextureDescriptor, - }, + }}, }; use std::{collections::HashMap, ops::Deref}; @@ -70,12 +70,11 @@ impl WgpuRenderer { bind_group_layouts: &mut HashMap, device: &wgpu::Device, ) -> wgpu::RenderPipeline { - let vertex_shader_module = pipeline_descriptor + let vertex_shader_module = Self::create_shader_module(device, &pipeline_descriptor .shader_stages - .vertex - .create_shader_module(device); + .vertex, None); let fragment_shader_module = match pipeline_descriptor.shader_stages.fragment { - Some(ref fragment_shader) => Some(fragment_shader.create_shader_module(device)), + Some(ref fragment_shader) => Some(Self::create_shader_module(device, fragment_shader, None)), None => None, }; @@ -411,6 +410,10 @@ impl WgpuRenderer { encoder.copy_buffer_to_buffer(&temp_buffer, 0, uniform_buffer.unwrap(), 0, size); } } + + pub fn create_shader_module(device: &wgpu::Device, shader: &Shader, macros: Option<&[String]>) -> wgpu::ShaderModule { + device.create_shader_module(&shader.get_spirv(macros)) + } } impl Renderer for WgpuRenderer { diff --git a/src/render/shader.rs b/src/render/shader.rs index 4adaf4fa4a..7d28247f6f 100644 --- a/src/render/shader.rs +++ b/src/render/shader.rs @@ -20,7 +20,7 @@ impl Into for ShaderStage { pub fn glsl_to_spirv( glsl_source: &str, stage: ShaderStage, - shader_defs: Option<&Vec>, + shader_defs: Option<&[String]>, ) -> Vec { let shader_kind: shaderc::ShaderKind = stage.into(); let mut compiler = shaderc::Compiler::new().unwrap(); @@ -52,7 +52,6 @@ pub struct Shader { pub source: ShaderSource, pub stage: ShaderStage, pub entry_point: String, - pub macros: Option>, } impl Shader { @@ -60,22 +59,25 @@ impl Shader { Shader { source: ShaderSource::Glsl(glsl.to_string()), entry_point: "main".to_string(), - macros: None, stage, } } - pub fn get_spirv(&self) -> Vec { + pub fn get_spirv(&self, macros: Option<&[String]>) -> Vec { match self.source { ShaderSource::Spirv(ref bytes) => bytes.clone(), ShaderSource::Glsl(ref source) => { - glsl_to_spirv(&source, self.stage, self.macros.as_ref()) + glsl_to_spirv(&source, self.stage, macros) } } } - pub fn create_shader_module(&self, device: &wgpu::Device) -> wgpu::ShaderModule { - device.create_shader_module(&self.get_spirv()) + pub fn get_spirv_shader(&self, macros: Option<&[String]>) -> Shader { + Shader { + source: ShaderSource::Spirv(self.get_spirv(macros)), + entry_point: self.entry_point.clone(), + stage: self.stage, + } } }