Skip to content

Commit

Permalink
Use pointers for textures
Browse files Browse the repository at this point in the history
  • Loading branch information
grovesNL committed Jun 24, 2018
1 parent c2c77d9 commit f9193d7
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 94 deletions.
89 changes: 52 additions & 37 deletions src/backend/metal/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {AutoreleasePool, Backend, PrivateDisabilities, Shared, validate_line_width, BufferPtr};
use {AutoreleasePool, Backend, PrivateDisabilities, Shared, validate_line_width, BufferPtr, TexturePtr};
use {conversions as conv, native, soft, window};
use internal::{BlitVertex, Channel, ClearKey, ClearVertex, ServicePipes};

Expand All @@ -21,6 +21,7 @@ use hal::range::RangeArg;

use foreign_types::ForeignType;
use metal::{self, MTLViewport, MTLScissorRect, MTLPrimitiveType, MTLIndexType, MTLSize, CaptureManager};
use objc::runtime::Object;
use cocoa::foundation::{NSUInteger, NSInteger, NSRange};
use block::{ConcreteBlock};
use smallvec::SmallVec;
Expand Down Expand Up @@ -217,11 +218,11 @@ impl State {
})
});
let com_textures = resources.textures.iter().enumerate().filter_map(move |(i, resource)| {
resource.as_ref().map(|texture| {
resource.map(|texture| {
soft::RenderCommand::BindTexture {
stage,
index: i as _,
texture: Some(texture.as_ref()),
texture: Some(texture),
}
})
});
Expand Down Expand Up @@ -276,10 +277,10 @@ impl State {
.iter()
.enumerate()
.filter_map(|(i, ref resource)| {
resource.as_ref().map(|texture| {
resource.map(|texture| {
soft::ComputeCommand::BindTexture {
index: i as _,
texture: Some(texture.as_ref()),
texture: Some(texture),
}
})
});
Expand Down Expand Up @@ -533,7 +534,7 @@ impl State {
#[derive(Clone, Debug)]
struct StageResources {
buffers: Vec<Option<(BufferPtr, buffer::Offset)>>,
textures: Vec<Option<metal::Texture>>,
textures: Vec<Option<TexturePtr>>,
samplers: Vec<Option<metal::SamplerState>>,
push_constants_buffer_id: Option<u32>,
}
Expand Down Expand Up @@ -562,7 +563,7 @@ impl StageResources {
self.buffers[slot] = Some((buffer.to_owned(), offset));
}

fn add_textures(&mut self, start: usize, textures: &[Option<(metal::Texture, Layout)>]) {
fn add_textures(&mut self, start: usize, textures: &[Option<(TexturePtr, Layout)>]) {
while self.textures.len() < start + textures.len() {
self.textures.push(None)
}
Expand Down Expand Up @@ -937,11 +938,12 @@ fn exec_render<'a>(encoder: &metal::RenderCommandEncoderRef, command: soft::Rend
}
}
Cmd::BindTexture { stage, index, texture } => {
let native = texture.as_ref().map(|t| t.as_native());
match stage {
pso::Stage::Vertex =>
encoder.set_vertex_texture(index as _, texture),
encoder.set_vertex_texture(index as _, native),
pso::Stage::Fragment =>
encoder.set_fragment_texture(index as _, texture),
encoder.set_fragment_texture(index as _, native),
_ => unimplemented!()
}
}
Expand Down Expand Up @@ -1051,12 +1053,12 @@ fn exec_blit<'a>(encoder: &metal::BlitCommandEncoderRef, command: soft::BlitComm
let layers = region.src_subresource.layers.zip(region.dst_subresource.layers);
for (src_layer, dst_layer) in layers {
encoder.copy_from_texture(
src,
src.as_native(),
src_layer as _,
region.src_subresource.level as _,
src_offset,
size,
dst,
dst.as_native(),
dst_layer as _,
region.dst_subresource.level as _,
dst_offset,
Expand All @@ -1077,7 +1079,7 @@ fn exec_blit<'a>(encoder: &metal::BlitCommandEncoderRef, command: soft::BlitComm
row_pitch as NSUInteger,
slice_pitch as NSUInteger,
extent,
dst,
dst.as_native(),
layer as NSUInteger,
r.level as NSUInteger,
origin,
Expand All @@ -1094,7 +1096,7 @@ fn exec_blit<'a>(encoder: &metal::BlitCommandEncoderRef, command: soft::BlitComm
for layer in r.layers.clone() {
let offset = region.buffer_offset + slice_pitch as NSUInteger * (layer - r.layers.start) as NSUInteger;
encoder.copy_from_texture_to_buffer(
src,
src.as_native(),
layer as NSUInteger,
r.level as NSUInteger,
origin,
Expand All @@ -1121,7 +1123,8 @@ fn exec_compute<'a>(encoder: &metal::ComputeCommandEncoderRef, command: soft::Co
encoder.set_bytes(index as _, (words.len() * WORD_SIZE) as u64, words.as_ptr() as _);
}
Cmd::BindTexture { index, texture } => {
encoder.set_texture(index as _, texture);
let native = texture.as_ref().map(|t| t.as_native());
encoder.set_texture(index as _, native);
}
Cmd::BindSampler { index, sampler } => {
encoder.set_sampler_state(index as _, sampler);
Expand Down Expand Up @@ -1598,22 +1601,28 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
};
let texture = if CLEAR_IMAGE_ARRAY && sub.layers.start > 0 {
// aliasing is necessary for bulk-clearing all layers starting with 0
let tex = image.raw.new_texture_view_from_slice(
let native = image.raw.as_native();
let mipmap_level_count = native.mipmap_level_count();
let raw = native.new_texture_view_from_slice(
image.mtl_format,
image.mtl_type,
NSRange {
location: 0,
length: image.raw.mipmap_level_count(),
length: mipmap_level_count,
},
NSRange {
location: sub.layers.start as _,
length: num_layers,
},
);
retained_textures.push(tex);
retained_textures.last().unwrap()
let raw_ptr = raw.as_ptr();
unsafe {
msg_send![raw_ptr as *mut Object, retain];
}
retained_textures.push(raw);
TexturePtr(raw_ptr)
} else {
&*image.raw
image.raw
};

for layer in layers {
Expand All @@ -1633,7 +1642,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
.color_attachments()
.object_at(0)
.unwrap();
attachment.set_texture(Some(texture));
attachment.set_texture(Some(texture.as_native()));
attachment.set_level(level as _);
attachment.set_store_action(metal::MTLStoreAction::Store);
if !CLEAR_IMAGE_ARRAY {
Expand All @@ -1652,7 +1661,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
let attachment = descriptor
.depth_attachment()
.unwrap();
attachment.set_texture(Some(texture));
attachment.set_texture(Some(texture.as_native()));
attachment.set_level(level as _);
attachment.set_store_action(metal::MTLStoreAction::Store);
if !CLEAR_IMAGE_ARRAY {
Expand All @@ -1671,7 +1680,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
let attachment = descriptor
.stencil_attachment()
.unwrap();
attachment.set_texture(Some(texture));
attachment.set_texture(Some(texture.as_native()));
attachment.set_level(level as _);
attachment.set_store_action(metal::MTLStoreAction::Store);
if !CLEAR_IMAGE_ARRAY {
Expand Down Expand Up @@ -2032,7 +2041,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
soft::RenderCommand::BindTexture {
stage: pso::Stage::Fragment,
index: 0,
texture: Some(&*src.raw)
texture: Some(src.raw)
},
];

Expand Down Expand Up @@ -2087,21 +2096,21 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
.color_attachments()
.object_at(0)
.unwrap();
attachment.set_texture(Some(&dst.raw));
attachment.set_texture(Some(dst.raw.as_native()));
attachment.set_level(level as _);
}
if aspects.contains(Aspects::DEPTH) {
let attachment = descriptor
.depth_attachment()
.unwrap();
attachment.set_texture(Some(&dst.raw));
attachment.set_texture(Some(dst.raw.as_native()));
attachment.set_level(level as _);
}
if aspects.contains(Aspects::STENCIL) {
let attachment = descriptor
.stencil_attachment()
.unwrap();
attachment.set_texture(Some(&dst.raw));
attachment.set_texture(Some(dst.raw.as_native()));
attachment.set_level(level as _);
}

Expand Down Expand Up @@ -2494,7 +2503,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
soft::RenderCommand::BindTexture {
stage,
index: start + i,
texture: texture.as_ref().map(|&(ref root, _)| root.as_ref()),
texture: texture.as_ref().map(|&(root, _)| root),
}
}));
}
Expand All @@ -2509,7 +2518,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
commands.push(soft::RenderCommand::BindTexture {
stage,
index: start_tx + i,
texture: texture.as_ref().map(|&(ref root, _)| root.as_ref()),
texture: texture.as_ref().map(|&(root, _)| root),
});
commands.push(soft::RenderCommand::BindSampler {
stage,
Expand Down Expand Up @@ -2677,7 +2686,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
commands.extend(images.iter().enumerate().map(|(i, texture)| {
soft::ComputeCommand::BindTexture {
index: start + i,
texture: texture.as_ref().map(|&(ref root, _)| root.as_ref()),
texture: texture.as_ref().map(|&(root, _)| root),
}
}));
}
Expand All @@ -2689,7 +2698,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
resources.add_samplers(id_sm, &[sampler.clone()]);
commands.push(soft::ComputeCommand::BindTexture {
index: id_tx,
texture: texture.as_ref().map(|&(ref root, _)| root.as_ref()),
texture: texture.as_ref().map(|&(root, _)| root),
});
commands.push(soft::ComputeCommand::BindSampler {
index: id_sm,
Expand Down Expand Up @@ -2869,18 +2878,24 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
} = *self.inner.borrow_mut();

let new_src = if src.mtl_format == dst.mtl_format {
&*src.raw
src.raw
} else {
assert_eq!(src.format_desc.bits, dst.format_desc.bits);
let tex = src.raw.new_texture_view(dst.mtl_format);
retained_textures.push(tex);
retained_textures.last().unwrap()

let raw = src.raw.as_native().new_texture_view(dst.mtl_format);
let raw_ptr = raw.as_ptr();
unsafe {
msg_send![raw_ptr as *mut Object, retain];
}

retained_textures.push(raw);
TexturePtr(raw_ptr)
};

let commands = regions.into_iter().map(|region| {
soft::BlitCommand::CopyImage {
src: new_src,
dst: &*dst.raw,
dst: dst.raw,
region: region.borrow().clone(),
}
});
Expand All @@ -2903,7 +2918,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
let commands = regions.into_iter().map(|region| {
soft::BlitCommand::CopyBufferToImage {
src: src.raw,
dst: &*dst.raw,
dst: dst.raw,
dst_desc: dst.format_desc,
region: region.borrow().clone(),
}
Expand All @@ -2927,7 +2942,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
// FIXME: layout
let commands = regions.into_iter().map(|region| {
soft::BlitCommand::CopyImageToBuffer {
src: &*src.raw,
src: src.raw,
src_desc: src.format_desc,
dst: dst.raw,
region: region.borrow().clone(),
Expand Down
Loading

0 comments on commit f9193d7

Please sign in to comment.