diff --git a/src/core/texture.rs b/src/core/texture.rs index 7390666..745a550 100644 --- a/src/core/texture.rs +++ b/src/core/texture.rs @@ -1,15 +1,16 @@ use crate::textures::*; use crate::utils::{MIPMap, MIPMapFilterOptions, TextureParameterDictionary}; use crate::{Arena, FileLoc}; -use anyhow::{anyhow, Result}; +use anyhow::{Result, anyhow}; use enum_dispatch::enum_dispatch; +use shared::Float; use shared::core::color::ColorEncoding; use shared::core::geometry::Vector3f; use shared::core::image::WrapMode; use shared::core::texture::SpectrumType; use shared::core::texture::{ - CylindricalMapping, PlanarMapping, SphericalMapping, TextureEvalContext, TextureMapping2D, - UVMapping, + CylindricalMapping, PlanarMapping, PointTransformMapping, SphericalMapping, + TextureEvalContext, TextureMapping2D, TextureMapping3D, UVMapping, }; use shared::spectra::{SampledSpectrum, SampledWavelengths}; use shared::textures::{ @@ -18,7 +19,6 @@ use shared::textures::{ SpectrumConstantTexture, SpectrumDotsTexture, WindyTexture, WrinkledTexture, }; use shared::utils::Transform; -use shared::Float; use std::collections::HashMap; use std::sync::{Arc, Mutex, OnceLock}; @@ -38,7 +38,6 @@ pub enum FloatTexture { Bilerp(FloatBilerpTexture), } - impl Default for FloatTexture { fn default() -> Self { FloatTexture::Constant(FloatConstantTexture::new(1.0)) @@ -102,6 +101,7 @@ pub trait CreateSpectrumTexture { parameters: TextureParameterDictionary, spectrum_type: SpectrumType, loc: FileLoc, + arena: &Arena, ) -> Result; } @@ -112,29 +112,29 @@ impl SpectrumTexture { params: TextureParameterDictionary, spectrum_type: SpectrumType, loc: FileLoc, - _arena: &Arena, + arena: &Arena, ) -> Result { match name { "constant" => { - SpectrumConstantTexture::create(render_from_texture, params, spectrum_type, loc) + SpectrumConstantTexture::create(render_from_texture, params, spectrum_type, loc, arena) } "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" => { - SpectrumDirectionMixTexture::create(render_from_texture, params, spectrum_type, loc) + SpectrumDirectionMixTexture::create(render_from_texture, params, spectrum_type, loc, arena) } "bilerp" => { - SpectrumBilerpTexture::create(render_from_texture, params, spectrum_type, loc) + SpectrumBilerpTexture::create(render_from_texture, params, spectrum_type, loc, arena) } "imagemap" => { - SpectrumImageTexture::create(render_from_texture, params, spectrum_type, loc) + SpectrumImageTexture::create(render_from_texture, params, spectrum_type, loc, arena) } "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!( "Spectrum texture type '{}' unknown at {}", name, @@ -197,6 +197,21 @@ impl CreateTextureMapping for TextureMapping2D { } } +impl CreateTextureMapping for TextureMapping3D { + fn create( + params: &TextureParameterDictionary, + render_from_texture: &Transform, + loc: &FileLoc, + ) -> Result + where + Self: Sized, + { + let mapping = PointTransformMapping::new(render_from_texture.inverse()); + Ok(TextureMapping3D::PointTransform(mapping)) + } + +} + pub static TEXTURE_CACHE: OnceLock>>> = OnceLock::new(); pub fn get_texture_cache() -> &'static Mutex>> { diff --git a/src/textures/bilerp.rs b/src/textures/bilerp.rs index 0f9592e..965e0c6 100644 --- a/src/textures/bilerp.rs +++ b/src/textures/bilerp.rs @@ -1,39 +1,64 @@ use crate::Arena; use crate::core::texture::{ - CreateFloatTexture, CreateSpectrumTexture, SpectrumTexture - }; + CreateFloatTexture, CreateSpectrumTexture, CreateTextureMapping, FloatTexture, SpectrumTexture, +}; +use crate::utils::{FileLoc, TextureParameterDictionary}; use anyhow::Result; -use shared::core::texture::{SpectrumType, TextureEvalContext}; -use shared::{ - spectra::{SampledSpectrum, SampledWavelengths}, - textures::{FloatBilerpTexture, SpectrumBilerpTexture}, - utils::Transform -}; - -use crate::{ - core::texture::FloatTexture, - utils::{FileLoc, TextureParameterDictionary} -}; +use shared::core::spectrum::Spectrum; +use shared::core::texture::{SpectrumType, TextureMapping2D}; +use shared::spectra::ConstantSpectrum; +use shared::textures::{FloatBilerpTexture, SpectrumBilerpTexture}; +use shared::utils::Transform; impl CreateFloatTexture for FloatBilerpTexture { fn create( - _render_from_texture: Transform, - _parameters: TextureParameterDictionary, - _loc: FileLoc, - _arena: &Arena, + render_from_texture: Transform, + parameters: TextureParameterDictionary, + loc: FileLoc, + arena: &Arena, ) -> Result { - todo!() + let map = TextureMapping2D::create(¶meters, &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 { fn create( - _render_from_texture: Transform, - _parameters: TextureParameterDictionary, - _spectrum_type: SpectrumType, - _loc: FileLoc, + render_from_texture: Transform, + parameters: TextureParameterDictionary, + spectrum_type: SpectrumType, + loc: FileLoc, + arena: &Arena, ) -> Result { - todo!() + let map = TextureMapping2D::create(¶meters, &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)) } } - diff --git a/src/textures/checkerboard.rs b/src/textures/checkerboard.rs index a35701a..0b1dafd 100644 --- a/src/textures/checkerboard.rs +++ b/src/textures/checkerboard.rs @@ -29,6 +29,7 @@ impl CreateSpectrumTexture for SpectrumCheckerboardTexture { _parameters: TextureParameterDictionary, _spectrum_type: SpectrumType, _loc: FileLoc, + _arena: &Arena, ) -> Result { todo!() } diff --git a/src/textures/constant.rs b/src/textures/constant.rs index ff52234..238727d 100644 --- a/src/textures/constant.rs +++ b/src/textures/constant.rs @@ -1,9 +1,11 @@ use crate::Arena; use anyhow::Result; +use shared::core::spectrum::Spectrum; +use shared::spectra::ConstantSpectrum; use shared::{ - core::texture::{SpectrumType, TextureEvalContext}, + core::texture::SpectrumType, textures::{FloatConstantTexture, SpectrumConstantTexture}, - utils::Transform + utils::Transform, }; use crate::{ @@ -15,22 +17,30 @@ use crate::{ impl CreateFloatTexture for FloatConstantTexture { fn create( _render_from_texture: Transform, - _parameters: TextureParameterDictionary, + parameters: TextureParameterDictionary, _loc: FileLoc, _arena: &Arena, ) -> Result { - todo!() + let value = parameters.get_one_float("value", 1.)?; + Ok(FloatTexture::Constant(FloatConstantTexture::new(value))) } } impl CreateSpectrumTexture for SpectrumConstantTexture { fn create( _render_from_texture: Transform, - _parameters: TextureParameterDictionary, - _spectrum_type: SpectrumType, + parameters: TextureParameterDictionary, + spectrum_type: SpectrumType, _loc: FileLoc, + _arena: &Arena, ) -> Result { - 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, + ))) } } diff --git a/src/textures/dots.rs b/src/textures/dots.rs index 29f425b..97cd160 100644 --- a/src/textures/dots.rs +++ b/src/textures/dots.rs @@ -29,6 +29,7 @@ impl CreateSpectrumTexture for SpectrumDotsTexture { _parameters: TextureParameterDictionary, _spectrum_type: SpectrumType, _loc: FileLoc, + _arena: &Arena, ) -> Result { todo!() } diff --git a/src/textures/image.rs b/src/textures/image.rs index 3707041..146ad03 100644 --- a/src/textures/image.rs +++ b/src/textures/image.rs @@ -134,6 +134,7 @@ impl CreateSpectrumTexture for SpectrumImageTexture { parameters: TextureParameterDictionary, spectrum_type: SpectrumType, loc: FileLoc, + _arena: &Arena, ) -> Result { let mapping = TextureMapping2D::create(¶meters, &render_from_texture, &loc)?; diff --git a/src/textures/marble.rs b/src/textures/marble.rs index b5541ce..6137c2a 100644 --- a/src/textures/marble.rs +++ b/src/textures/marble.rs @@ -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::textures::MarbleTexture; impl CreateSpectrumTexture for MarbleTexture { fn create( - _render_from_texture: shared::utils::Transform, - _parameters: crate::utils::TextureParameterDictionary, + _render_from_texture: Transform, + _parameters: TextureParameterDictionary, _spectrum_type: SpectrumType, - _loc: crate::utils::FileLoc, - ) -> anyhow::Result { + _loc: FileLoc, + _arena: &Arena, + ) -> Result { todo!() } } diff --git a/src/textures/mix.rs b/src/textures/mix.rs index 6944e26..05ac31d 100644 --- a/src/textures/mix.rs +++ b/src/textures/mix.rs @@ -80,6 +80,7 @@ impl CreateSpectrumTexture for SpectrumMixTexture { _parameters: TextureParameterDictionary, _spectrum_type: SpectrumType, _loc: FileLoc, + _arena: &Arena, ) -> Result { todo!() } @@ -98,6 +99,7 @@ impl CreateSpectrumTexture for SpectrumDirectionMixTexture { _parameters: TextureParameterDictionary, _spectrum_type: SpectrumType, _loc: FileLoc, + _arena: &Arena, ) -> Result { todo!() } diff --git a/src/textures/mod.rs b/src/textures/mod.rs index 6dad3ab..a293563 100644 --- a/src/textures/mod.rs +++ b/src/textures/mod.rs @@ -10,6 +10,7 @@ mod scaled; mod windy; mod wrinkled; +pub use bilerp::*; pub use image::*; pub use mix::*; pub use scaled::*; diff --git a/src/textures/scaled.rs b/src/textures/scaled.rs index e40d3a5..c798d75 100644 --- a/src/textures/scaled.rs +++ b/src/textures/scaled.rs @@ -63,6 +63,7 @@ impl CreateSpectrumTexture for SpectrumScaledTexture { parameters: TextureParameterDictionary, spectrum_type: SpectrumType, _loc: FileLoc, + _arena: &Arena, ) -> Result { let one = Spectrum::Constant(ConstantSpectrum::new(1.0)); let tex = parameters