texture: First image load
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Has been cancelled

This commit is contained in:
Florian RICHER 2025-05-27 22:25:17 +02:00
parent 5b0ab19207
commit 29a4da5666
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
11 changed files with 220 additions and 72 deletions

View file

@ -3,67 +3,35 @@ use crate::core::render::pipelines::triangle_pipeline::create_triangle_pipeline;
use crate::core::render::primitives::camera::Camera;
use crate::core::render::primitives::vertex::Vertex2D;
use crate::core::render::render_context::RenderContext;
use crate::core::render::texture::Texture;
use crate::core::scene::Scene;
use crate::core::timer::Timer;
use glam::{Mat4, Quat, Vec3};
use std::sync::Arc;
use vulkano::buffer::Subbuffer;
use vulkano::command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer};
use vulkano::command_buffer::{
AutoCommandBufferBuilder, CommandBufferUsage, PrimaryAutoCommandBuffer,
PrimaryCommandBufferAbstract,
};
use vulkano::descriptor_set::{DescriptorSet, WriteDescriptorSet};
use vulkano::pipeline::{GraphicsPipeline, Pipeline, PipelineBindPoint};
const VERTICES: [Vertex2D; 12] = [
// Triangle en haut à gauche
const VERTICES: [Vertex2D; 4] = [
Vertex2D {
position: [-0.5, -0.75],
color: [1.0, 0.0, 0.0],
position: [-0.5, -0.5],
uv: [0.0, 0.0],
},
Vertex2D {
position: [-0.75, -0.25],
color: [0.0, 1.0, 0.0],
position: [-0.5, 0.5],
uv: [0.0, 1.0],
},
Vertex2D {
position: [-0.25, -0.25],
color: [0.0, 0.0, 1.0],
},
// Triangle en bas à gauche
Vertex2D {
position: [-0.5, 0.25],
color: [0.5, 0.5, 0.5],
position: [0.5, -0.5],
uv: [1.0, 0.0],
},
Vertex2D {
position: [-0.75, 0.75],
color: [0.2, 0.8, 0.2],
},
Vertex2D {
position: [-0.25, 0.75],
color: [0.8, 0.2, 0.2],
},
// Triangle en haut à droite
Vertex2D {
position: [0.5, -0.75],
color: [1.0, 1.0, 0.0],
},
Vertex2D {
position: [0.25, -0.25],
color: [0.0, 1.0, 1.0],
},
Vertex2D {
position: [0.75, -0.25],
color: [1.0, 0.0, 1.0],
},
// Triangle en bas à droite
Vertex2D {
position: [0.5, 0.25],
color: [0.1, 0.5, 0.8],
},
Vertex2D {
position: [0.25, 0.75],
color: [0.8, 0.6, 0.1],
},
Vertex2D {
position: [0.75, 0.75],
color: [0.3, 0.4, 0.6],
position: [0.5, 0.5],
uv: [1.0, 1.0],
},
];
@ -71,6 +39,7 @@ pub struct MainSceneState {
pipeline: Arc<GraphicsPipeline>,
vertex_buffer: Subbuffer<[Vertex2D]>,
camera: Camera,
texture: Texture,
}
#[derive(Default)]
@ -105,11 +74,33 @@ impl Scene for MainScene {
),
);
let mut uploads = AutoCommandBufferBuilder::primary(
render_context.command_buffer_allocator().clone(),
render_context.graphics_queue().queue_family_index(),
CommandBufferUsage::OneTimeSubmit,
)
.unwrap();
let texture = Texture::from_file(
render_context.device(),
render_context.memory_allocator(),
&mut uploads,
"res/textures/wooden-crate.jpg",
)
.unwrap();
let _ = uploads
.build()
.unwrap()
.execute(render_context.graphics_queue().clone())
.unwrap();
self.state = Some(MainSceneState {
pipeline,
vertex_buffer,
camera,
})
texture,
});
}
fn update(
@ -157,21 +148,32 @@ impl Scene for MainScene {
) {
let state = self.state.as_ref().unwrap();
let vertex_count = state.vertex_buffer.len() as u32;
let instance_count = vertex_count / 3;
let instance_count = vertex_count / 4;
let layout = &state.pipeline.layout().set_layouts()[0];
let layouts = state.pipeline.layout().set_layouts();
let uniform_buffer = state
.camera
.create_buffer(render_context.memory_allocator())
.unwrap();
let descriptor_set = DescriptorSet::new(
let uniform_descriptor_set = DescriptorSet::new(
render_context.descriptor_set_allocator().clone(),
layout.clone(),
layouts[0].clone(),
[WriteDescriptorSet::buffer(0, uniform_buffer)],
[],
)
.unwrap();
let texture_descriptor_set = DescriptorSet::new(
render_context.descriptor_set_allocator().clone(),
layouts[1].clone(),
[
WriteDescriptorSet::sampler(0, state.texture.get_sampler().clone()),
WriteDescriptorSet::image_view(1, state.texture.get_texture().clone()),
],
[],
)
.unwrap();
unsafe {
builder
.bind_pipeline_graphics(state.pipeline.clone())
@ -180,7 +182,7 @@ impl Scene for MainScene {
PipelineBindPoint::Graphics,
state.pipeline.layout().clone(),
0,
descriptor_set,
vec![uniform_descriptor_set, texture_descriptor_set],
)
.unwrap()
.bind_vertex_buffers(0, state.vertex_buffer.clone())