app/gfx: allow RGB8 texture fmts in addition to RGBA8

This commit is contained in:
darkfi
2025-12-25 19:46:14 -03:00
parent f897e8f36c
commit 187829a873
7 changed files with 79 additions and 23 deletions

View File

@@ -30,7 +30,8 @@ use miniquad::{
conf, window, Backend, Bindings, BlendFactor, BlendState, BlendValue, BufferLayout,
BufferSource, BufferType, BufferUsage, Equation, EventHandler, KeyCode, KeyMods, MouseButton,
PassAction, Pipeline, PipelineParams, RenderingBackend, ShaderMeta, ShaderSource, TouchPhase,
UniformDesc, UniformType, VertexAttribute, VertexFormat,
TextureFormat, TextureKind, TextureParams, TextureWrap, UniformDesc, UniformType,
VertexAttribute, VertexFormat,
UniformBlockLayout,
};
@@ -293,8 +294,8 @@ impl Stage {
fn process_method(&mut self, method: GraphicsMethod) {
//println!("Received method: {:?}", method);
match method {
GraphicsMethod::NewTexture((width, height, data, gfx_texture_id)) => {
self.method_new_texture(width, height, data, gfx_texture_id)
GraphicsMethod::NewTexture((width, height, data, fmt, gfx_texture_id, _)) => {
self.method_new_texture(width, height, data, fmt, gfx_texture_id)
}
GraphicsMethod::DeleteTexture(texture) => self.method_delete_texture(texture),
GraphicsMethod::NewVertexBuffer((verts, sendr)) => {
@@ -313,9 +314,24 @@ impl Stage {
width: u16,
height: u16,
data: Vec<u8>,
fmt: TextureFormat,
gfx_texture_id: GfxTextureId,
) {
let texture = self.ctx.new_texture_from_rgba8(width, height, &data);
let texture = self.ctx.new_texture_from_data_and_format(
&data,
TextureParams {
kind: TextureKind::Texture2D,
format: fmt,
width: width as _,
height: height as _,
wrap: TextureWrap::Clamp,
min_filter: miniquad::FilterMode::Linear,
mag_filter: miniquad::FilterMode::Linear,
mipmap_filter: miniquad::MipmapFilterMode::None,
allocate_mipmaps: false,
sample_count: 1,
},
);
if DEBUG_GFXAPI {
println!("Invoked method: new_texture({}, {}, ..., {}) -> {:?}",
width, height, gfx_texture_id, texture);

View File

@@ -25,8 +25,9 @@ use miniquad::native::egl;
use miniquad::{
conf, window, Backend, Bindings, BlendFactor, BlendState, BlendValue, BufferLayout,
BufferSource, BufferType, BufferUsage, Equation, EventHandler, KeyCode, KeyMods, MouseButton,
PassAction, Pipeline, PipelineParams, RenderingBackend, ShaderMeta, ShaderSource, TouchPhase,
UniformDesc, UniformType, VertexAttribute, VertexFormat,
PassAction, Pipeline, PipelineParams, RenderingBackend, ShaderMeta, ShaderSource,
TextureFormat, TextureKind, TextureParams, TextureWrap, TouchPhase, UniformDesc, UniformType,
VertexAttribute, VertexFormat,
};
use parking_lot::Mutex as SyncMutex;
use std::{
@@ -216,11 +217,12 @@ impl RenderApi {
width: u16,
height: u16,
data: Vec<u8>,
fmt: TextureFormat,
tag: DebugTag,
) -> (TextureId, EpochIndex) {
let gfx_texture_id = NEXT_TEXTURE_ID.fetch_add(1, Ordering::Relaxed);
let method = GraphicsMethod::NewTexture((width, height, data, gfx_texture_id, tag));
let method = GraphicsMethod::NewTexture((width, height, data, fmt, gfx_texture_id, tag));
let epoch = self.send(method);
(gfx_texture_id, epoch)
@@ -231,9 +233,10 @@ impl RenderApi {
width: u16,
height: u16,
data: Vec<u8>,
fmt: TextureFormat,
tag: DebugTag,
) -> ManagedTexturePtr {
let (id, epoch) = self.new_unmanaged_texture(width, height, data, tag);
let (id, epoch) = self.new_unmanaged_texture(width, height, data, fmt, tag);
Arc::new(ManagedTexture { id, epoch, render_api: self.clone(), tag })
}
@@ -733,7 +736,7 @@ type DcId = u64;
#[derive(Clone)]
pub enum GraphicsMethod {
NewTexture((u16, u16, Vec<u8>, TextureId, DebugTag)),
NewTexture((u16, u16, Vec<u8>, TextureFormat, TextureId, DebugTag)),
DeleteTexture((TextureId, DebugTag)),
NewVertexBuffer((Vec<Vertex>, BufferId, DebugTag)),
NewIndexBuffer((Vec<u16>, BufferId, DebugTag)),
@@ -1045,8 +1048,8 @@ impl Stage {
fn process_method(&mut self, mut method: GraphicsMethod) {
//d!("Received method: {method:?}");
match &mut method {
GraphicsMethod::NewTexture((width, height, data, gtex_id, _)) => {
self.method_new_texture(*width, *height, data, *gtex_id)
GraphicsMethod::NewTexture((width, height, data, fmt, gtex_id, _)) => {
self.method_new_texture(*width, *height, data, *fmt, *gtex_id)
}
GraphicsMethod::DeleteTexture((gtex_id, _)) => self.method_delete_texture(*gtex_id),
GraphicsMethod::NewVertexBuffer((verts, gbuff_id, _)) => {
@@ -1122,9 +1125,24 @@ impl Stage {
width: u16,
height: u16,
data: &Vec<u8>,
fmt: TextureFormat,
gfx_texture_id: TextureId,
) {
let texture = self.ctx.new_texture_from_rgba8(width, height, data);
let texture = self.ctx.new_texture_from_data_and_format(
data,
TextureParams {
kind: TextureKind::Texture2D,
format: fmt,
width: width as _,
height: height as _,
wrap: TextureWrap::Clamp,
min_filter: miniquad::FilterMode::Linear,
mag_filter: miniquad::FilterMode::Linear,
mipmap_filter: miniquad::MipmapFilterMode::None,
allocate_mipmaps: false,
sample_count: 1,
},
);
if DEBUG_GFXAPI {
d!("Invoked method: new_texture({width}, {height}, ..., {gfx_texture_id}) -> {texture:?}");
//d!("Invoked method: new_texture({}, {}, ..., {}) -> {:?}\n{}",
@@ -1273,7 +1291,7 @@ impl Stage {
fn trax_method(&self, epoch: EpochIndex, method: &GraphicsMethod) {
let mut trax = get_trax().lock();
match method {
GraphicsMethod::NewTexture((_, _, _, gtex_id, tag)) => {
GraphicsMethod::NewTexture((_, _, _, _, gtex_id, tag)) => {
trax.put_tex(epoch, *gtex_id, *tag);
}
GraphicsMethod::DeleteTexture((gtex_id, tag)) => {
@@ -1487,7 +1505,7 @@ impl PruneMethodHeap {
fn process_method(&mut self, mut method: GraphicsMethod) {
match &method {
GraphicsMethod::NewTexture((_, _, _, gtex_id, _)) => {
GraphicsMethod::NewTexture((_, _, _, _, gtex_id, _)) => {
if DEBUG_GFXAPI {
t!("Prune method: new_texture(..., {gtex_id})");
}

View File

@@ -17,6 +17,7 @@
*/
use crate::gfx::{DebugTag, ManagedTexturePtr, Rectangle, RenderApi};
use miniquad::TextureFormat;
use super::{
ft::{Sprite, SpritePtr},
@@ -163,8 +164,13 @@ impl<'a> Atlas<'a> {
assert_eq!(self.glyph_ids.len(), self.x_pos.len());
let atlas = self.render();
let texture =
self.render_api.new_texture(self.width as u16, self.height as u16, atlas, self.tag);
let texture = self.render_api.new_texture(
self.width as u16,
self.height as u16,
atlas,
TextureFormat::RGBA8,
self.tag,
);
let uv_rects = self.compute_uvs();
let glyph_ids = self.glyph_ids;

View File

@@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use miniquad::TextureId;
use miniquad::{TextureFormat, TextureId};
use crate::{
error::Result,
@@ -111,8 +111,9 @@ pub async fn make_texture_atlas(
}
// Finally allocate the texture
let texture_id =
render_api.new_texture(total_width as u16, total_height as u16, atlas_bmp).await?;
let texture_id = render_api
.new_texture(total_width as u16, total_height as u16, atlas_bmp, TextureFormat::RGBA8)
.await?;
Ok(RenderedAtlas { uv_rects, texture_id })
}

View File

@@ -16,6 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use miniquad::TextureFormat;
use crate::gfx::{DebugTag, ManagedTexturePtr, Rectangle, RenderApi};
/// Prevents render artifacts from aliasing.
@@ -177,8 +179,13 @@ impl<'a> Atlas<'a> {
assert_eq!(self.glyph_ids.len(), self.x_pos.len());
let atlas = self.render();
let texture =
self.render_api.new_texture(self.width as u16, self.height as u16, atlas, self.tag);
let texture = self.render_api.new_texture(
self.width as u16,
self.height as u16,
atlas,
TextureFormat::RGBA8,
self.tag,
);
let uv_rects = self.compute_uvs();
let glyph_ids = self.glyph_ids;

View File

@@ -22,6 +22,7 @@ use chrono::{Local, NaiveDate, TimeZone};
use darkfi_serial::{Decodable, FutAsyncWriteExt, SerialDecodable, SerialEncodable};
use futures::stream::{Stream, StreamExt};
use image::{ImageBuffer, ImageReader, Rgba};
use miniquad::TextureFormat;
use parking_lot::Mutex as SyncMutex;
use std::{
collections::HashMap,
@@ -755,7 +756,13 @@ impl FileMessage {
let bmp = img.as_raw().clone();
drop(imgbuf);
render_api.new_texture(width, height, bmp, gfxtag!("file_img_texture"))
render_api.new_texture(
width,
height,
bmp,
TextureFormat::RGBA8,
gfxtag!("file_img_texture"),
)
}
pub fn height(&self, line_height: f32) -> f32 {

View File

@@ -18,6 +18,7 @@
use async_trait::async_trait;
use image::ImageReader;
use miniquad::TextureFormat;
use parking_lot::Mutex as SyncMutex;
use rand::{rngs::OsRng, Rng};
use std::{io::Cursor, sync::Arc};
@@ -116,7 +117,7 @@ impl Image {
let height = img.height() as u16;
let bmp = img.into_raw();
self.render_api.new_texture(width, height, bmp, gfxtag!("img"))
self.render_api.new_texture(width, height, bmp, TextureFormat::RGBA8, gfxtag!("img"))
}
#[instrument(target = "ui::button")]