Skip to content

Commit

Permalink
drop extra texture
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Aug 30, 2024
1 parent 2cd76b0 commit 0b3220b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 146 deletions.
137 changes: 22 additions & 115 deletions sugarloaf/src/components/rich_text/image_cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ pub struct ImageCache {
free_entries: u32,
free_images: u32,
max_texture_size: u16,
color_texture: wgpu::Texture,
mask_texture: wgpu::Texture,
pub color_texture_view: wgpu::TextureView,
pub mask_texture_view: wgpu::TextureView,
texture: wgpu::Texture,
pub texture_view: wgpu::TextureView,
}

pub const SIZE: u32 = 2048;
Expand All @@ -31,8 +29,8 @@ impl ImageCache {
// let max_texture_size = max_texture_size.clamp(1024, 8192);
let max_texture_size = SIZE;

let color_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("rich_text create color_texture"),
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("rich_text create texture"),
size: wgpu::Extent3d {
width: SIZE,
height: SIZE,
Expand All @@ -47,33 +45,12 @@ impl ImageCache {
mip_level_count: 1,
sample_count: 1,
});
let color_texture_view =
color_texture.create_view(&wgpu::TextureViewDescriptor {
let texture_view =
texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});

let mask_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("rich_text create mask_texture"),
size: wgpu::Extent3d {
width: SIZE,
height: SIZE,
depth_or_array_layers: 1,
},
view_formats: &[],
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R8Unorm,
usage: wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::COPY_SRC
| wgpu::TextureUsages::TEXTURE_BINDING,
mip_level_count: 1,
sample_count: 1,
});
let mask_texture_view = mask_texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});

Self {
entries: Vec::new(),
atlases: Vec::new(),
Expand All @@ -83,10 +60,8 @@ impl ImageCache {
free_entries: END_OF_LIST,
free_images: END_OF_LIST,
max_texture_size: max_texture_size.try_into().unwrap(),
color_texture_view,
mask_texture_view,
color_texture,
mask_texture,
texture_view,
texture,
}
}

Expand Down Expand Up @@ -296,13 +271,6 @@ impl ImageCache {
view_formats: &[],
});

let texture = match format {
// Mask
PixelFormat::A8 => &self.mask_texture,
// Color
PixelFormat::Rgba8 => &self.color_texture,
};

// if let Some(data) = data {
// let channels = match format {
// // Mask
Expand Down Expand Up @@ -333,7 +301,7 @@ impl ImageCache {

encoder.copy_texture_to_texture(
wgpu::ImageCopyTexture {
texture: &texture,
texture: &self.texture,
mip_level: 0,
origin: wgpu::Origin3d {
x: 0,
Expand All @@ -359,24 +327,11 @@ impl ImageCache {
},
);

match format {
// Mask
PixelFormat::A8 => {
self.mask_texture = new_texture;
self.mask_texture_view = self.mask_texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});
},
// Color
PixelFormat::Rgba8 => {
self.color_texture = new_texture;
self.color_texture_view = self.color_texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});
},
};
self.texture = new_texture;
self.texture_view = self.texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});
}
Event::UpdateTexture(id, format, region, data) => {
println!("bbb UpdateTexture {:?}", id);
Expand Down Expand Up @@ -408,13 +363,6 @@ impl ImageCache {
PixelFormat::Rgba8 => 4,
};

let texture = match format {
// Mask
PixelFormat::A8 => &self.mask_texture,
// Color
PixelFormat::Rgba8 => &self.color_texture,
};

encoder.copy_buffer_to_texture(
wgpu::ImageCopyBuffer {
buffer: &buffer,
Expand All @@ -425,7 +373,7 @@ impl ImageCache {
},
},
wgpu::ImageCopyTexture {
texture: &texture,
texture: &self.texture,
mip_level: 0,
origin: wgpu::Origin3d {
x: u32::from(*x),
Expand Down Expand Up @@ -513,11 +461,6 @@ impl ImageCache {
view_formats: &[],
});

let texture = match atlas.format {
PixelFormat::A8 => &self.mask_texture,
PixelFormat::Rgba8 => &self.color_texture,
};

println!("aaa CreateTexture {:?} {:?}", atlas.texture_id, atlas.format);

if let Some(data) = data {
Expand Down Expand Up @@ -578,24 +521,12 @@ impl ImageCache {
// },
// );

match atlas.format {
// Mask
PixelFormat::A8 => {
self.mask_texture = new_texture;
self.mask_texture_view = self.mask_texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});
},
// Color
PixelFormat::Rgba8 => {
self.color_texture = new_texture;
self.color_texture_view = self.color_texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});
},
};

self.texture = new_texture;
self.texture_view = self.texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});

// self.textures.insert(atlas.texture_id, texture);
} else {
Expand All @@ -615,17 +546,10 @@ impl ImageCache {
PixelFormat::Rgba8 => 4,
};

let texture = match atlas.format {
// Mask
PixelFormat::A8 => &self.mask_texture,
// Color
PixelFormat::Rgba8 => &self.color_texture,
};

context.queue.write_texture(
// Tells wgpu where to copy the pixel data
wgpu::ImageCopyTexture {
texture,
texture: &self.texture,
mip_level: 0,
origin: wgpu::Origin3d {
x: 0,
Expand All @@ -644,23 +568,6 @@ impl ImageCache {
},
texture_size,
);

match atlas.format {
// Mask
PixelFormat::A8 => {
self.mask_texture_view = self.mask_texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});
},
// Color
PixelFormat::Rgba8 => {
self.color_texture_view = self.color_texture.create_view(&wgpu::TextureViewDescriptor {
dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default()
});
},
};
// }
}
atlas.fresh = false;
Expand Down
29 changes: 2 additions & 27 deletions sugarloaf/src/components/rich_text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,6 @@ impl RichTextBrush {
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX
| wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float {
filterable: true,
},
view_dimension: wgpu::TextureViewDimension::D2Array,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float {
Expand Down Expand Up @@ -193,13 +180,7 @@ impl RichTextBrush {
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(
&images.color_texture_view,
),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(
&images.mask_texture_view,
&images.texture_view,
),
},
],
Expand Down Expand Up @@ -473,13 +454,7 @@ impl RichTextBrush {
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(
&self.images.color_texture_view,
),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(
&self.images.mask_texture_view,
&self.images.texture_view,
),
},
],
Expand Down
7 changes: 3 additions & 4 deletions sugarloaf/src/components/rich_text/rich_text.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ struct Globals {

@group(0) @binding(0) var<uniform> globals: Globals;
@group(0) @binding(1) var font_sampler: sampler;
@group(1) @binding(0) var font_color_tex: texture_2d_array<f32>;
@group(1) @binding(1) var font_mask_tex: texture_2d_array<f32>;
@group(1) @binding(0) var font_texture: texture_2d_array<f32>;

struct VertexInput {
@builtin(vertex_index) vertex_index: u32,
Expand Down Expand Up @@ -40,11 +39,11 @@ fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
var out: vec4<f32> = input.f_color;

if input.color_layer > 0 {
out = textureSample(font_color_tex, font_sampler, input.f_uv, input.color_layer);
out = textureSample(font_texture, font_sampler, input.f_uv, input.color_layer);
}

if input.mask_layer > 0 {
out = vec4<f32>(out.xyz, textureSample(font_mask_tex, font_sampler, input.f_uv, input.mask_layer).x);
out = vec4<f32>(out.xyz, textureSample(font_texture, font_sampler, input.f_uv, input.mask_layer).x);
}

return out;
Expand Down

0 comments on commit 0b3220b

Please sign in to comment.