r300g: move two-sided stencilref fallback to its own file
authorMarek Olšák <maraeo@gmail.com>
Sat, 12 Jun 2010 19:30:37 +0000 (21:30 +0200)
committerMarek Olšák <maraeo@gmail.com>
Sun, 13 Jun 2010 15:43:37 +0000 (17:43 +0200)
src/gallium/drivers/r300/Makefile
src/gallium/drivers/r300/SConscript
src/gallium/drivers/r300/r300_context.h
src/gallium/drivers/r300/r300_render.c
src/gallium/drivers/r300/r300_render_stencilref.c [new file with mode: 0644]

index edd15f6a66dd3350833da97e5b86614c189cf3fa..dd897f6072fb646912685af14d252bb70a74068c 100644 (file)
@@ -14,6 +14,7 @@ C_SOURCES = \
        r300_hyperz.c \
        r300_query.c \
        r300_render.c \
+       r300_render_stencilref.c \
        r300_render_translate.c \
        r300_resource.c \
        r300_screen.c \
index cef49c9ed4e22bc7c16b90ccf582d0a5798295f6..ee19e9d27837179f9bbab0d2150d04ec161c23a2 100644 (file)
@@ -24,6 +24,7 @@ r300 = env.ConvenienceLibrary(
         'r300_hyperz.c',
         'r300_query.c',
         'r300_render.c',
+        'r300_render_stencilref.c',
         'r300_render_translate.c',
         'r300_resource.c',
         'r300_screen.c',
index 0c5256184a526747217c6345ddf76ed9d7da2d2a..766d09425d12c2c608d6464f9f05e63f801bdf11 100644 (file)
@@ -512,6 +512,9 @@ void r300_translate_index_buffer(struct r300_context *r300,
                                  unsigned *index_size, unsigned index_offset,
                                  unsigned *start, unsigned count);
 
+/* r300_render_stencilref.c */
+void r300_plug_in_stencil_ref_fallback(struct r300_context *r300);
+
 boolean r300_check_cs(struct r300_context *r300, unsigned size);
 void r300_finish(struct r300_context *r300);
 void r500_dump_rs_block(struct r300_rs_block *rs);
index e83dbab092e5dd7bfa8e895569d6b7091f35ce4a..95a20c1fd4e5f305b377bc7c3918f608a0863967 100644 (file)
@@ -1013,135 +1013,6 @@ struct draw_stage* r300_draw_stage(struct r300_context* r300)
     return stage;
 }
 
