Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return the texture_set_wrap_xy and texture_set_filter_min_mag methods #398

Merged
merged 4 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,14 @@ pub trait RenderingBackend {
self.texture_update_part(texture, 0 as _, 0 as _, width as _, height as _, bytes)
}
fn texture_set_filter(&mut self, texture: TextureId, filter: FilterMode);
fn texture_set_filter_min_mag(
&mut self,
texture: TextureId,
filter_min: FilterMode,
filter_max: FilterMode,
);
fn texture_set_wrap(&mut self, texture: TextureId, wrap: TextureWrap);
fn texture_set_wrap_xy(&mut self, texture: TextureId, wrap_x: TextureWrap, wrap_y: TextureWrap);
fn texture_resize(&mut self, texture: TextureId, width: u32, height: u32, bytes: Option<&[u8]>);
fn texture_read_pixels(&mut self, texture: TextureId, bytes: &mut [u8]);
fn texture_update_part(
Expand Down
62 changes: 62 additions & 0 deletions src/graphics/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,50 @@ impl Texture {
}
ctx.cache.restore_texture_binding(0);
}
pub fn set_filter_min_mag(
&self,
ctx: &mut GlContext,
min_filter: FilterMode,
max_filter: FilterMode,
) {
ctx.cache.store_texture_binding(0);
ctx.cache.bind_texture(0, self.raw);

let min_filter = match min_filter {
FilterMode::Nearest => GL_NEAREST,
FilterMode::Linear => GL_LINEAR,
};
let max_filter = match max_filter {
FilterMode::Nearest => GL_NEAREST,
FilterMode::Linear => GL_LINEAR,
};
unsafe {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter as i32);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter as i32);
}
ctx.cache.restore_texture_binding(0);
}
pub fn set_wrap_xy(&self, ctx: &mut GlContext, wrap_x: TextureWrap, wrap_y: TextureWrap) {
ctx.cache.store_texture_binding(0);
ctx.cache.bind_texture(0, self.raw);
let wrap_x = match wrap_x {
TextureWrap::Repeat => GL_REPEAT,
TextureWrap::Mirror => GL_MIRRORED_REPEAT,
TextureWrap::Clamp => GL_CLAMP_TO_EDGE,
};

let wrap_y = match wrap_y {
TextureWrap::Repeat => GL_REPEAT,
TextureWrap::Mirror => GL_MIRRORED_REPEAT,
TextureWrap::Clamp => GL_CLAMP_TO_EDGE,
};

unsafe {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_x as i32);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_x as i32);
}
ctx.cache.restore_texture_binding(0);
}

pub fn set_wrap(&self, ctx: &mut GlContext, wrap: TextureWrap) {
ctx.cache.store_texture_binding(0);
Expand Down Expand Up @@ -723,10 +767,28 @@ impl RenderingBackend for GlContext {
let t = self.textures.get(texture);
t.set_filter(self, filter);
}
fn texture_set_filter_min_mag(
&mut self,
texture: TextureId,
filter_min: FilterMode,
filter_max: FilterMode,
) {
let t = self.textures.get(texture);
t.set_filter_min_mag(self, filter_min, filter_max);
}
fn texture_set_wrap(&mut self, texture: TextureId, wrap: TextureWrap) {
let t = self.textures.get(texture);
t.set_wrap(self, wrap);
}
fn texture_set_wrap_xy(
&mut self,
texture: TextureId,
wrap_x: TextureWrap,
wrap_y: TextureWrap,
) {
let t = self.textures.get(texture);
t.set_wrap_xy(self, wrap_x, wrap_y);
}
fn texture_resize(
&mut self,
texture: TextureId,
Expand Down
34 changes: 34 additions & 0 deletions src/graphics/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,32 @@ impl RenderingBackend for MetalContext {
};
unsafe { msg_send_![self.render_encoder.unwrap(), setScissorRect: r] };
}
fn texture_set_filter_min_mag(
&mut self,
texture: TextureId,
filter_min: FilterMode,
filter_max: FilterMode,
) {
let mut texture = &mut self.textures.get(texture);

let filter_min = match filter_min {
FilterMode::Nearest => MTLSamplerMinMagFilter::Nearest,
FilterMode::Linear => MTLSamplerMinMagFilter::Linear,
};
let filter_max = match filter_max {
FilterMode::Nearest => MTLSamplerMinMagFilter::Nearest,
FilterMode::Linear => MTLSamplerMinMagFilter::Linear,
};

texture.sampler = unsafe {
let sampler_dsc = msg_send_![class!(MTLSamplerDescriptor), new];

msg_send_![sampler_dsc, setMinFilter: filter_min];
msg_send_![sampler_dsc, setMagFilter: filter_max];

msg_send_![self.device, newSamplerStateWithDescriptor: sampler_dsc]
};
}
fn texture_set_filter(&mut self, texture: TextureId, filter: FilterMode) {
let texture = self.textures.get_mut(texture);

Expand All @@ -417,6 +443,14 @@ impl RenderingBackend for MetalContext {
fn texture_set_wrap(&mut self, _texture: TextureId, _wrap: TextureWrap) {
unimplemented!()
}
fn texture_set_wrap_xy(
&mut self,
texture: TextureId,
wrap_x: TextureWrap,
wrap_y: TextureWrap,
) {
unimplemented!();
}
fn texture_resize(
&mut self,
_texture: TextureId,
Expand Down
Loading