Skip to content

Commit

Permalink
added more texture settings
Browse files Browse the repository at this point in the history
- base level, max level, min lod, max lod, lod bias
  • Loading branch information
mrDIMAS committed Dec 19, 2024
1 parent 5fc5a3f commit ac97efc
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 104 deletions.
123 changes: 115 additions & 8 deletions fyrox-graphics/src/gl/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ pub struct GlTexture {
r_wrap_mode: WrapMode,
anisotropy: f32,
pixel_kind: PixelKind,
base_level: usize,
max_level: usize,
min_lod: f32,
max_lod: f32,
lod_bias: f32,
// Force compiler to not implement Send and Sync, because OpenGL is not thread-safe.
thread_mark: PhantomData<*const u8>,
}
Expand Down Expand Up @@ -302,6 +307,46 @@ impl TempBinding {
);
}
}

fn set_base_level(&mut self, level: usize) {
unsafe {
self.server
.gl
.tex_parameter_i32(self.target, glow::TEXTURE_BASE_LEVEL, level as i32);
}
}

fn set_max_level(&mut self, level: usize) {
unsafe {
self.server
.gl
.tex_parameter_i32(self.target, glow::TEXTURE_MAX_LEVEL, level as i32);
}
}

fn set_min_lod(&mut self, min_lod: f32) {
unsafe {
self.server
.gl
.tex_parameter_f32(self.target, glow::TEXTURE_MIN_LOD, min_lod);
}
}

fn set_max_lod(&mut self, max_lod: f32) {
unsafe {
self.server
.gl
.tex_parameter_f32(self.target, glow::TEXTURE_MAX_LOD, max_lod);
}
}

fn set_lod_bias(&mut self, bias: f32) {
unsafe {
self.server
.gl
.tex_parameter_f32(self.target, glow::TEXTURE_LOD_BIAS, bias);
}
}
}