-/****************************************************************************
- * Two-sided stencil reference value fallback. It's designed to be as much
- * separate from rest of the driver as possible.
- ***************************************************************************/
-
-struct r300_stencilref_context {
-    void (*draw_arrays)(struct pipe_context *pipe,
-                        unsigned mode, unsigned start, unsigned count);
-
-    void (*draw_range_elements)(
-        struct pipe_context *pipe, struct pipe_resource *indexBuffer,
-        unsigned indexSize, int indexBias, unsigned minIndex, unsigned maxIndex,
-        unsigned mode, unsigned start, unsigned count);
-
-    uint32_t rs_cull_mode;
-    uint32_t zb_stencilrefmask;
-    ubyte ref_value_front;
-};
-
-static boolean r300_stencilref_needed(struct r300_context *r300)
-{
-    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
-
-    return dsa->two_sided_stencil_ref ||
-           (dsa->two_sided &&
-            r300->stencil_ref.ref_value[0] != r300->stencil_ref.ref_value[1]);
-}
-
-/* Set drawing for front faces. */
-static void r300_stencilref_begin(struct r300_context *r300)
-{
-    struct r300_stencilref_context *sr = r300->stencilref_fallback;
-    struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
-    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
-
-    /* Save state. */
-    sr->rs_cull_mode = rs->cull_mode;
-    sr->zb_stencilrefmask = dsa->stencil_ref_mask;
-    sr->ref_value_front = r300->stencil_ref.ref_value[0];
-
-    /* We *cull* pixels, therefore no need to mask out the bits. */
-    rs->cull_mode |= R300_CULL_BACK;
-
-    r300->rs_state.dirty = TRUE;
-}
-
-/* Set drawing for back faces. */
-static void r300_stencilref_switch_side(struct r300_context *r300)
-{
-    struct r300_stencilref_context *sr = r300->stencilref_fallback;
-    struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
-    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
-
-    rs->cull_mode = sr->rs_cull_mode | R300_CULL_FRONT;
-    dsa->stencil_ref_mask = dsa->stencil_ref_bf;
-    r300->stencil_ref.ref_value[0] = r300->stencil_ref.ref_value[1];
-
-    r300->rs_state.dirty = TRUE;
-    r300->dsa_state.dirty = TRUE;
-}
-
-/* Restore the original state. */
-static void r300_stencilref_end(struct r300_context *r300)
-{
-    struct r300_stencilref_context *sr = r300->stencilref_fallback;
-    struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
-    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
-
-    /* Restore state. */
-    rs->cull_mode = sr->rs_cull_mode;
-    dsa->stencil_ref_mask = sr->zb_stencilrefmask;
-    r300->stencil_ref.ref_value[0] = sr->ref_value_front;
-
-    r300->rs_state.dirty = TRUE;
-    r300->dsa_state.dirty = TRUE;
-}
-
-static void r300_stencilref_draw_arrays(struct pipe_context *pipe, unsigned mode,
-                                        unsigned start, unsigned count)
-{
-    struct r300_context *r300 = r300_context(pipe);
-    struct r300_stencilref_context *sr = r300->stencilref_fallback;
-
-    if (!r300_stencilref_needed(r300)) {
-        sr->draw_arrays(pipe, mode, start, count);
-    } else {
-        r300_stencilref_begin(r300);
-        sr->draw_arrays(pipe, mode, start, count);
-        r300_stencilref_switch_side(r300);
-        sr->draw_arrays(pipe, mode, start, count);
-        r300_stencilref_end(r300);
-    }
-}
-
-static void r300_stencilref_draw_range_elements(
-    struct pipe_context *pipe, struct pipe_resource *indexBuffer,
-    unsigned indexSize, int indexBias, unsigned minIndex, unsigned maxIndex,
-    unsigned mode, unsigned start, unsigned count)
-{
-    struct r300_context *r300 = r300_context(pipe);
-    struct r300_stencilref_context *sr = r300->stencilref_fallback;
-
-    if (!r300_stencilref_needed(r300)) {
-        sr->draw_range_elements(pipe, indexBuffer, indexSize, indexBias,
-                                minIndex, maxIndex, mode, start, count);
-    } else {
-        r300_stencilref_begin(r300);
-        sr->draw_range_elements(pipe, indexBuffer, indexSize, indexBias,
-                                minIndex, maxIndex, mode, start, count);
-        r300_stencilref_switch_side(r300);
-        sr->draw_range_elements(pipe, indexBuffer, indexSize, indexBias,
-                                minIndex, maxIndex, mode, start, count);
-        r300_stencilref_end(r300);
-    }
-}
-
-static void r300_plug_in_stencil_ref_fallback(struct r300_context *r300)
-{
-    r300->stencilref_fallback = CALLOC_STRUCT(r300_stencilref_context);
-
-    /* Save original draw functions. */
-    r300->stencilref_fallback->draw_arrays = r300->context.draw_arrays;
-    r300->stencilref_fallback->draw_range_elements = r300->context.draw_range_elements;
-
-    /* Override the draw functions. */
-    r300->context.draw_arrays = r300_stencilref_draw_arrays;
-    r300->context.draw_range_elements = r300_stencilref_draw_range_elements;
-}
-
 void r300_init_render_functions(struct r300_context *r300)
 {
     /* Set generic functions. */
@@ -1156,7 +1027,7 @@ void r300_init_render_functions(struct r300_context *r300)
         r300->context.draw_range_elements = r300_swtcl_draw_range_elements;
     }
 
-    /* Plug in two-sided stencil reference value fallback if needed. */
+    /* Plug in the two-sided stencil reference value fallback if needed. */
     if (!r300->screen->caps.is_r500)
         r300_plug_in_stencil_ref_fallback(r300);
 }
