Continuing work on Texture creation

This commit is contained in:
Wito Wiala 2026-09-01 20:12:39 +01:00
parent 87afe4168d
commit 2e9526ce18
10 changed files with 112 additions and 50 deletions

View file

@ -1,15 +1,16 @@
use crate::textures::*; use crate::textures::*;
use crate::utils::{MIPMap, MIPMapFilterOptions, TextureParameterDictionary}; use crate::utils::{MIPMap, MIPMapFilterOptions, TextureParameterDictionary};
use crate::{Arena, FileLoc}; use crate::{Arena, FileLoc};
use anyhow::{anyhow, Result}; use anyhow::{Result, anyhow};
use enum_dispatch::enum_dispatch; use enum_dispatch::enum_dispatch;
use shared::Float;
use shared::core::color::ColorEncoding; use shared::core::color::ColorEncoding;
use shared::core::geometry::Vector3f; use shared::core::geometry::Vector3f;
use shared::core::image::WrapMode; use shared::core::image::WrapMode;
use shared::core::texture::SpectrumType; use shared::core::texture::SpectrumType;
use shared::core::texture::{ use shared::core::texture::{
CylindricalMapping, PlanarMapping, SphericalMapping, TextureEvalContext, TextureMapping2D, CylindricalMapping, PlanarMapping, PointTransformMapping, SphericalMapping,
UVMapping, TextureEvalContext, TextureMapping2D, TextureMapping3D, UVMapping,
}; };
use shared::spectra::{SampledSpectrum, SampledWavelengths}; use shared::spectra::{SampledSpectrum, SampledWavelengths};
use shared::textures::{ use shared::textures::{
@ -18,7 +19,6 @@ use shared::textures::{
SpectrumConstantTexture, SpectrumDotsTexture, WindyTexture, WrinkledTexture, SpectrumConstantTexture, SpectrumDotsTexture, WindyTexture, WrinkledTexture,
}; };
use shared::utils::Transform; use shared::utils::Transform;
use shared::Float;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock}; use std::sync::{Arc, Mutex, OnceLock};
@ -38,7 +38,6 @@ pub enum FloatTexture {
Bilerp(FloatBilerpTexture), Bilerp(FloatBilerpTexture),
} }
impl Default for FloatTexture { impl Default for FloatTexture {
fn default() -> Self { fn default() -> Self {
FloatTexture::Constant(FloatConstantTexture::new(1.0)) FloatTexture::Constant(FloatConstantTexture::new(1.0))
@ -102,6 +101,7 @@ pub trait CreateSpectrumTexture {
parameters: TextureParameterDictionary, parameters: TextureParameterDictionary,
spectrum_type: SpectrumType, spectrum_type: SpectrumType,
loc: FileLoc, loc: FileLoc,
arena: &Arena,
) -> Result<SpectrumTexture>; ) -> Result<SpectrumTexture>;
} }
@ -112,29 +112,29 @@ impl SpectrumTexture {
params: TextureParameterDictionary, params: TextureParameterDictionary,
spectrum_type: SpectrumType, spectrum_type: SpectrumType,
loc: FileLoc, loc: FileLoc,
_arena: &Arena, arena: &Arena,
) -> Result<Self> { ) -> Result<Self> {
match name { match name {
"constant" => { "constant" => {
SpectrumConstantTexture::create(render_from_texture, params, spectrum_type, loc) SpectrumConstantTexture::create(render_from_texture, params, spectrum_type, loc, arena)
} }
"scale" => { "scale" => {
SpectrumScaledTexture::create(render_from_texture, params, spectrum_type, loc) SpectrumScaledTexture::create(render_from_texture, params, spectrum_type, loc, arena)
} }
"mix" => SpectrumMixTexture::create(render_from_texture, params, spectrum_type, loc), "mix" => SpectrumMixTexture::create(render_from_texture, params, spectrum_type, loc, arena),
"directionmix" => { "directionmix" => {
SpectrumDirectionMixTexture::create(render_from_texture, params, spectrum_type, loc) SpectrumDirectionMixTexture::create(render_from_texture, params, spectrum_type, loc, arena)
} }
"bilerp" => { "bilerp" => {
SpectrumBilerpTexture::create(render_from_texture, params, spectrum_type, loc) SpectrumBilerpTexture::create(render_from_texture, params, spectrum_type, loc, arena)
} }
"imagemap" => { "imagemap" => {
SpectrumImageTexture::create(render_from_texture, params, spectrum_type, loc) SpectrumImageTexture::create(render_from_texture, params, spectrum_type, loc, arena)
} }
"checkerboard" => { "checkerboard" => {
SpectrumCheckerboardTexture::create(render_from_texture, params, spectrum_type, loc) SpectrumCheckerboardTexture::create(render_from_texture, params, spectrum_type, loc, arena)
} }
"dots" => SpectrumDotsTexture::create(render_from_texture, params, spectrum_type, loc), "dots" => SpectrumDotsTexture::create(render_from_texture, params, spectrum_type, loc, arena),
_ => Err(anyhow!( _ => Err(anyhow!(
"Spectrum texture type '{}' unknown at {}", "Spectrum texture type '{}' unknown at {}",
name, name,
@ -197,6 +197,21 @@ impl CreateTextureMapping for TextureMapping2D {
} }
} }
impl CreateTextureMapping for TextureMapping3D {
fn create(
params: &TextureParameterDictionary,
render_from_texture: &Transform,
loc: &FileLoc,
) -> Result<Self>
where
Self: Sized,
{
let mapping = PointTransformMapping::new(render_from_texture.inverse());
Ok(TextureMapping3D::PointTransform(mapping))
}
}
pub static TEXTURE_CACHE: OnceLock<Mutex<HashMap<TexInfo, Arc<MIPMap>>>> = OnceLock::new(); pub static TEXTURE_CACHE: OnceLock<Mutex<HashMap<TexInfo, Arc<MIPMap>>>> = OnceLock::new();
pub fn get_texture_cache() -> &'static Mutex<HashMap<TexInfo, Arc<MIPMap>>> { pub fn get_texture_cache() -> &'static Mutex<HashMap<TexInfo, Arc<MIPMap>>> {

View file

@ -1,39 +1,64 @@
use crate::Arena; use crate::Arena;
use crate::core::texture::{ use crate::core::texture::{
CreateFloatTexture, CreateSpectrumTexture, SpectrumTexture CreateFloatTexture, CreateSpectrumTexture, CreateTextureMapping, FloatTexture, SpectrumTexture,
}; };
use crate::utils::{FileLoc, TextureParameterDictionary};
use anyhow::Result; use anyhow::Result;
use shared::core::texture::{SpectrumType, TextureEvalContext}; use shared::core::spectrum::Spectrum;
use shared::{ use shared::core::texture::{SpectrumType, TextureMapping2D};
spectra::{SampledSpectrum, SampledWavelengths}, use shared::spectra::ConstantSpectrum;
textures::{FloatBilerpTexture, SpectrumBilerpTexture}, use shared::textures::{FloatBilerpTexture, SpectrumBilerpTexture};
utils::Transform use shared::utils::Transform;
};
use crate::{
core::texture::FloatTexture,
utils::{FileLoc, TextureParameterDictionary}
};
impl CreateFloatTexture for FloatBilerpTexture { impl CreateFloatTexture for FloatBilerpTexture {
fn create( fn create(
_render_from_texture: Transform, render_from_texture: Transform,
_parameters: TextureParameterDictionary, parameters: TextureParameterDictionary,
_loc: FileLoc, loc: FileLoc,
_arena: &Arena, arena: &Arena,
) -> Result<FloatTexture> { ) -> Result<FloatTexture> {
todo!() let map = TextureMapping2D::create(&parameters, &render_from_texture, &loc)?;
let tex = FloatBilerpTexture::new(
map,
parameters.get_one_float("v00", 0.)?,
parameters.get_one_float("v01", 1.)?,
parameters.get_one_float("v10", 0.)?,
parameters.get_one_float("v11", 1.)?,
);
Ok(FloatTexture::Bilerp(tex))
} }
} }
impl CreateSpectrumTexture for SpectrumBilerpTexture { impl CreateSpectrumTexture for SpectrumBilerpTexture {
fn create( fn create(
_render_from_texture: Transform, render_from_texture: Transform,
_parameters: TextureParameterDictionary, parameters: TextureParameterDictionary,
_spectrum_type: SpectrumType, spectrum_type: SpectrumType,
_loc: FileLoc, loc: FileLoc,
arena: &Arena,
) -> Result<SpectrumTexture> { ) -> Result<SpectrumTexture> {
todo!() let map = TextureMapping2D::create(&parameters, &render_from_texture, &loc)?;
} let zero = Spectrum::Constant(ConstantSpectrum::new(0.));
} let one = Spectrum::Constant(ConstantSpectrum::new(1.));
// `Spectrum` is already a device type, so it is allocated into the arena
// rather than uploaded -- upload is for host trees (Arc) -> device (Ptr).
let mut get = |name: &str, def: Spectrum| {
let s = parameters
.get_one_spectrum(name, Some(def), spectrum_type)
.unwrap_or(def);
arena.alloc(s)
};
let tex = SpectrumBilerpTexture::new(
map,
get("v00", zero),
get("v01", one),
get("v10", zero),
get("v11", one),
);
Ok(SpectrumTexture::Bilerp(tex))
}
}

View file

@ -29,6 +29,7 @@ impl CreateSpectrumTexture for SpectrumCheckerboardTexture {
_parameters: TextureParameterDictionary, _parameters: TextureParameterDictionary,
_spectrum_type: SpectrumType, _spectrum_type: SpectrumType,
_loc: FileLoc, _loc: FileLoc,
_arena: &Arena,
) -> Result<SpectrumTexture> { ) -> Result<SpectrumTexture> {
todo!() todo!()
} }

View file

@ -1,9 +1,11 @@
use crate::Arena; use crate::Arena;
use anyhow::Result; use anyhow::Result;
use shared::core::spectrum::Spectrum;
use shared::spectra::ConstantSpectrum;
use shared::{ use shared::{
core::texture::{SpectrumType, TextureEvalContext}, core::texture::SpectrumType,
textures::{FloatConstantTexture, SpectrumConstantTexture}, textures::{FloatConstantTexture, SpectrumConstantTexture},
utils::Transform utils::Transform,
}; };
use crate::{ use crate::{
@ -15,22 +17,30 @@ use crate::{
impl CreateFloatTexture for FloatConstantTexture { impl CreateFloatTexture for FloatConstantTexture {
fn create( fn create(
_render_from_texture: Transform, _render_from_texture: Transform,
_parameters: TextureParameterDictionary, parameters: TextureParameterDictionary,
_loc: FileLoc, _loc: FileLoc,
_arena: &Arena, _arena: &Arena,
) -> Result<FloatTexture> { ) -> Result<FloatTexture> {
todo!() let value = parameters.get_one_float("value", 1.)?;
Ok(FloatTexture::Constant(FloatConstantTexture::new(value)))
} }
} }
impl CreateSpectrumTexture for SpectrumConstantTexture { impl CreateSpectrumTexture for SpectrumConstantTexture {
fn create( fn create(
_render_from_texture: Transform, _render_from_texture: Transform,
_parameters: TextureParameterDictionary, parameters: TextureParameterDictionary,
_spectrum_type: SpectrumType, spectrum_type: SpectrumType,
_loc: FileLoc, _loc: FileLoc,
_arena: &Arena,
) -> Result<SpectrumTexture> { ) -> Result<SpectrumTexture> {
todo!() let one = Spectrum::Constant(ConstantSpectrum::new(1.));
let value = parameters
.get_one_spectrum("value", Some(one), spectrum_type)
.unwrap_or(one);
Ok(SpectrumTexture::Constant(SpectrumConstantTexture::new(
value,
)))
} }
} }

View file

@ -29,6 +29,7 @@ impl CreateSpectrumTexture for SpectrumDotsTexture {
_parameters: TextureParameterDictionary, _parameters: TextureParameterDictionary,
_spectrum_type: SpectrumType, _spectrum_type: SpectrumType,
_loc: FileLoc, _loc: FileLoc,
_arena: &Arena,
) -> Result<SpectrumTexture> { ) -> Result<SpectrumTexture> {
todo!() todo!()
} }

View file

@ -134,6 +134,7 @@ impl CreateSpectrumTexture for SpectrumImageTexture {
parameters: TextureParameterDictionary, parameters: TextureParameterDictionary,
spectrum_type: SpectrumType, spectrum_type: SpectrumType,
loc: FileLoc, loc: FileLoc,
_arena: &Arena,
) -> Result<SpectrumTexture> { ) -> Result<SpectrumTexture> {
let mapping = TextureMapping2D::create(&parameters, &render_from_texture, &loc)?; let mapping = TextureMapping2D::create(&parameters, &render_from_texture, &loc)?;

View file

@ -1,14 +1,19 @@
use crate::core::texture::{CreateSpectrumTexture, SpectrumTexture}; use crate::core::texture::{CreateSpectrumTexture, SpectrumTexture};
use crate::utils::{FileLoc, TextureParameterDictionary};
use crate::Arena;
use anyhow::Result;
use shared::Transform;
use shared::core::texture::SpectrumType; use shared::core::texture::SpectrumType;
use shared::textures::MarbleTexture; use shared::textures::MarbleTexture;
impl CreateSpectrumTexture for MarbleTexture { impl CreateSpectrumTexture for MarbleTexture {
fn create( fn create(
_render_from_texture: shared::utils::Transform, _render_from_texture: Transform,
_parameters: crate::utils::TextureParameterDictionary, _parameters: TextureParameterDictionary,
_spectrum_type: SpectrumType, _spectrum_type: SpectrumType,
_loc: crate::utils::FileLoc, _loc: FileLoc,
) -> anyhow::Result<SpectrumTexture> { _arena: &Arena,
) -> Result<SpectrumTexture> {
todo!() todo!()
} }
} }

View file

@ -80,6 +80,7 @@ impl CreateSpectrumTexture for SpectrumMixTexture {
_parameters: TextureParameterDictionary, _parameters: TextureParameterDictionary,
_spectrum_type: SpectrumType, _spectrum_type: SpectrumType,
_loc: FileLoc, _loc: FileLoc,
_arena: &Arena,
) -> Result<SpectrumTexture> { ) -> Result<SpectrumTexture> {
todo!() todo!()
} }
@ -98,6 +99,7 @@ impl CreateSpectrumTexture for SpectrumDirectionMixTexture {
_parameters: TextureParameterDictionary, _parameters: TextureParameterDictionary,
_spectrum_type: SpectrumType, _spectrum_type: SpectrumType,
_loc: FileLoc, _loc: FileLoc,
_arena: &Arena,
) -> Result<SpectrumTexture> { ) -> Result<SpectrumTexture> {
todo!() todo!()
} }

View file

@ -10,6 +10,7 @@ mod scaled;
mod windy; mod windy;
mod wrinkled; mod wrinkled;
pub use bilerp::*;
pub use image::*; pub use image::*;
pub use mix::*; pub use mix::*;
pub use scaled::*; pub use scaled::*;

View file

@ -63,6 +63,7 @@ impl CreateSpectrumTexture for SpectrumScaledTexture {
parameters: TextureParameterDictionary, parameters: TextureParameterDictionary,
spectrum_type: SpectrumType, spectrum_type: SpectrumType,
_loc: FileLoc, _loc: FileLoc,
_arena: &Arena,
) -> Result<SpectrumTexture> { ) -> Result<SpectrumTexture> {
let one = Spectrum::Constant(ConstantSpectrum::new(1.0)); let one = Spectrum::Constant(ConstantSpectrum::new(1.0));
let tex = parameters let tex = parameters