render: Add WebGlRenderBackend::draw_quad

Moving draw_rect to draw_quad prevents code duplication when
methods related to drawing lines are added.
This commit is contained in:
Kamil Jarosz 2024-06-09 11:27:39 +02:00
parent fe08638d26
commit 07df84a486
1 changed files with 62 additions and 57 deletions

View File

@ -13,6 +13,7 @@ use ruffle_render::bitmap::{
};
use ruffle_render::commands::{CommandHandler, CommandList, RenderBlendMode};
use ruffle_render::error::Error as BitmapError;
use ruffle_render::matrix::Matrix;
use ruffle_render::quality::StageQuality;
use ruffle_render::shape_utils::{DistilledShape, GradientType};
use ruffle_render::tessellator::{
@ -913,6 +914,64 @@ impl WebGlRenderBackend {
self.apply_blend_mode(current.clone());
}
}
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],
[0.0, 0.0, 1.0, 0.0],
[
matrix.tx.to_pixels() as f32,
matrix.ty.to_pixels() as f32,
0.0,
1.0,
],
];
let mult_color = [
color.r as f32 * 255.0,
color.g as f32 * 255.0,
color.b as f32 * 255.0,
color.a as f32 * 255.0,
];
let add_color = [0.0; 4];
self.set_stencil_state();
let program = &self.color_program;
// Set common render state, while minimizing unnecessary state changes.
// TODO: Using designated layout specifiers in WebGL2/OpenGL ES 3, we could guarantee that uniforms
// are in the same location between shaders, and avoid changing them unless necessary.
if program as *const ShaderProgram != self.active_program {
self.gl.use_program(Some(&program.program));
self.active_program = program as *const ShaderProgram;
program.uniform_matrix4fv(&self.gl, ShaderUniform::ViewMatrix, &self.view_matrix);
self.mult_color = None;
self.add_color = None;
};
self.color_program
.uniform_matrix4fv(&self.gl, ShaderUniform::WorldMatrix, &world_matrix);
if Some(mult_color) != self.mult_color {
self.color_program
.uniform4fv(&self.gl, ShaderUniform::MultColor, &mult_color);
self.mult_color = Some(mult_color);
}
if Some(add_color) != self.add_color {
self.color_program
.uniform4fv(&self.gl, ShaderUniform::AddColor, &add_color);
self.add_color = Some(add_color);
}
let quad = &self.color_quad_draws;
self.bind_vertex_array(Some(&quad[0].vao));
self.gl
.draw_elements_with_i32(Gl::TRIANGLES, quad[0].num_indices, Gl::UNSIGNED_INT, 0);
}
}
fn same_blend_mode(first: Option<&RenderBlendMode>, second: &RenderBlendMode) -> bool {
@ -1207,7 +1266,7 @@ impl CommandHandler for WebGlRenderBackend {
// Scale the quad to the bitmap's dimensions.
let mut matrix = transform.matrix;
pixel_snapping.apply(&mut matrix);
matrix *= ruffle_render::matrix::Matrix::scale(entry.width as f32, entry.height as f32);
matrix *= Matrix::scale(entry.width as f32, entry.height as f32);
let world_matrix = [
[matrix.a, matrix.b, 0.0, 0.0],
@ -1432,62 +1491,8 @@ impl CommandHandler for WebGlRenderBackend {
panic!("Stage3D should not have been created on WebGL backend")
}
fn draw_rect(&mut self, color: Color, matrix: ruffle_render::matrix::Matrix) {
let world_matrix = [
[matrix.a, matrix.b, 0.0, 0.0],
[matrix.c, matrix.d, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[
matrix.tx.to_pixels() as f32,
matrix.ty.to_pixels() as f32,
0.0,
1.0,
],
];
let mult_color = [
color.r as f32 * 255.0,
color.g as f32 * 255.0,
color.b as f32 * 255.0,
color.a as f32 * 255.0,
];
let add_color = [0.0; 4];
self.set_stencil_state();
let program = &self.color_program;
// Set common render state, while minimizing unnecessary state changes.
// TODO: Using designated layout specifiers in WebGL2/OpenGL ES 3, we could guarantee that uniforms
// are in the same location between shaders, and avoid changing them unless necessary.
if program as *const ShaderProgram != self.active_program {
self.gl.use_program(Some(&program.program));
self.active_program = program as *const ShaderProgram;
program.uniform_matrix4fv(&self.gl, ShaderUniform::ViewMatrix, &self.view_matrix);
self.mult_color = None;
self.add_color = None;
};
self.color_program
.uniform_matrix4fv(&self.gl, ShaderUniform::WorldMatrix, &world_matrix);
if Some(mult_color) != self.mult_color {
self.color_program
.uniform4fv(&self.gl, ShaderUniform::MultColor, &mult_color);
self.mult_color = Some(mult_color);
}
if Some(add_color) != self.add_color {
self.color_program
.uniform4fv(&self.gl, ShaderUniform::AddColor, &add_color);
self.add_color = Some(add_color);
}
let quad = &self.color_quad_draws;
self.bind_vertex_array(Some(&quad[0].vao));
self.gl
.draw_elements_with_i32(Gl::TRIANGLES, quad[0].num_indices, Gl::UNSIGNED_INT, 0);
fn draw_rect(&mut self, color: Color, matrix: Matrix) {
self.draw_quad(color, matrix);
}
fn push_mask(&mut self) {