From 2ba5e47b590178da7400eec3f7c0a7e0fe864ecd Mon Sep 17 00:00:00 2001 From: Kamil Jarosz Date: Sun, 9 Jun 2024 11:23:54 +0200 Subject: [PATCH] render: Implement drawing lines using WebGL This patch provides an efficient implementation of draw_line and draw_line_rect for WebGL using Gl::LINE_STRIP. --- render/webgl/src/lib.rs | 27 +++++++++++++++++---------- render/wgpu/src/backend.rs | 2 +- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/render/webgl/src/lib.rs b/render/webgl/src/lib.rs index a77b705ad..b9168ae42 100644 --- a/render/webgl/src/lib.rs +++ b/render/webgl/src/lib.rs @@ -23,7 +23,7 @@ use ruffle_render::transform::Transform; use ruffle_web_common::{JsError, JsResult}; use std::borrow::Cow; use std::sync::Arc; -use swf::{BlendMode, Color}; +use swf::{BlendMode, Color, Twips}; use thiserror::Error; use wasm_bindgen::{JsCast, JsValue}; use web_sys::{ @@ -919,7 +919,7 @@ impl WebGlRenderBackend { } } - fn draw_quad(&mut self, color: Color, matrix: Matrix) { + fn draw_quad(&mut self, color: Color, matrix: Matrix) { let world_matrix = [ [matrix.a, matrix.b, 0.0, 0.0], [matrix.c, matrix.d, 0.0, 0.0], @@ -973,8 +973,13 @@ impl WebGlRenderBackend { let quad = &self.color_quad_draws; self.bind_vertex_array(Some(&quad[0].vao)); + let count = if COUNT < 0 { + quad[0].num_indices + } else { + COUNT + }; self.gl - .draw_elements_with_i32(Gl::TRIANGLE_FAN, quad[0].num_indices, Gl::UNSIGNED_INT, 0); + .draw_elements_with_i32(MODE, count, Gl::UNSIGNED_INT, 0); } } @@ -1496,15 +1501,19 @@ impl CommandHandler for WebGlRenderBackend { } fn draw_rect(&mut self, color: Color, matrix: Matrix) { - self.draw_quad(color, matrix); + self.draw_quad::<{ Gl::TRIANGLE_FAN }, -1>(color, matrix) } - fn draw_line(&mut self, _color: Color, _matrix: Matrix) { - // TODO implement + fn draw_line(&mut self, color: Color, mut matrix: Matrix) { + matrix.tx += Twips::HALF; + matrix.ty += Twips::HALF; + self.draw_quad::<{ Gl::LINE_STRIP }, 2>(color, matrix) } - fn draw_line_rect(&mut self, _color: Color, _matrix: Matrix) { - // TODO implement + fn draw_line_rect(&mut self, color: Color, mut matrix: Matrix) { + matrix.tx += Twips::HALF; + matrix.ty += Twips::HALF; + self.draw_quad::<{ Gl::LINE_LOOP }, -1>(color, matrix) } fn push_mask(&mut self) { @@ -1806,8 +1815,6 @@ impl ShaderProgram { } } -impl WebGlRenderBackend {} - trait GlExt { fn check_error(&self, error_msg: &'static str) -> Result<(), Error>; } diff --git a/render/wgpu/src/backend.rs b/render/wgpu/src/backend.rs index 92eff087d..cdb387925 100644 --- a/render/wgpu/src/backend.rs +++ b/render/wgpu/src/backend.rs @@ -37,7 +37,7 @@ use std::path::Path; use std::sync::Arc; use swf::Color; use tracing::instrument; -use wgpu::{Backend, SubmissionIndex}; +use wgpu::SubmissionIndex; pub struct WgpuRenderBackend { pub(crate) descriptors: Arc,