Просмотр исходного кода

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

darkfi 7 месяцев назад
Родитель
Сommit
187829a873

+ 20 - 4
bin/app/bin/drawsim.rs

@@ -30,7 +30,8 @@ use miniquad::{
     conf, window, Backend, Bindings, BlendFactor, BlendState, BlendValue, BufferLayout,
     conf, window, Backend, Bindings, BlendFactor, BlendState, BlendValue, BufferLayout,
     BufferSource, BufferType, BufferUsage, Equation, EventHandler, KeyCode, KeyMods, MouseButton,
     BufferSource, BufferType, BufferUsage, Equation, EventHandler, KeyCode, KeyMods, MouseButton,
     PassAction, Pipeline, PipelineParams, RenderingBackend, ShaderMeta, ShaderSource, TouchPhase,
     PassAction, Pipeline, PipelineParams, RenderingBackend, ShaderMeta, ShaderSource, TouchPhase,
-    UniformDesc, UniformType, VertexAttribute, VertexFormat,
+    TextureFormat, TextureKind, TextureParams, TextureWrap, UniformDesc, UniformType,
+    VertexAttribute, VertexFormat,
     UniformBlockLayout,
     UniformBlockLayout,
 };
 };
 
 
@@ -293,8 +294,8 @@ impl Stage {
     fn process_method(&mut self, method: GraphicsMethod) {
     fn process_method(&mut self, method: GraphicsMethod) {
         //println!("Received method: {:?}", method);
         //println!("Received method: {:?}", method);
         match 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::DeleteTexture(texture) => self.method_delete_texture(texture),
             GraphicsMethod::NewVertexBuffer((verts, sendr)) => {
             GraphicsMethod::NewVertexBuffer((verts, sendr)) => {
@@ -313,9 +314,24 @@ impl Stage {
         width: u16,
         width: u16,
         height: u16,
         height: u16,
         data: Vec<u8>,
         data: Vec<u8>,
+        fmt: TextureFormat,
         gfx_texture_id: GfxTextureId,
         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 {
         if DEBUG_GFXAPI {
             println!("Invoked method: new_texture({}, {}, ..., {}) -> {:?}",
             println!("Invoked method: new_texture({}, {}, ..., {}) -> {:?}",
                    width, height, gfx_texture_id, texture);
                    width, height, gfx_texture_id, texture);

+ 28 - 10
bin/app/src/gfx/mod.rs

@@ -25,8 +25,9 @@ use miniquad::native::egl;
 use miniquad::{
 use miniquad::{
     conf, window, Backend, Bindings, BlendFactor, BlendState, BlendValue, BufferLayout,
     conf, window, Backend, Bindings, BlendFactor, BlendState, BlendValue, BufferLayout,
     BufferSource, BufferType, BufferUsage, Equation, EventHandler, KeyCode, KeyMods, MouseButton,
     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 parking_lot::Mutex as SyncMutex;
 use std::{
 use std::{
@@ -216,11 +217,12 @@ impl RenderApi {
         width: u16,
         width: u16,
         height: u16,
         height: u16,
         data: Vec<u8>,
         data: Vec<u8>,
+        fmt: TextureFormat,
         tag: DebugTag,
         tag: DebugTag,
     ) -> (TextureId, EpochIndex) {
     ) -> (TextureId, EpochIndex) {
         let gfx_texture_id = NEXT_TEXTURE_ID.fetch_add(1, Ordering::Relaxed);
         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);
         let epoch = self.send(method);
 
 
         (gfx_texture_id, epoch)
         (gfx_texture_id, epoch)
@@ -231,9 +233,10 @@ impl RenderApi {
         width: u16,
         width: u16,
         height: u16,
         height: u16,
         data: Vec<u8>,
         data: Vec<u8>,
+        fmt: TextureFormat,
         tag: DebugTag,
         tag: DebugTag,
     ) -> ManagedTexturePtr {
     ) -> 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 })
         Arc::new(ManagedTexture { id, epoch, render_api: self.clone(), tag })
     }
     }
 
 
@@ -733,7 +736,7 @@ type DcId = u64;
 
 
 #[derive(Clone)]
 #[derive(Clone)]
 pub enum GraphicsMethod {
 pub enum GraphicsMethod {
-    NewTexture((u16, u16, Vec<u8>, TextureId, DebugTag)),
+    NewTexture((u16, u16, Vec<u8>, TextureFormat, TextureId, DebugTag)),
     DeleteTexture((TextureId, DebugTag)),
     DeleteTexture((TextureId, DebugTag)),
     NewVertexBuffer((Vec<Vertex>, BufferId, DebugTag)),
     NewVertexBuffer((Vec<Vertex>, BufferId, DebugTag)),
     NewIndexBuffer((Vec<u16>, BufferId, DebugTag)),
     NewIndexBuffer((Vec<u16>, BufferId, DebugTag)),
@@ -1045,8 +1048,8 @@ impl Stage {
     fn process_method(&mut self, mut method: GraphicsMethod) {
     fn process_method(&mut self, mut method: GraphicsMethod) {
         //d!("Received method: {method:?}");
         //d!("Received method: {method:?}");
         match &mut 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::DeleteTexture((gtex_id, _)) => self.method_delete_texture(*gtex_id),
             GraphicsMethod::NewVertexBuffer((verts, gbuff_id, _)) => {
             GraphicsMethod::NewVertexBuffer((verts, gbuff_id, _)) => {
@@ -1122,9 +1125,24 @@ impl Stage {
         width: u16,
         width: u16,
         height: u16,
         height: u16,
         data: &Vec<u8>,
         data: &Vec<u8>,
+        fmt: TextureFormat,
         gfx_texture_id: TextureId,
         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 {
         if DEBUG_GFXAPI {
             d!("Invoked method: new_texture({width}, {height}, ..., {gfx_texture_id}) -> {texture:?}");
             d!("Invoked method: new_texture({width}, {height}, ..., {gfx_texture_id}) -> {texture:?}");
             //d!("Invoked method: new_texture({}, {}, ..., {}) -> {:?}\n{}",
             //d!("Invoked method: new_texture({}, {}, ..., {}) -> {:?}\n{}",
@@ -1273,7 +1291,7 @@ impl Stage {
     fn trax_method(&self, epoch: EpochIndex, method: &GraphicsMethod) {
     fn trax_method(&self, epoch: EpochIndex, method: &GraphicsMethod) {
         let mut trax = get_trax().lock();
         let mut trax = get_trax().lock();
         match method {
         match method {
-            GraphicsMethod::NewTexture((_, _, _, gtex_id, tag)) => {
+            GraphicsMethod::NewTexture((_, _, _, _, gtex_id, tag)) => {
                 trax.put_tex(epoch, *gtex_id, *tag);
                 trax.put_tex(epoch, *gtex_id, *tag);
             }
             }
             GraphicsMethod::DeleteTexture((gtex_id, tag)) => {
             GraphicsMethod::DeleteTexture((gtex_id, tag)) => {
@@ -1487,7 +1505,7 @@ impl PruneMethodHeap {
 
 
     fn process_method(&mut self, mut method: GraphicsMethod) {
     fn process_method(&mut self, mut method: GraphicsMethod) {
         match &method {
         match &method {
-            GraphicsMethod::NewTexture((_, _, _, gtex_id, _)) => {
+            GraphicsMethod::NewTexture((_, _, _, _, gtex_id, _)) => {
                 if DEBUG_GFXAPI {
                 if DEBUG_GFXAPI {
                     t!("Prune method: new_texture(..., {gtex_id})");
                     t!("Prune method: new_texture(..., {gtex_id})");
                 }
                 }

+ 8 - 2
bin/app/src/text/atlas.rs

@@ -17,6 +17,7 @@
  */
  */
 
 
 use crate::gfx::{DebugTag, ManagedTexturePtr, Rectangle, RenderApi};
 use crate::gfx::{DebugTag, ManagedTexturePtr, Rectangle, RenderApi};
+use miniquad::TextureFormat;
 
 
 use super::{
 use super::{
     ft::{Sprite, SpritePtr},
     ft::{Sprite, SpritePtr},
@@ -163,8 +164,13 @@ impl<'a> Atlas<'a> {
         assert_eq!(self.glyph_ids.len(), self.x_pos.len());
         assert_eq!(self.glyph_ids.len(), self.x_pos.len());
 
 
         let atlas = self.render();
         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 uv_rects = self.compute_uvs();
         let glyph_ids = self.glyph_ids;
         let glyph_ids = self.glyph_ids;

+ 4 - 3
bin/app/src/text/old_atlas.rs

@@ -16,7 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
  */
 
 
-use miniquad::TextureId;
+use miniquad::{TextureFormat, TextureId};
 
 
 use crate::{
 use crate::{
     error::Result,
     error::Result,
@@ -111,8 +111,9 @@ pub async fn make_texture_atlas(
     }
     }
 
 
     // Finally allocate the texture
     // 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 })
     Ok(RenderedAtlas { uv_rects, texture_id })
 }
 }

+ 9 - 2
bin/app/src/text2/atlas.rs

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

+ 8 - 1
bin/app/src/ui/chatview/page.rs

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

+ 2 - 1
bin/app/src/ui/image.rs

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