rusticl: Wrap pipe query reads
authorDr. David Alan Gilbert <dave@treblig.org>
Tue, 11 Jul 2023 20:34:04 +0000 (21:34 +0100)
committerMarge Bot <emma+marge@anholt.net>
Wed, 19 Jul 2023 10:29:25 +0000 (10:29 +0000)
Take a query we previously created and read it's result.
The type of the result is usually implicitly known; for now
just handle the query we use in 64 bit.

This is safe because the trait bindings ensure that
when we create a query with PipeQueryGen we embed the type
of the result in the PipeQuery, and that produces the correct
result type.

Signed-off-by: Dr. David Alan Gilbert <dave@treblig.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24101>

src/gallium/frontends/rusticl/mesa/pipe/context.rs
src/gallium/frontends/rusticl/mesa/pipe/query.rs

index 404bf21..c3ddf76 100644 (file)
@@ -529,6 +529,15 @@ impl PipeContext {
         unsafe { self.pipe.as_ref().end_query.unwrap()(self.pipe.as_ptr(), pq) }
     }
 
+    pub fn get_query_result(
+        &self,
+        pq: *mut pipe_query,
+        wait: bool,
+        pqr: *mut pipe_query_result,
+    ) -> bool {
+        unsafe { self.pipe.as_ref().get_query_result.unwrap()(self.pipe.as_ptr(), pq, wait, pqr) }
+    }
+
     pub fn destroy_query(&self, pq: *mut pipe_query) {
         unsafe { self.pipe.as_ref().destroy_query.unwrap()(self.pipe.as_ptr(), pq) }
     }
index f58c43f..603f47e 100644 (file)
@@ -63,3 +63,28 @@ impl<'a, R> Drop for PipeQuery<'a, R> {
         self.ctx.destroy_query(self.query);
     }
 }
+
+pub trait QueryReadTrait {
+    type ResType;
+    fn read(&mut self, wait: bool) -> Option<Self::ResType>;
+
+    fn read_blocked(&mut self) -> Self::ResType {
+        self.read(true).unwrap()
+    }
+}
+
+impl QueryReadTrait for PipeQuery<'_, u64> {
+    type ResType = u64;
+
+    fn read(&mut self, wait: bool) -> Option<u64> {
+        let mut raw_result = pipe_query_result::default();
+        if self.ctx.get_query_result(self.query, wait, &mut raw_result) {
+            // SAFETY: We know this is the right type
+            // because of the trait bound on PipeQueryGen binds the
+            // query type with the result type.
+            Some(unsafe { raw_result.u64_ })
+        } else {
+            None
+        }
+    }
+}