Skip to content

Commit

Permalink
Call it a night
Browse files Browse the repository at this point in the history
Signed-off-by: Hal Gentz <[email protected]>
  • Loading branch information
goddessfreya committed May 29, 2018
1 parent 93013a2 commit dd88cf1
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 72 deletions.
30 changes: 15 additions & 15 deletions examples/hal/3-color_uniform/data/quad.frag
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ layout(set = 1, binding = 0) uniform UBOCol {
vec4 color;
} color_dat;

layout (set=2, binding=9, r32f) uniform image2D myStorageImage;
layout (set=2, binding=8) uniform sampler mySampler;
layout (set=2, binding=7) uniform texture2D mySampledImage;
layout (set=2, binding=6) uniform sampler2D myCombinedImageSampler;
layout (set=2, binding=5) uniform samplerBuffer myUniformTexelBuffer;
layout (set=2, binding=4, r32f) uniform imageBuffer myStorageTexelBuffer;
layout (set=2, binding=3) uniform myUniformBuffer
{
vec4 myElement[32];
};
layout (set=2, binding=20) buffer myStorageBuffer
{
vec4 myElement2[];
};
layout (input_attachment_index=3, set=2, binding=1) uniform subpassInput myInputAttachment;
//layout (set=2, binding=9, r32f) uniform image2D myStorageImage;
//layout (set=2, binding=8) uniform sampler mySampler;
//layout (set=2, binding=7) uniform texture2D mySampledImage;
//layout (set=2, binding=6) uniform sampler2D myCombinedImageSampler;
//layout (set=2, binding=5) uniform samplerBuffer myUniformTexelBuffer;
//layout (set=2, binding=4, r32f) uniform imageBuffer myStorageTexelBuffer;
//layout (set=2, binding=3) uniform myUniformBuffer
//{
// vec4 myElement[32];
//};
//layout (set=2, binding=20) buffer myStorageBuffer
//{
// vec4 myElement2[];
//};
//layout (input_attachment_index=3, set=2, binding=1) uniform subpassInput myInputAttachment;

