Mesa (master): r300g: add API for building command buffers

Marek Olšák mareko at kemper.freedesktop.org
Sun Jun 13 10:17:52 PDT 2010


Module: Mesa
Branch: master
Commit: a062156bb27d604abb19b4ad6a3668c42b1334ab
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=a062156bb27d604abb19b4ad6a3668c42b1334ab

Author: Marek Olšák <maraeo at gmail.com>
Date:   Sun Jun 13 00:31:04 2010 +0200

r300g: add API for building command buffers

The idea is to build a hardware command buffer for every CSO and memcpy
the buffer to a command stream at bind time (or dirty-state-emission time,
to be precise).

---

 src/gallium/drivers/r300/r300_cb.h |  133 ++++++++++++++++++++++++++++++++++++
 src/gallium/drivers/r300/r300_cs.h |   12 +++
 2 files changed, 145 insertions(+), 0 deletions(-)

diff --git a/src/gallium/drivers/r300/r300_cb.h b/src/gallium/drivers/r300/r300_cb.h
new file mode 100644
index 0000000..9345263
--- /dev/null
+++ b/src/gallium/drivers/r300/r300_cb.h
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2008 Corbin Simpson <MostAwesomeDude at gmail.com>
+ * Copyright 2010 Marek Olšák <maraeo at gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
+ * license, and/or sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE. */
+
+/**
+ * This file contains macros for building command buffers in memory.
+ *
+ * Use NEW_CB for buffers with a varying size and it will also allocate
+ * the buffer.
+ * Use BEGIN_CB for arrays with a static size.
+ *
+ * Example:
+ *
+ *     uint32_t cb[3];
+ *     CB_LOCALS;
+ *
+ *     BEGIN_CB(cb, 3);
+ *     OUT_CB_REG_SEQ(R500_RB3D_CONSTANT_COLOR_AR, 2);
+ *     OUT_CB(blend_color_red_alpha);
+ *     OUT_CB(blend_color_green_blue);
+ *     END_CB;
+ *
+ * And later:
+ *
+ *     CS_LOCALS;
+ *     WRITE_CS_TABLE(cb, 3);
+ *
+ * Or using a little slower variant:
+ *
+ *     CS_LOCALS;
+ *     BEGIN_CS(cb, 3);
+ *     OUT_CS_TABLE(cb, 3);
+ *     END_CS;
+ */
+
+#ifndef R300_CB_H
+#define R300_CB_H
+
+#include "r300_reg.h"
+
+/* Yes, I know macros are ugly. However, they are much prettier than the code
+ * that they neatly hide away, and don't have the cost of function setup, so
+ * we're going to use them. */
+
+
+/**
+ * Command buffer setup.
+ */
+
+#define CB_LOCALS \
+    int cs_count = 0; \
+    uint32_t *cs_ptr = NULL; \
+    (void) cs_count; (void) cs_ptr;
+
+#define NEW_CB(ptr, size) do { \
+    assert(sizeof(*ptr) == sizeof(uint32_t)); \
+    cs_ptr = (ptr) = (uint32_t*)malloc((size) * sizeof(uint32_t)); \
+    cs_count = size; \
+} while (0)
+
+#define BEGIN_CB(ptr, size) do { \
+    assert(sizeof(*ptr) == sizeof(uint32_t)); \
+    cs_ptr = ptr; \
+    cs_count = size; \
+} while (0)
+
+#define END_CB do { \
+    if (cs_count != 0) \
+        debug_printf("r300: Warning: cs_count off by %d at (%s, %s:%i)\n", \
+                     cs_count, __FUNCTION__, __FILE__, __LINE__); \
+} while (0)
+
+
+/**
+ * Storing pure DWORDs.
+ */
+
+#define OUT_CB(value) do { \
+    *cs_ptr = (value); \
+    cs_ptr++; \
+    cs_count--; \
+} while (0)
+
+#define OUT_CB_TABLE(values, count) do { \
+    memcpy(cs_ptr, values, count * sizeof(uint32_t)); \
+    cs_ptr += count; \
+    cs_count -= count; \
+} while (0)
+
+#define OUT_CB_32F(value) \
+    OUT_CB(fui(value));
+
+#define OUT_CB_REG(register, value) do { \
+    assert(register); \
+    OUT_CB(CP_PACKET0(register, 0)); \
+    OUT_CB(value); \
+} while (0)
+
+/* Note: This expects count to be the number of registers,
+ * not the actual packet0 count! */
+#define OUT_CB_REG_SEQ(register, count) do { \
+    assert(register); \
+    OUT_CB(CP_PACKET0(register, (count) - 1)); \
+} while (0)
+
+#define OUT_CB_ONE_REG(register, count) do { \
+    assert(register); \
+    OUT_CB(CP_PACKET0(register, (count) - 1) | RADEON_ONE_REG_WR); \
+} while (0)
+
+#define OUT_CB_PKT3(op, count) \
+    OUT_CB(CP_PACKET3(op, count))
+
+#endif /* R300_CB_H */
diff --git a/src/gallium/drivers/r300/r300_cs.h b/src/gallium/drivers/r300/r300_cs.h
index 1cdd4b8..1649766 100644
--- a/src/gallium/drivers/r300/r300_cs.h
+++ b/src/gallium/drivers/r300/r300_cs.h
@@ -56,6 +56,7 @@
     if (cs_count != 0) \
         debug_printf("r300: Warning: cs_count off by %d\n", cs_count); \
     cs_winsys->end_cs(cs_winsys, __FILE__, __FUNCTION__, __LINE__); \
+    cs_count = 0; \
 } while (0)
 
 
@@ -137,4 +138,15 @@
     cs_count -= 4; \
 } while (0)
 
+
+/**
+ * Command buffer emission.
+ */
+
+/* It's recommended not to call begin_cs/end_cs before/after this macro. */
+#define WRITE_CS_TABLE(values, count) do { \
+    assert(cs_count == 0); \
+    cs_winsys->write_cs_table(cs_winsys, values, count); \
+} while (0)
+
 #endif /* R300_CS_H */



More information about the mesa-commit mailing list