|
| 1 | +// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in all |
| 11 | +// copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +use crate::renderer::cache::TemporaryCache; |
| 22 | +use fyrox_core::err_once; |
| 23 | +use fyrox_graphics::{ |
| 24 | + error::FrameworkError, |
| 25 | + sampler::{ |
| 26 | + GpuSampler, GpuSamplerDescriptor, MagnificationFilter, MinificationFilter, WrapMode, |
| 27 | + }, |
| 28 | + server::GraphicsServer, |
| 29 | +}; |
| 30 | +use fyrox_texture::{ |
| 31 | + sampler::{TextureSampler, TextureSamplerResource}, |
| 32 | + TextureMagnificationFilter, TextureMinificationFilter, TextureWrapMode, |
| 33 | +}; |
| 34 | + |
| 35 | +pub fn convert_magnification_filter(v: TextureMagnificationFilter) -> MagnificationFilter { |
| 36 | + match v { |
| 37 | + TextureMagnificationFilter::Nearest => MagnificationFilter::Nearest, |
| 38 | + TextureMagnificationFilter::Linear => MagnificationFilter::Linear, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +pub fn convert_minification_filter(v: TextureMinificationFilter) -> MinificationFilter { |
| 43 | + match v { |
| 44 | + TextureMinificationFilter::Nearest => MinificationFilter::Nearest, |
| 45 | + TextureMinificationFilter::NearestMipMapNearest => MinificationFilter::NearestMipMapNearest, |
| 46 | + TextureMinificationFilter::NearestMipMapLinear => MinificationFilter::NearestMipMapLinear, |
| 47 | + TextureMinificationFilter::Linear => MinificationFilter::Linear, |
| 48 | + TextureMinificationFilter::LinearMipMapNearest => MinificationFilter::LinearMipMapNearest, |
| 49 | + TextureMinificationFilter::LinearMipMapLinear => MinificationFilter::LinearMipMapLinear, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +pub fn convert_wrap_mode(v: TextureWrapMode) -> WrapMode { |
| 54 | + match v { |
| 55 | + TextureWrapMode::Repeat => WrapMode::Repeat, |
| 56 | + TextureWrapMode::ClampToEdge => WrapMode::ClampToEdge, |
| 57 | + TextureWrapMode::ClampToBorder => WrapMode::ClampToBorder, |
| 58 | + TextureWrapMode::MirroredRepeat => WrapMode::MirroredRepeat, |
| 59 | + TextureWrapMode::MirrorClampToEdge => WrapMode::MirrorClampToEdge, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fn create_sampler( |
| 64 | + server: &dyn GraphicsServer, |
| 65 | + sampler: &TextureSampler, |
| 66 | +) -> Result<SamplerRenderData, FrameworkError> { |
| 67 | + Ok(SamplerRenderData { |
| 68 | + gpu_sampler: server.create_sampler(GpuSamplerDescriptor { |
| 69 | + mag_filter: convert_magnification_filter(sampler.magnification_filter()), |
| 70 | + min_filter: convert_minification_filter(sampler.minification_filter()), |
| 71 | + s_wrap_mode: convert_wrap_mode(sampler.s_wrap_mode()), |
| 72 | + t_wrap_mode: convert_wrap_mode(sampler.t_wrap_mode()), |
| 73 | + r_wrap_mode: convert_wrap_mode(sampler.r_wrap_mode()), |
| 74 | + anisotropy: sampler.anisotropy_level(), |
| 75 | + min_lod: sampler.min_lod(), |
| 76 | + max_lod: sampler.max_lod(), |
| 77 | + lod_bias: sampler.lod_bias(), |
| 78 | + })?, |
| 79 | + sampler_modifications_counter: sampler.modification_count, |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +#[derive(Clone)] |
| 84 | +pub struct SamplerRenderData { |
| 85 | + pub gpu_sampler: GpuSampler, |
| 86 | + sampler_modifications_counter: u64, |
| 87 | +} |
| 88 | + |
| 89 | +#[derive(Default)] |
| 90 | +pub struct SamplerCache { |
| 91 | + cache: TemporaryCache<SamplerRenderData>, |
| 92 | +} |
| 93 | + |
| 94 | +impl SamplerCache { |
| 95 | + /// Unconditionally uploads requested texture into GPU memory, previous GPU texture will be automatically |
| 96 | + /// destroyed. |
| 97 | + pub fn upload( |
| 98 | + &mut self, |
| 99 | + server: &dyn GraphicsServer, |
| 100 | + sampler_resource: &TextureSamplerResource, |
| 101 | + ) -> Result<(), FrameworkError> { |
| 102 | + let sampler = sampler_resource.state(); |
| 103 | + if let Some(sampler) = sampler.data_ref() { |
| 104 | + self.cache.get_entry_mut_or_insert_with( |
| 105 | + &sampler.cache_index, |
| 106 | + Default::default(), |
| 107 | + || create_sampler(server, sampler), |
| 108 | + )?; |
| 109 | + Ok(()) |
| 110 | + } else { |
| 111 | + Err(FrameworkError::Custom( |
| 112 | + "Sampler is not loaded yet!".to_string(), |
| 113 | + )) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + pub fn get( |
| 118 | + &mut self, |
| 119 | + server: &dyn GraphicsServer, |
| 120 | + sampler_resource: &TextureSamplerResource, |
| 121 | + ) -> Option<&GpuSampler> { |
| 122 | + let sampler_data_guard = sampler_resource.state(); |
| 123 | + if let Some(sampler) = sampler_data_guard.data_ref() { |
| 124 | + match self.cache.get_mut_or_insert_with( |
| 125 | + &sampler.cache_index, |
| 126 | + Default::default(), |
| 127 | + || create_sampler(server, sampler), |
| 128 | + ) { |
| 129 | + Ok(entry) => { |
| 130 | + // Check if some value has changed in resource. |
| 131 | + |
| 132 | + if entry.sampler_modifications_counter != sampler.sampler_modifications_count() |
| 133 | + { |
| 134 | + entry.gpu_sampler = create_sampler(server, sampler).unwrap(); |
| 135 | + } |
| 136 | + |
| 137 | + return Some(entry); |
| 138 | + } |
| 139 | + Err(e) => { |
| 140 | + drop(sampler_data_guard); |
| 141 | + err_once!( |
| 142 | + sampler_resource.key() as usize, |
| 143 | + "Failed to create GPU sampler from sampler. Reason: {:?}", |
| 144 | + e, |
| 145 | + ); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + None |
| 150 | + } |
| 151 | + |
| 152 | + pub fn update(&mut self, dt: f32) { |
| 153 | + self.cache.update(dt) |
| 154 | + } |
| 155 | + |
| 156 | + pub fn clear(&mut self) { |
| 157 | + self.cache.clear(); |
| 158 | + } |
| 159 | + |
| 160 | + pub fn unload(&mut self, sampler: &TextureSamplerResource) { |
| 161 | + if let Some(sampler) = sampler.state().data() { |
| 162 | + self.cache.remove(&sampler.cache_index); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + pub fn alive_count(&self) -> usize { |
| 167 | + self.cache.alive_count() |
| 168 | + } |
| 169 | +} |
0 commit comments