void main() {
target0 = texture(sampler2D(u_texture, u_sampler), v_uv) * color_dat.color;
Expand Down
30 changes: 24 additions & 6 deletions src/backend/gl/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ pub enum Command {
CopySurfaceToBuffer(n::Surface, n::RawBuffer, command::BufferImageCopy),
CopyImageToTexture(n::ImageKind, n::Texture, command::ImageCopy),
CopyImageToSurface(n::ImageKind, n::Surface, command::ImageCopy),

BindBufferRange(gl::types::GLenum, gl::types::GLuint, n::RawBuffer, gl::types::GLintptr, gl::types::GLsizeiptr),
}

pub type FrameBufferTarget = gl::types::GLenum;
Expand Down Expand Up @@ -801,7 +803,6 @@ impl command::RawCommandBuffer<Backend> for RawCommandBuffer {
ref blend_targets,
ref attributes,
ref vertex_buffers,
ref desc_remap_data,
} = *pipeline;

if self.cache.primitive != Some(primitive) {
Expand Down Expand Up @@ -829,20 +830,37 @@ impl command::RawCommandBuffer<Backend> for RawCommandBuffer {

fn bind_graphics_descriptor_sets<T>(
&mut self,
_layout: &n::PipelineLayout,
_first_set: usize,
_sets: T,
layout: &n::PipelineLayout,
first_set: usize,
sets: T,
) where
T: IntoIterator,
T::Item: Borrow<n::DescriptorSet>,
{
// TODO
let mut set = first_set as _;

for desc_set in sets {
let desc_set = desc_set.borrow();
for new_binding in desc_set.bindings.read().unwrap().iter() {
match new_binding {
n::DescSetBindings::Buffer(btype, binding, buffer, offset, size) => {
self.push_cmd(Command::BindBufferRange(
gl::UNIFORM_BUFFER,
*layout.desc_remap_data.read().unwrap().get_binding(*btype, set, *binding).unwrap(),
*buffer,
*offset,
*size,
))
}
}
}
set += 1;
}
}

fn bind_compute_pipeline(&mut self, pipeline: &n::ComputePipeline) {
let n::ComputePipeline {
program,
ref desc_remap_data,
} = *pipeline;

if self.cache.program != Some(program) {
Expand Down
77 changes: 48 additions & 29 deletions src/backend/gl/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashMap;
use std::iter::repeat;
use std::ops::Range;
use std::{ptr, mem, slice};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, RwLock};

use gl;
use gl::types::{GLint, GLenum, GLfloat};
Expand Down Expand Up @@ -289,27 +289,42 @@ impl Device {
ast: &mut spirv::Ast<glsl::Target>,
desc_remap_data: &mut n::DescRemapData,
) {
let mut res = ast.get_shader_resources().unwrap();
let res = ast.get_shader_resources().unwrap();

let mut all_res = res.uniform_buffers;
all_res.append(&mut res.storage_buffers);
all_res.append(&mut res.storage_images);
all_res.append(&mut res.sampled_images);
all_res.append(&mut res.separate_images);
all_res.append(&mut res.separate_samplers);
all_res.append(&mut res.subpass_inputs);
println!("{:?}", res);

self.remap_desc_set(ast, desc_remap_data, &res.uniform_buffers, n::BindingTypes::UniformBuffers);
self.remap_desc_set(ast, desc_remap_data, &res.storage_buffers, n::BindingTypes::StorageBuffers);
self.remap_desc_set(ast, desc_remap_data, &res.storage_images, n::BindingTypes::StorageImages);
self.remap_desc_set(ast, desc_remap_data, &res.sampled_images, n::BindingTypes::SampledImages);
self.remap_desc_set(ast, desc_remap_data, &res.separate_images, n::BindingTypes::SeparateImages);
self.remap_desc_set(ast, desc_remap_data, &res.separate_samplers, n::BindingTypes::SeparateSamplers);
self.remap_desc_set(ast, desc_remap_data, &res.subpass_inputs, n::BindingTypes::SubpassInputs);

// Everything but these four, I think
//all_res.append(&mut res.stage_inputs);
//all_res.append(&mut res.stage_outputs);
//all_res.append(&mut res.atomic_counters);
//all_res.append(&mut res.push_constants);
}


fn remap_desc_set(
&self,
ast: &mut spirv::Ast<glsl::Target>,
desc_remap_data: &mut n::DescRemapData,
all_res: &[spirv::Resource],
btype: n::BindingTypes,
) {
for res in all_res {
if btype == n::BindingTypes::SubpassInputs {
unimplemented!()
}

for res in &all_res {
let set = ast.get_decoration(res.id, spirv::Decoration::DescriptorSet).unwrap();
let binding = ast.get_decoration(res.id, spirv::Decoration::Binding).unwrap();
let mut new_binding = desc_remap_data.get_binding_insert(set, binding);
desc_remap_data.associate_name(res.name.clone(), set, binding);
let mut new_binding = desc_remap_data.get_binding_insert(btype, set, binding);
desc_remap_data.associate_name(res.name.clone(), btype, set, binding);

if self.share.legacy_features.contains(LegacyFeatures::NO_EXPLICIT_LAYOUTS_IN_SHADER) {
debug!("Next remap is going to be to zero because explicit layouts in shaders aren't supported prior to OpenGL4.2");
Expand Down Expand Up @@ -345,11 +360,11 @@ impl Device {
_ => panic!("Unhandled")
};

for (name, (set, binding)) in desc_remap_data.get_names_and_data() {
for (name, (btype, set, binding)) in desc_remap_data.get_names_and_data() {
let gl = &self.share.context;
unsafe {
let index = gl.GetUniformBlockIndex(program, name.as_ptr() as _);
gl.UniformBlockBinding(program, index, *desc_remap_data.get_binding(*set, *binding).unwrap())
gl.UniformBlockBinding(program, index, *desc_remap_data.get_binding(*btype, *set, *binding).unwrap())
}
}

Expand Down Expand Up @@ -445,7 +460,9 @@ impl d::Device<B> for Device {
IR: IntoIterator,
IR::Item: Borrow<(pso::ShaderStageFlags, Range<u32>)>,
{
n::PipelineLayout
n::PipelineLayout {
desc_remap_data: Arc::new(RwLock::new(n::DescRemapData::new())),
}
}

fn create_graphics_pipeline<'a>(
Expand All @@ -462,8 +479,6 @@ impl d::Device<B> for Device {
}
};

let mut desc_remap_data = n::DescRemapData::new();

let program = {
let name = unsafe { gl.CreateProgram() };

Expand All @@ -476,12 +491,11 @@ impl d::Device<B> for Device {
(pso::Stage::Fragment, desc.shaders.fragment.as_ref()),
];


let shader_names = &shaders
.iter()
.filter_map(|&(stage, point_maybe)| {
point_maybe.map(|point| {
let shader_name = self.compile_shader(point, stage, &mut desc_remap_data);
let shader_name = self.compile_shader(point, stage, &mut *desc.layout.desc_remap_data.write().unwrap());
unsafe { gl.AttachShader(name, shader_name); }
shader_name
})
Expand Down Expand Up @@ -556,7 +570,6 @@ impl d::Device<B> for Device {
}
})
.collect(),
desc_remap_data,
})
}

Expand All @@ -567,11 +580,10 @@ impl d::Device<B> for Device {
let gl = &self.share.context;
let share = &self.share;

let mut desc_remap_data = n::DescRemapData::new();
let program = {
let name = unsafe { gl.CreateProgram() };

let shader = self.compile_shader(&desc.shader, pso::Stage::Compute, &mut desc_remap_data);
let shader = self.compile_shader(&desc.shader, pso::Stage::Compute, &mut *desc.layout.desc_remap_data.write().unwrap());
unsafe { gl.AttachShader(name, shader) };

unsafe { gl.LinkProgram(name) };
Expand Down Expand Up @@ -600,7 +612,6 @@ impl d::Device<B> for Device {

Ok(n::ComputePipeline {
program,
desc_remap_data,
})
}

Expand Down Expand Up @@ -1048,22 +1059,30 @@ impl d::Device<B> for Device {
for mut write in writes {
let set = &mut write.set;
let binding = write.binding;
let mut offset = write.array_offset as u64;
let mut offset = write.array_offset as _;

for descriptor in write.descriptors {
match descriptor.borrow() {
pso::Descriptor::Buffer(buffer, ref range) => {
let start = range.start.unwrap_or(0);
let end = range.end.unwrap_or(buffer.size);
let size = end - start;

set.bindings.push(n::DescSetBindings::Buffer(binding, buffer.raw, offset, size));
// must be done latter gl.BindBufferRange(GL_UNIFORM_BUFFER, self.desc_remap_data.get_binding(set, binding), buffer.raw, offset, size);
let size = (end - start) as _;

set.bindings.write().unwrap().push(
n::DescSetBindings::Buffer(
n::BindingTypes::UniformBuffers,
binding,
buffer.raw,
offset,
size,
)
);

offset += size;
},
pso::Descriptor::Sampler(sampler) => unimplemented!(),
_ => (),
pso::Descriptor::Image(view, layout) => unimplemented!(),
pso::Descriptor::Sampler(sampler) => unimplemented!(),
pso::Descriptor::CombinedImageSampler(view, layout, sampler) => unimplemented!(),
pso::Descriptor::UniformTexelBuffer(view) => unimplemented!(),
pso::Descriptor::StorageTexelBuffer(view) => unimplemented!(),
Expand Down
Loading

0 comments on commit dd88cf1

Please sign in to comment.