mesa: add/update comments in _mesa_copy_buffer_subdata()
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_sol.c
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
34 static void
35 gen6_update_sol_surfaces(struct brw_context *brw)
36 {
37 struct gl_context *ctx = &brw->intel.ctx;
38 /* _NEW_TRANSFORM_FEEDBACK */
39 struct gl_transform_feedback_object *xfb_obj =
40 ctx->TransformFeedback.CurrentObject;
41 /* BRW_NEW_VERTEX_PROGRAM */
42 const struct gl_shader_program *shaderprog =
43 ctx->Shader.CurrentVertexProgram;
44 const struct gl_transform_feedback_info *linked_xfb_info =
45 &shaderprog->LinkedTransformFeedback;
46 int i;
47
48 for (i = 0; i < BRW_MAX_SOL_BINDINGS; ++i) {
49 const int surf_index = SURF_INDEX_SOL_BINDING(i);
50 if (xfb_obj->Active && !xfb_obj->Paused &&
51 i < linked_xfb_info->NumOutputs) {
52 unsigned buffer = linked_xfb_info->Outputs[i].OutputBuffer;
53 unsigned buffer_offset =
54 xfb_obj->Offset[buffer] / 4 +
55 linked_xfb_info->Outputs[i].DstOffset;
56 brw_update_sol_surface(
57 brw, xfb_obj->Buffers[buffer], &brw->bind.surf_offset[surf_index],
58 linked_xfb_info->Outputs[i].NumComponents,
59 linked_xfb_info->BufferStride[buffer], buffer_offset);
60 } else {
61 brw->bind.surf_offset[surf_index] = 0;
62 }
63 }
64
65 brw->state.dirty.brw |= BRW_NEW_SURFACES;
66 }
67
68 const struct brw_tracked_state gen6_sol_surface = {
69 .dirty = {
70 .mesa = _NEW_TRANSFORM_FEEDBACK,
71 .brw = (BRW_NEW_BATCH |
72 BRW_NEW_VERTEX_PROGRAM),
73 .cache = 0
74 },
75 .emit = gen6_update_sol_surfaces,
76 };
77
78 static void
79 gen6_update_sol_indices(struct brw_context *brw)
80 {
81 struct intel_context *intel = &brw->intel;
82
83 BEGIN_BATCH(4);
84 OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
85 OUT_BATCH(0);
86 OUT_BATCH(brw->sol.svbi_0_starting_index); /* BRW_NEW_SOL_INDICES */
87 OUT_BATCH(brw->sol.svbi_0_max_index); /* BRW_NEW_SOL_INDICES */
88 ADVANCE_BATCH();
89 }
90
91 const struct brw_tracked_state gen6_sol_indices = {
92 .dirty = {
93 .mesa = 0,
94 .brw = (BRW_NEW_BATCH |
95 BRW_NEW_SOL_INDICES),
96 .cache = 0
97 },
98 .emit = gen6_update_sol_indices,
99 };
100
101 void
102 brw_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
103 struct gl_transform_feedback_object *obj)
104 {
105 struct brw_context *brw = brw_context(ctx);
106 const struct gl_shader_program *vs_prog =
107 ctx->Shader.CurrentVertexProgram;
108 const struct gl_transform_feedback_info *linked_xfb_info =
109 &vs_prog->LinkedTransformFeedback;
110 struct gl_transform_feedback_object *xfb_obj =
111 ctx->TransformFeedback.CurrentObject;
112
113 unsigned max_index = 0xffffffff;
114
115 /* Compute the maximum number of vertices that we can write without
116 * overflowing any of the buffers currently being used for feedback.
117 */
118 for (int i = 0; i < BRW_MAX_SOL_BUFFERS; ++i) {
119 unsigned stride = linked_xfb_info->BufferStride[i];
120
121 /* Skip any inactive buffers, which have a stride of 0. */
122 if (stride == 0)
123 continue;
124
125 unsigned max_for_this_buffer = xfb_obj->Size[i] / (4 * stride);
126 max_index = MIN2(max_index, max_for_this_buffer);
127 }
128
129 /* Initialize the SVBI 0 register to zero and set the maximum index.
130 * These values will be sent to the hardware on the next draw.
131 */
132 brw->state.dirty.brw |= BRW_NEW_SOL_INDICES;
133 brw->sol.svbi_0_starting_index = 0;
134 brw->sol.svbi_0_max_index = max_index;
135 brw->sol.offset_0_batch_start = 0;
136 }
137
138 void
139 brw_end_transform_feedback(struct gl_context *ctx,
140 struct gl_transform_feedback_object *obj)
141 {
142 /* After EndTransformFeedback, it's likely that the client program will try
143 * to draw using the contents of the transform feedback buffer as vertex
144 * input. In order for this to work, we need to flush the data through at
145 * least the GS stage of the pipeline, and flush out the render cache. For
146 * simplicity, just do a full flush.
147 */
148 struct brw_context *brw = brw_context(ctx);
149 struct intel_context *intel = &brw->intel;
150 intel_batchbuffer_emit_mi_flush(intel);
151 }