move wgpu shader code out of Shader

This commit is contained in:
Carter Anderson 2020-02-13 09:17:18 -08:00
parent 7759fdefac
commit e38d3be0e3
2 changed files with 18 additions and 13 deletions

View file

@ -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<u64, wgpu::BindGroupLayout>,
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 {

View file

@ -20,7 +20,7 @@ impl Into<shaderc::ShaderKind> for ShaderStage {
pub fn glsl_to_spirv(
glsl_source: &str,
stage: ShaderStage,
shader_defs: Option<&Vec<String>>,
shader_defs: Option<&[String]>,
) -> Vec<u32> {
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<Vec<String>>,
}
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<u32> {
pub fn get_spirv(&self, macros: Option<&[String]>) -> Vec<u32> {
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,
}
}
}