impl Drop for TempBinding {
Expand All @@ -328,8 +373,20 @@ impl GlTexture {
/// smaller than previous.
pub fn new(
server: &GlGraphicsServer,
desc: GpuTextureDescriptor,
mut desc: GpuTextureDescriptor,
) -> Result<Self, FrameworkError> {
// Clamp the mip level values to sensible range to prevent weird behavior.
let actual_max_level = desc.mip_count.saturating_sub(1);
if desc.max_level > actual_max_level {
desc.max_level = actual_max_level;
}
if desc.base_level > desc.max_level {
desc.base_level = desc.max_level;
}
if desc.base_level > actual_max_level {
desc.base_level = actual_max_level;
}

unsafe {
let texture = server.gl.create_texture()?;

Expand All @@ -344,6 +401,11 @@ impl GlTexture {
r_wrap_mode: desc.r_wrap_mode,
anisotropy: desc.anisotropy,
pixel_kind: desc.pixel_kind,
base_level: desc.base_level,
max_level: desc.max_level,
min_lod: desc.min_lod,
max_lod: desc.max_lod,
lod_bias: desc.lod_bias,
thread_mark: PhantomData,
};

Expand All @@ -356,6 +418,11 @@ impl GlTexture {
binding.set_wrap(Coordinate::T, desc.t_wrap_mode);
binding.set_wrap(Coordinate::R, desc.r_wrap_mode);
binding.set_anisotropy(desc.anisotropy);
binding.set_base_level(desc.base_level);
binding.set_max_level(desc.max_level);
binding.set_min_lod(desc.min_lod);
binding.set_max_lod(desc.max_lod);
binding.set_lod_bias(desc.lod_bias);

Ok(result)
}
Expand Down Expand Up @@ -528,16 +595,11 @@ impl GpuTexture for GlTexture {
self.kind = kind;
self.pixel_kind = pixel_kind;

let temp_binding = self.make_temp_binding();
let mut temp_binding = self.make_temp_binding();
temp_binding.set_max_level(mip_count.saturating_sub(1));
let target = kind.gl_texture_target();

unsafe {
temp_binding.server.gl.tex_parameter_i32(
target,
glow::TEXTURE_MAX_LEVEL,
mip_count as i32 - 1,
);

let PixelDescriptor {
data_type,
format,
Expand Down Expand Up @@ -816,4 +878,49 @@ impl GpuTexture for GlTexture {
fn pixel_kind(&self) -> PixelKind {
self.pixel_kind
}

fn set_base_level(&mut self, level: usize) {
self.make_temp_binding().set_base_level(level);
self.base_level = level;
}

fn base_level(&self) -> usize {
self.base_level
}

fn set_max_level(&mut self, level: usize) {
self.make_temp_binding().set_max_level(level);
self.max_level = level;
}

fn max_level(&self) -> usize {
self.max_level
}

fn set_min_lod(&mut self, min_lod: f32) {
self.make_temp_binding().set_min_lod(min_lod);
self.min_lod = min_lod;
}

fn min_lod(&self) -> f32 {
self.min_lod
}

fn set_max_lod(&mut self, max_lod: f32) {
self.make_temp_binding().set_max_lod(max_lod);
self.max_lod = max_lod;
}

fn max_lod(&self) -> f32 {
self.max_lod
}

fn set_lod_bias(&mut self, bias: f32) {
self.make_temp_binding().set_lod_bias(bias);
self.lod_bias = bias;
}

fn lod_bias(&self) -> f32 {
self.lod_bias
}
}
42 changes: 42 additions & 0 deletions fyrox-graphics/src/gpu_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,38 @@ pub struct GpuTextureDescriptor<'a> {
pub r_wrap_mode: WrapMode,
pub anisotropy: f32,
pub data: Option<&'a [u8]>,
pub base_level: usize,
pub max_level: usize,
pub min_lod: f32,
pub max_lod: f32,
pub lod_bias: f32,
}

impl Default for GpuTextureDescriptor<'_> {
// WARNING: Do NOT change these default values. This will affect a lot of places in the engine
// and may potentially lead to weird behavior!
fn default() -> Self {
Self {
kind: GpuTextureKind::Rectangle {
width: 1,
height: 1,
},
pixel_kind: PixelKind::RGBA8,
min_filter: Default::default(),
mag_filter: Default::default(),
mip_count: 1,
s_wrap_mode: Default::default(),
t_wrap_mode: Default::default(),
r_wrap_mode: Default::default(),
anisotropy: 1.0,
data: None,
base_level: 0,
max_level: 1000,
min_lod: -1000.0,
max_lod: 1000.0,
lod_bias: 0.0,
}
}
}

pub trait GpuTexture: Any {
Expand All @@ -408,6 +440,16 @@ pub trait GpuTexture: Any {
fn read_pixels(&self) -> Vec<u8>;
fn kind(&self) -> GpuTextureKind;
fn pixel_kind(&self) -> PixelKind;
fn set_base_level(&mut self, level: usize);
fn base_level(&self) -> usize;
fn set_max_level(&mut self, level: usize);
fn max_level(&self) -> usize;
fn set_min_lod(&mut self, min_lod: f32);
fn min_lod(&self) -> f32;
fn set_max_lod(&mut self, max_lod: f32);
fn max_lod(&self) -> f32;
fn set_lod_bias(&mut self, bias: f32);
fn lod_bias(&self) -> f32;
}

impl dyn GpuTexture {
Expand Down
4 changes: 1 addition & 3 deletions fyrox-graphics/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,10 @@ pub trait GraphicsServer: Any {
pixel_kind,
min_filter: MinificationFilter::Nearest,
mag_filter: MagnificationFilter::Nearest,
mip_count: 1,
s_wrap_mode: WrapMode::ClampToEdge,
t_wrap_mode: WrapMode::ClampToEdge,
r_wrap_mode: WrapMode::ClampToEdge,
anisotropy: 1.0,
data: None,
..Default::default()
})
}
}
5 changes: 5 additions & 0 deletions fyrox-impl/src/renderer/cache/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ fn create_gpu_texture(
r_wrap_mode: texture.r_wrap_mode().into(),
anisotropy: texture.anisotropy_level(),
data: Some(texture.data()),
base_level: texture.base_level(),
max_level: texture.max_level(),
min_lod: texture.min_lod(),
max_lod: texture.max_lod(),
lod_bias: texture.lod_bias(),
})
.map(|gpu_texture| TextureRenderData {
gpu_texture,
Expand Down
13 changes: 2 additions & 11 deletions fyrox-impl/src/renderer/hdr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ use crate::{
ResourceBinding,
},
geometry_buffer::{DrawCallStatistics, GeometryBuffer},
gpu_texture::{
GpuTexture, GpuTextureDescriptor, GpuTextureKind, MagnificationFilter,
MinificationFilter, PixelKind, WrapMode,
},
gpu_texture::{GpuTexture, GpuTextureDescriptor, GpuTextureKind, PixelKind},
server::GraphicsServer,
uniform::StaticUniformBuffer,
DrawParameters, ElementRange,
Expand Down Expand Up @@ -139,14 +136,8 @@ impl HighDynamicRangeRenderer {
depth: 1,
},
pixel_kind: PixelKind::RGB8,
min_filter: MinificationFilter::Linear,
mag_filter: MagnificationFilter::Linear,
mip_count: 1,
s_wrap_mode: WrapMode::Repeat,
t_wrap_mode: WrapMode::Repeat,
r_wrap_mode: WrapMode::Repeat,
anisotropy: 1.0,
data: Some(&[0, 0, 0]),
..Default::default()
})?,
lum_calculation_method: LuminanceCalculationMethod::DownSampling,
})
Expand Down
Loading

0 comments on commit ac97efc

Please sign in to comment.