mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-01 15:53:26 -10:00
Add upload texture trait
This commit is contained in:
3 files changed
+54
-40
No files matched your search
@@ -9,7 +9,7 @@ use std::sync::{LazyLock, Mutex};
|
||||
pub use graphene_core_shaders::registry::types;
|
||||
|
||||
// Translation struct between macro and definition
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct NodeMetadata {
|
||||
pub display_name: &'static str,
|
||||
pub category: Option<&'static str>,
|
||||
|
||||
@@ -128,7 +128,7 @@ impl PerPixelAdjustCodegen<'_> {
|
||||
#(pub #uniform_members),*
|
||||
}
|
||||
};
|
||||
let uniform_struct_shader_struct_derive = crate::buffer_struct::derive_buffer_struct_struct(&self.crate_ident, &uniform_struct)?;
|
||||
let uniform_struct_shader_struct_derive = crate::buffer_struct::derive_buffer_struct_struct(self.crate_ident, &uniform_struct)?;
|
||||
|
||||
let image_params = self
|
||||
.params
|
||||
|
||||
@@ -6,47 +6,61 @@ use graphene_core::table::{Table, TableRow};
|
||||
use wgpu::util::{DeviceExt, TextureDataOrder};
|
||||
use wgpu::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages};
|
||||
|
||||
#[node_macro::node(category(""))]
|
||||
pub async fn upload_texture<'a: 'n>(_: impl Ctx, input: Table<Raster<CPU>>, executor: &'a WgpuExecutor) -> Table<Raster<GPU>> {
|
||||
let device = &executor.context.device;
|
||||
let queue = &executor.context.queue;
|
||||
let table = input
|
||||
.iter()
|
||||
.map(|row| {
|
||||
let image = row.element;
|
||||
let rgba8_data: Vec<SRGBA8> = image.data.iter().map(|x| (*x).into()).collect();
|
||||
pub trait UploadTexture {
|
||||
fn upload_texture(self, executor: &WgpuExecutor) -> Table<Raster<GPU>>;
|
||||
}
|
||||
|
||||
impl UploadTexture for Table<Raster<GPU>> {
|
||||
fn upload_texture(self, _executor: &WgpuExecutor) -> Table<Raster<GPU>> {
|
||||
self
|
||||
}
|
||||
}
|
||||
impl UploadTexture for Table<Raster<CPU>> {
|
||||
fn upload_texture(self, executor: &WgpuExecutor) -> Table<Raster<GPU>> {
|
||||
let device = &executor.context.device;
|
||||
let queue = &executor.context.queue;
|
||||
let table = self
|
||||
.iter()
|
||||
.map(|row| {
|
||||
let image = row.element;
|
||||
let rgba8_data: Vec<SRGBA8> = image.data.iter().map(|x| (*x).into()).collect();
|
||||
|
||||
let texture = device.create_texture_with_data(
|
||||
queue,
|
||||
&TextureDescriptor {
|
||||
label: Some("upload_texture node texture"),
|
||||
size: Extent3d {
|
||||
width: image.width,
|
||||
height: image.height,
|
||||
depth_or_array_layers: 1,
|
||||
let texture = device.create_texture_with_data(
|
||||
queue,
|
||||
&TextureDescriptor {
|
||||
label: Some("upload_texture node texture"),
|
||||
size: Extent3d {
|
||||
width: image.width,
|
||||
height: image.height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: TextureDimension::D2,
|
||||
format: TextureFormat::Rgba8UnormSrgb,
|
||||
// I don't know what usages are actually necessary
|
||||
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::COPY_SRC,
|
||||
view_formats: &[],
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: TextureDimension::D2,
|
||||
format: TextureFormat::Rgba8UnormSrgb,
|
||||
// I don't know what usages are actually necessary
|
||||
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::COPY_SRC,
|
||||
view_formats: &[],
|
||||
},
|
||||
TextureDataOrder::LayerMajor,
|
||||
bytemuck::cast_slice(rgba8_data.as_slice()),
|
||||
);
|
||||
TextureDataOrder::LayerMajor,
|
||||
bytemuck::cast_slice(rgba8_data.as_slice()),
|
||||
);
|
||||
|
||||
TableRow {
|
||||
element: Raster::new_gpu(texture),
|
||||
transform: *row.transform,
|
||||
alpha_blending: *row.alpha_blending,
|
||||
source_node_id: *row.source_node_id,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
TableRow {
|
||||
element: Raster::new_gpu(texture),
|
||||
transform: *row.transform,
|
||||
alpha_blending: *row.alpha_blending,
|
||||
source_node_id: *row.source_node_id,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
queue.submit([]);
|
||||
queue.submit([]);
|
||||
table
|
||||
}
|
||||
}
|
||||
|
||||
table
|
||||
#[node_macro::node(category(""))]
|
||||
pub async fn upload_texture<'a: 'n, T: UploadTexture>(_: impl Ctx, #[implementations(Table<Raster<CPU>>, Table<Raster<GPU>>)] input: T, executor: &'a WgpuExecutor) -> Table<Raster<GPU>> {
|
||||
input.upload_texture(executor)
|
||||
}
|
||||
Reference in new issue
Block a user