Skip to content

Commit

Permalink
gles: GPU scopes support
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Sep 28, 2024
1 parent ade6968 commit 58b2d3f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
88 changes: 36 additions & 52 deletions blade-graphics/src/gles/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use std::{str, time::Duration};

const COLOR_ATTACHMENTS: &[u32] = &[
glow::COLOR_ATTACHMENT0,
Expand Down Expand Up @@ -82,6 +82,13 @@ impl crate::ShaderBindable for super::AccelerationStructure {

impl super::CommandEncoder {
fn begin_pass(&mut self, label: &str) {
if self.needs_scopes {
let start = self.string_data.len();
self.string_data.extend_from_slice(label.as_bytes());
self.commands.push(super::Command::PushScope {
name_range: start..self.string_data.len(),
});
}
if let Some(ref mut timing_datas) = self.timing_datas {
let td = timing_datas.first_mut().unwrap();
let id = td.pass_names.len();
Expand All @@ -92,9 +99,22 @@ impl super::CommandEncoder {
}
}

fn pass<P>(&mut self, kind: super::PassKind) -> super::PassEncoder<P> {
super::PassEncoder {
commands: &mut self.commands,
plain_data: &mut self.plain_data,
kind,
invalidate_attachments: Vec::new(),
pipeline: Default::default(),
limits: &self.limits,
has_scope: self.needs_scopes,
}
}

pub fn start(&mut self) {
self.commands.clear();
self.plain_data.clear();
self.string_data.clear();
self.has_present = false;
}

Expand Down Expand Up @@ -147,14 +167,7 @@ impl super::CommandEncoder {

pub fn transfer(&mut self, label: &str) -> super::PassEncoder<()> {
self.begin_pass(label);
super::PassEncoder {
commands: &mut self.commands,
plain_data: &mut self.plain_data,
kind: super::PassKind::Transfer,
invalidate_attachments: Vec::new(),
pipeline: Default::default(),
limits: &self.limits,
}
self.pass(super::PassKind::Transfer)
}

pub fn acceleration_structure(&mut self, _label: &str) -> super::PassEncoder<()> {
Expand All @@ -163,14 +176,7 @@ impl super::CommandEncoder {

pub fn compute(&mut self, label: &str) -> super::PassEncoder<super::ComputePipeline> {
self.begin_pass(label);
super::PassEncoder {
commands: &mut self.commands,
plain_data: &mut self.plain_data,
kind: super::PassKind::Compute,
invalidate_attachments: Vec::new(),
pipeline: Default::default(),
limits: &self.limits,
}
self.pass(super::PassKind::Compute)
}

pub fn render(
Expand Down Expand Up @@ -250,14 +256,9 @@ impl super::CommandEncoder {
}
}

super::PassEncoder {
commands: &mut self.commands,
plain_data: &mut self.plain_data,
kind: super::PassKind::Render,
invalidate_attachments,
pipeline: Default::default(),
limits: &self.limits,
}
let mut pass = self.pass(super::PassKind::Render);
pass.invalidate_attachments = invalidate_attachments;
pass
}

pub fn timings(&self) -> &[(String, Duration)] {
Expand Down Expand Up @@ -329,6 +330,9 @@ impl<T> Drop for super::PassEncoder<'_, T> {
self.commands.push(super::Command::ResetFramebuffer);
}
}
if self.has_scope {
self.commands.push(super::Command::PopScope);
}
}
}

Expand Down Expand Up @@ -401,33 +405,6 @@ impl crate::traits::TransferEncoder for super::PassEncoder<'_, ()> {
}
}

#[hidden_trait::expose]
impl crate::traits::AccelerationStructureEncoder for super::PassEncoder<'_, ()> {
type AccelerationStructure = crate::AccelerationStructure;
type AccelerationStructureMesh = crate::AccelerationStructureMesh;
type BufferPiece = crate::BufferPiece;

fn build_bottom_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_meshes: &[crate::AccelerationStructureMesh],
_scratch_data: crate::BufferPiece,
) {
unimplemented!()
}

