1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 /** \file gen6_sol.c
25  *
26  * Code to initialize the binding table entries used by transform feedback.
27  */
28 
29 #include "main/macros.h"
30 #include "brw_context.h"
31 #include "intel_batchbuffer.h"
32 #include "brw_defines.h"
33 #include "brw_state.h"
34 
35 static void
gen6_update_sol_surfaces(struct brw_context * brw)36 gen6_update_sol_surfaces(struct brw_context *brw)
37 {
38    struct gl_context *ctx = &brw->intel.ctx;
39    /* _NEW_TRANSFORM_FEEDBACK */
40    struct gl_transform_feedback_object *xfb_obj =
41       ctx->TransformFeedback.CurrentObject;
42    /* BRW_NEW_VERTEX_PROGRAM */
43    const struct gl_shader_program *shaderprog =
44       ctx->Shader.CurrentVertexProgram;
45    const struct gl_transform_feedback_info *linked_xfb_info =
46       &shaderprog->LinkedTransformFeedback;
47    int i;
48 
49    for (i = 0; i < BRW_MAX_SOL_BINDINGS; ++i) {
50       const int surf_index = SURF_INDEX_SOL_BINDING(i);
51       if (xfb_obj->Active && !xfb_obj->Paused &&
52           i < linked_xfb_info->NumOutputs) {
53          unsigned buffer = linked_xfb_info->Outputs[i].OutputBuffer;
54          unsigned buffer_offset =
55             xfb_obj->Offset[buffer] / 4 +
56             linked_xfb_info->Outputs[i].DstOffset;
57          brw_update_sol_surface(
58             brw, xfb_obj->Buffers[buffer], &brw->gs.surf_offset[surf_index],
59             linked_xfb_info->Outputs[i].NumComponents,
60             linked_xfb_info->BufferStride[buffer], buffer_offset);
61       } else {
62          brw->gs.surf_offset[surf_index] = 0;
63       }
64    }
65 
66    brw->state.dirty.brw |= BRW_NEW_SURFACES;
67 }
68 
69 const struct brw_tracked_state gen6_sol_surface = {
70    .dirty = {
71       .mesa = _NEW_TRANSFORM_FEEDBACK,
72       .brw = (BRW_NEW_BATCH |
73               BRW_NEW_VERTEX_PROGRAM),
74       .cache = 0
75    },
76    .emit = gen6_update_sol_surfaces,
77 };
78 
79 /**
80  * Constructs the binding table for the WM surface state, which maps unit
81  * numbers to surface state objects.
82  */
83 static void
brw_gs_upload_binding_table(struct brw_context * brw)84 brw_gs_upload_binding_table(struct brw_context *brw)
85 {
86    struct gl_context *ctx = &brw->intel.ctx;
87    /* BRW_NEW_VERTEX_PROGRAM */
88    const struct gl_shader_program *shaderprog =
89       ctx->Shader.CurrentVertexProgram;
90    bool has_surfaces = false;
91    uint32_t *bind;
92 
93    if (shaderprog) {
94       const struct gl_transform_feedback_info *linked_xfb_info =
95 	 &shaderprog->LinkedTransformFeedback;
96       /* Currently we only ever upload surfaces for SOL. */
97       has_surfaces = linked_xfb_info->NumOutputs != 0;
98    }
99 
100    /* Skip making a binding table if we don't have anything to put in it. */
101    if (!has_surfaces) {
102       if (brw->gs.bind_bo_offset != 0) {
103 	 brw->state.dirty.brw |= BRW_NEW_GS_BINDING_TABLE;
104 	 brw->gs.bind_bo_offset = 0;
105       }
106       return;
107    }
108 
109    /* Might want to calculate nr_surfaces first, to avoid taking up so much
110     * space for the binding table.
111     */
112    bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
113 			  sizeof(uint32_t) * BRW_MAX_GS_SURFACES,
114 			  32, &brw->gs.bind_bo_offset);
115 
116    /* BRW_NEW_SURFACES */
117    memcpy(bind, brw->gs.surf_offset, BRW_MAX_GS_SURFACES * sizeof(uint32_t));
118 
119    brw->state.dirty.brw |= BRW_NEW_GS_BINDING_TABLE;
120 }
121 
122 const struct brw_tracked_state gen6_gs_binding_table = {
123    .dirty = {
124       .mesa = 0,
125       .brw = (BRW_NEW_BATCH |
126 	      BRW_NEW_VERTEX_PROGRAM |
127 	      BRW_NEW_SURFACES),
128       .cache = 0
129    },
130    .emit = brw_gs_upload_binding_table,
131 };
132 
133 static void
gen6_update_sol_indices(struct brw_context * brw)134 gen6_update_sol_indices(struct brw_context *brw)
135 {
136    struct intel_context *intel = &brw->intel;
137 
138    BEGIN_BATCH(4);
139    OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
140    OUT_BATCH(0);
141    OUT_BATCH(brw->sol.svbi_0_starting_index); /* BRW_NEW_SOL_INDICES */
142    OUT_BATCH(brw->sol.svbi_0_max_index); /* BRW_NEW_SOL_INDICES */
143    ADVANCE_BATCH();
144 }
145 
146 const struct brw_tracked_state gen6_sol_indices = {
147    .dirty = {
148       .mesa = 0,
149       .brw = (BRW_NEW_BATCH |
150               BRW_NEW_SOL_INDICES),
151       .cache = 0
152    },
153    .emit = gen6_update_sol_indices,
154 };
155 
156 void
brw_begin_transform_feedback(struct gl_context * ctx,GLenum mode,struct gl_transform_feedback_object * obj)157 brw_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
158 			     struct gl_transform_feedback_object *obj)
159 {
160    struct brw_context *brw = brw_context(ctx);
161    const struct gl_shader_program *vs_prog =
162       ctx->Shader.CurrentVertexProgram;
163    const struct gl_transform_feedback_info *linked_xfb_info =
164       &vs_prog->LinkedTransformFeedback;
165    struct gl_transform_feedback_object *xfb_obj =
166       ctx->TransformFeedback.CurrentObject;
167 
168    unsigned max_index = 0xffffffff;
169 
170    /* Compute the maximum number of vertices that we can write without
171     * overflowing any of the buffers currently being used for feedback.
172     */
173    for (int i = 0; i < BRW_MAX_SOL_BUFFERS; ++i) {
174       unsigned stride = linked_xfb_info->BufferStride[i];
175 
176       /* Skip any inactive buffers, which have a stride of 0. */
177       if (stride == 0)
178 	 continue;
179 
180       unsigned max_for_this_buffer = xfb_obj->Size[i] / (4 * stride);
181       max_index = MIN2(max_index, max_for_this_buffer);
182    }
183 
184    /* Initialize the SVBI 0 register to zero and set the maximum index.
185     * These values will be sent to the hardware on the next draw.
186     */
187    brw->state.dirty.brw |= BRW_NEW_SOL_INDICES;
188    brw->sol.svbi_0_starting_index = 0;
189    brw->sol.svbi_0_max_index = max_index;
190    brw->sol.offset_0_batch_start = 0;
191 }
192 
193 void
brw_end_transform_feedback(struct gl_context * ctx,struct gl_transform_feedback_object * obj)194 brw_end_transform_feedback(struct gl_context *ctx,
195                            struct gl_transform_feedback_object *obj)
196 {
197    /* After EndTransformFeedback, it's likely that the client program will try
198     * to draw using the contents of the transform feedback buffer as vertex
199     * input.  In order for this to work, we need to flush the data through at
200     * least the GS stage of the pipeline, and flush out the render cache.  For
201     * simplicity, just do a full flush.
202     */
203    struct brw_context *brw = brw_context(ctx);
204    struct intel_context *intel = &brw->intel;
205    intel_batchbuffer_emit_mi_flush(intel);
206 }
207