Skip to content

Commit

Permalink
add RenderResourceContext.get_specialized_shader
Browse files Browse the repository at this point in the history
  • Loading branch information
mrk-its committed Nov 12, 2020
1 parent b7ce610 commit 434a3e6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 39 deletions.
6 changes: 5 additions & 1 deletion crates/bevy_render/src/pipeline/pipeline_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct PipelineCompiler {
impl PipelineCompiler {
fn compile_shader(
&mut self,
render_resource_context: &dyn RenderResourceContext,
shaders: &mut Assets<Shader>,
shader_handle: &Handle<Shader>,
shader_specialization: &ShaderSpecialization,
Expand Down Expand Up @@ -102,7 +103,8 @@ impl PipelineCompiler {
.iter()
.cloned()
.collect::<Vec<String>>();
let compiled_shader = shader.get_spirv_shader(Some(&shader_def_vec));
let compiled_shader =
render_resource_context.get_specialized_shader(shader, Some(&shader_def_vec));
let specialized_handle = shaders.add(compiled_shader);
let weak_specialized_handle = specialized_handle.clone_weak();
specialized_shaders.push(SpecializedShader {
Expand Down Expand Up @@ -141,6 +143,7 @@ impl PipelineCompiler {
let source_descriptor = pipelines.get(source_pipeline).unwrap();
let mut specialized_descriptor = source_descriptor.clone();
specialized_descriptor.shader_stages.vertex = self.compile_shader(
render_resource_context,
shaders,
&specialized_descriptor.shader_stages.vertex,
&pipeline_specialization.shader_specialization,
Expand All @@ -151,6 +154,7 @@ impl PipelineCompiler {
.as_ref()
.map(|fragment| {
self.compile_shader(
render_resource_context,
shaders,
fragment,
&pipeline_specialization.shader_specialization,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,8 @@ impl RenderResourceContext for HeadlessRenderResourceContext {
fn get_aligned_texture_size(&self, size: usize) -> usize {
size
}

fn get_specialized_shader(&self, shader: &Shader, _macros: Option<&[String]>) -> Shader {
shader.clone()
}
}
1 change: 1 addition & 0 deletions crates/bevy_render/src/renderer/render_resource_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub trait RenderResourceContext: Downcast + Send + Sync + 'static {
fn create_buffer_with_data(&self, buffer_info: BufferInfo, data: &[u8]) -> BufferId;
fn create_shader_module(&self, shader_handle: &Handle<Shader>, shaders: &Assets<Shader>);
fn create_shader_module_from_source(&self, shader_handle: &Handle<Shader>, shader: &Shader);
fn get_specialized_shader(&self, shader: &Shader, macros: Option<&[String]>) -> Shader;
fn remove_buffer(&self, buffer: BufferId);
fn remove_texture(&self, texture: TextureId);
fn remove_sampler(&self, sampler: SamplerId);
Expand Down
6 changes: 2 additions & 4 deletions crates/bevy_render/src/shader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ mod shader_defs;
#[cfg(not(target_arch = "wasm32"))]
mod shader_reflect;

#[cfg(target_arch = "wasm32")]
#[path = "shader_reflect_wasm.rs"]
mod shader_reflect;

pub use shader::*;
pub use shader_defs::*;

#[cfg(not(target_arch = "wasm32"))]
pub use shader_reflect::*;

use crate::pipeline::{BindGroupDescriptor, VertexBufferDescriptor};
Expand Down
28 changes: 2 additions & 26 deletions crates/bevy_render/src/shader/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Into<bevy_glsl_to_spirv::ShaderType> for ShaderStage {
}

#[cfg(all(not(target_os = "ios"), not(target_arch = "wasm32")))]
fn glsl_to_spirv(
pub fn glsl_to_spirv(
glsl_source: &str,
stage: ShaderStage,
shader_defs: Option<&[String]>,
Expand All @@ -43,7 +43,7 @@ impl Into<shaderc::ShaderKind> for ShaderStage {
}

#[cfg(target_os = "ios")]
fn glsl_to_spirv(
pub fn glsl_to_spirv(
glsl_source: &str,
stage: ShaderStage,
shader_defs: Option<&[String]>,
Expand Down Expand Up @@ -129,30 +129,6 @@ impl Shader {
}
}

#[cfg(target_arch = "wasm32")]
pub fn get_spirv_shader(&self, macros: Option<&[String]>) -> Shader {
if let ShaderSource::Glsl(source) = &self.source {
assert!(source.starts_with("#version"));
let eol_index = source.find('\n').unwrap();
let (version_str, source) = source.split_at(eol_index);
let mut processed = version_str.to_string();
processed.push_str("\n");
if let Some(macros) = macros {
for m in macros.iter() {
processed.push_str(&format!("#define {}\n", m));
}
}
processed.push_str("#define WEBGL\n");
processed.push_str(source);
Shader {
source: ShaderSource::Glsl(processed),
stage: self.stage,
}
} else {
panic!("spirv shader is not supported");
}
}

pub fn reflect_layout(&self, enforce_bevy_conventions: bool) -> Option<ShaderLayout> {
if let ShaderSource::Spirv(ref spirv) = self.source {
Some(ShaderLayout::from_spirv(
Expand Down
7 changes: 0 additions & 7 deletions crates/bevy_render/src/shader/shader_reflect_wasm.rs

This file was deleted.

13 changes: 12 additions & 1 deletion crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use bevy_render::{
BindGroup, BufferId, BufferInfo, RenderResourceBinding, RenderResourceContext,
RenderResourceId, SamplerId, TextureId,
},
shader::Shader,
shader::{glsl_to_spirv, Shader, ShaderSource},
texture::{Extent3d, SamplerDescriptor, TextureDescriptor},
};
use bevy_window::{Window, WindowId};
Expand Down Expand Up @@ -568,4 +568,15 @@ impl RenderResourceContext for WgpuRenderResourceContext {
size
}
}

fn get_specialized_shader(&self, shader: &Shader, macros: Option<&[String]>) -> Shader {
let spirv_data = match shader.source {
ShaderSource::Spirv(ref bytes) => bytes.clone(),
ShaderSource::Glsl(ref source) => glsl_to_spirv(&source, shader.stage, macros),
};
Shader {
source: ShaderSource::Spirv(spirv_data),
..*shader
}
}
}

0 comments on commit 434a3e6

Please sign in to comment.