fn build_top_level(
&mut self,
_acceleration_structure: super::AccelerationStructure,
_bottom_level: &[super::AccelerationStructure],
_instance_count: u32,
_instance_data: crate::BufferPiece,
_scratch_data: crate::BufferPiece,
) {
unimplemented!()
}
}

#[hidden_trait::expose]
impl crate::traits::PipelineEncoder for super::PipelineEncoder<'_> {
fn bind<D: crate::ShaderData>(&mut self, group: u32, data: &D) {
Expand Down Expand Up @@ -1048,6 +1025,13 @@ impl super::Command {
Self::QueryCounter { query } => {
gl.query_counter(query, glow::TIMESTAMP);
}
Self::PushScope { ref name_range } => {
let name = str::from_utf8(&ec.string_data[name_range.clone()]).unwrap();
gl.push_debug_group(glow::DEBUG_SOURCE_APPLICATION, super::DEBUG_ID, name);
}
Self::PopScope => {
gl.pop_debug_group();
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions blade-graphics/src/gles/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,11 @@ impl EglContext {
);

let toggles = super::Toggles {
scoping: desc.capture
&& (gl.supports_debug() || {
log::warn!("Scoping is not supported");
false
}),
timing: desc.timing
&& (extensions.contains("GL_EXT_disjoint_timer_query") || {
log::warn!("Timing is not supported");
Expand Down
14 changes: 13 additions & 1 deletion blade-graphics/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod pipeline;
mod platform;
mod resource;

use std::{marker::PhantomData, ops::Range, time::Duration};
use std::{marker::PhantomData, mem, ops::Range, time::Duration};

type BindTarget = u32;
const DEBUG_ID: u32 = 0;
Expand All @@ -27,6 +27,7 @@ struct Limits {

#[derive(Debug, Default)]
struct Toggles {
scoping: bool,
timing: bool,
}

Expand Down Expand Up @@ -349,6 +350,10 @@ enum Command {
QueryCounter {
query: glow::Query,
},
PushScope {
name_range: Range<usize>,
},
PopScope,
}

struct TimingData {
Expand All @@ -360,6 +365,8 @@ pub struct CommandEncoder {
name: String,
commands: Vec<Command>,
plain_data: Vec<u8>,
string_data: Vec<u8>,
needs_scopes: bool,
has_present: bool,
limits: Limits,
timing_datas: Option<Box<[TimingData]>>,
Expand All @@ -380,6 +387,7 @@ pub struct PassEncoder<'a, P> {
invalidate_attachments: Vec<u32>,
pipeline: PhantomData<P>,
limits: &'a Limits,
has_scope: bool,
}

pub type ComputeCommandEncoder<'a> = PassEncoder<'a, ComputePipeline>;
Expand Down Expand Up @@ -420,6 +428,7 @@ pub struct SyncPoint {
struct ExecutionContext {
framebuf: glow::Framebuffer,
plain_buffer: glow::Buffer,
string_data: Box<[u8]>,
}

impl Context {
Expand Down Expand Up @@ -463,6 +472,8 @@ impl crate::traits::CommandDevice for Context {
name: desc.name.to_string(),
commands: Vec::new(),
plain_data: Vec::new(),
string_data: Vec::new(),
needs_scopes: self.toggles.scoping,
has_present: false,
limits: self.limits.clone(),
timing_datas,
Expand Down Expand Up @@ -510,6 +521,7 @@ impl crate::traits::CommandDevice for Context {
ExecutionContext {
framebuf,
plain_buffer,
string_data: mem::take(&mut encoder.string_data).into_boxed_slice(),
}
};
for command in encoder.commands.iter() {
Expand Down

0 comments on commit 58b2d3f

Please sign in to comment.