diff --git a/src/gallium/drivers/r300/r300_render_stencilref.c b/src/gallium/drivers/r300/r300_render_stencilref.c
new file mode 100644 (file)
index 0000000..d509ded
--- /dev/null
@@ -0,0 +1,158 @@
+/*
+ * Copyright 2010 Marek Olšák <maraeo@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. */
+
+/**
+ * The two-sided stencil reference value fallback for r3xx-r4xx chips.
+ * These chips support two-sided stencil functions but they do not support
+ * a two-sided reference value.
+ *
+ * The functions below split every draw call which uses the two-sided
+ * reference value into two draw calls -- the first one renders front faces
+ * and the second renders back faces with the other reference value.
+ */
+
+#include "r300_context.h"
+#include "r300_reg.h"
+
+struct r300_stencilref_context {
+    void (*draw_arrays)(struct pipe_context *pipe,
+                        unsigned mode, unsigned start, unsigned count);
+
+    void (*draw_range_elements)(
+        struct pipe_context *pipe, struct pipe_resource *indexBuffer,
+        unsigned indexSize, int indexBias, unsigned minIndex, unsigned maxIndex,
+        unsigned mode, unsigned start, unsigned count);
+
+    uint32_t rs_cull_mode;
+    uint32_t zb_stencilrefmask;
+    ubyte ref_value_front;
+};
+
+static boolean r300_stencilref_needed(struct r300_context *r300)
+{
+    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
+
+    return dsa->two_sided_stencil_ref ||
+           (dsa->two_sided &&
+            r300->stencil_ref.ref_value[0] != r300->stencil_ref.ref_value[1]);
+}
+
+/* Set drawing for front faces. */
+static void r300_stencilref_begin(struct r300_context *r300)
+{
+    struct r300_stencilref_context *sr = r300->stencilref_fallback;
+    struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
+    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
+
+    /* Save state. */
+    sr->rs_cull_mode = rs->cull_mode;
+    sr->zb_stencilrefmask = dsa->stencil_ref_mask;
+    sr->ref_value_front = r300->stencil_ref.ref_value[0];
+
+    /* We *cull* pixels, therefore no need to mask out the bits. */
+    rs->cull_mode |= R300_CULL_BACK;
+
+    r300->rs_state.dirty = TRUE;
+}
+
+/* Set drawing for back faces. */
+static void r300_stencilref_switch_side(struct r300_context *r300)
+{
+    struct r300_stencilref_context *sr = r300->stencilref_fallback;
+    struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
+    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
+
+    rs->cull_mode = sr->rs_cull_mode | R300_CULL_FRONT;
+    dsa->stencil_ref_mask = dsa->stencil_ref_bf;
+    r300->stencil_ref.ref_value[0] = r300->stencil_ref.ref_value[1];
+
+    r300->rs_state.dirty = TRUE;
+    r300->dsa_state.dirty = TRUE;
+}
+
+/* Restore the original state. */
+static void r300_stencilref_end(struct r300_context *r300)
+{
+    struct r300_stencilref_context *sr = r300->stencilref_fallback;
+    struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
+    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
+
+    /* Restore state. */
+    rs->cull_mode = sr->rs_cull_mode;
+    dsa->stencil_ref_mask = sr->zb_stencilrefmask;
+    r300->stencil_ref.ref_value[0] = sr->ref_value_front;
+
+    r300->rs_state.dirty = TRUE;
+    r300->dsa_state.dirty = TRUE;
+}
+
+static void r300_stencilref_draw_arrays(struct pipe_context *pipe, unsigned mode,
+                                        unsigned start, unsigned count)
+{
+    struct r300_context *r300 = r300_context(pipe);
+    struct r300_stencilref_context *sr = r300->stencilref_fallback;
+
+    if (!r300_stencilref_needed(r300)) {
+        sr->draw_arrays(pipe, mode, start, count);
+    } else {
+        r300_stencilref_begin(r300);
+        sr->draw_arrays(pipe, mode, start, count);
+        r300_stencilref_switch_side(r300);
+        sr->draw_arrays(pipe, mode, start, count);
+        r300_stencilref_end(r300);
+    }
+}
+
+static void r300_stencilref_draw_range_elements(
+    struct pipe_context *pipe, struct pipe_resource *indexBuffer,
+    unsigned indexSize, int indexBias, unsigned minIndex, unsigned maxIndex,
+    unsigned mode, unsigned start, unsigned count)
+{
+    struct r300_context *r300 = r300_context(pipe);
+    struct r300_stencilref_context *sr = r300->stencilref_fallback;
+
+    if (!r300_stencilref_needed(r300)) {
+        sr->draw_range_elements(pipe, indexBuffer, indexSize, indexBias,
+                                minIndex, maxIndex, mode, start, count);
+    } else {
+        r300_stencilref_begin(r300);
+        sr->draw_range_elements(pipe, indexBuffer, indexSize, indexBias,
+                                minIndex, maxIndex, mode, start, count);
+        r300_stencilref_switch_side(r300);
+        sr->draw_range_elements(pipe, indexBuffer, indexSize, indexBias,
+                                minIndex, maxIndex, mode, start, count);
+        r300_stencilref_end(r300);
+    }
+}
+
+void r300_plug_in_stencil_ref_fallback(struct r300_context *r300)
+{
+    r300->stencilref_fallback = CALLOC_STRUCT(r300_stencilref_context);
+
+    /* Save original draw functions. */
+    r300->stencilref_fallback->draw_arrays = r300->context.draw_arrays;
+    r300->stencilref_fallback->draw_range_elements = r300->context.draw_range_elements;
+
+    /* Override the draw functions. */
+    r300->context.draw_arrays = r300_stencilref_draw_arrays;
+    r300->context.draw_range_elements = r300_stencilref_draw_range_elements;
+}