Merge remote-tracking branch 'origin/master' into vulkan
[mesa.git] / src / mesa / drivers / dri / i965 / gen8_sol_state.c
1 /*
2 * Copyright © 2012 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 /**
25 * @file gen8_sol_state.c
26 *
27 * Controls the stream output logic (SOL) stage of the gen8 hardware, which is
28 * used to implement GL_EXT_transform_feedback.
29 */
30
31 #include "brw_context.h"
32 #include "brw_state.h"
33 #include "brw_defines.h"
34 #include "intel_batchbuffer.h"
35 #include "intel_buffer_objects.h"
36 #include "main/transformfeedback.h"
37
38 static void
39 gen8_upload_3dstate_so_buffers(struct brw_context *brw)
40 {
41 struct gl_context *ctx = &brw->ctx;
42 /* BRW_NEW_TRANSFORM_FEEDBACK */
43 struct gl_transform_feedback_object *xfb_obj =
44 ctx->TransformFeedback.CurrentObject;
45 struct brw_transform_feedback_object *brw_obj =
46 (struct brw_transform_feedback_object *) xfb_obj;
47 uint32_t mocs_wb = brw->gen >= 9 ? SKL_MOCS_WB : BDW_MOCS_WB;
48
49 /* Set up the up to 4 output buffers. These are the ranges defined in the
50 * gl_transform_feedback_object.
51 */
52 for (int i = 0; i < 4; i++) {
53 struct intel_buffer_object *bufferobj =
54 intel_buffer_object(xfb_obj->Buffers[i]);
55
56 if (!bufferobj) {
57 BEGIN_BATCH(8);
58 OUT_BATCH(_3DSTATE_SO_BUFFER << 16 | (8 - 2));
59 OUT_BATCH((i << SO_BUFFER_INDEX_SHIFT));
60 OUT_BATCH(0);
61 OUT_BATCH(0);
62 OUT_BATCH(0);
63 OUT_BATCH(0);
64 OUT_BATCH(0);
65 OUT_BATCH(0);
66 ADVANCE_BATCH();
67 continue;
68 }
69
70 uint32_t start = xfb_obj->Offset[i];
71 assert(start % 4 == 0);
72 uint32_t end = ALIGN(start + xfb_obj->Size[i], 4);
73 drm_intel_bo *bo =
74 intel_bufferobj_buffer(brw, bufferobj, start, end - start);
75 assert(end <= bo->size);
76
77 perf_debug("Missing MOCS setup for 3DSTATE_SO_BUFFER.");
78
79 BEGIN_BATCH(8);
80 OUT_BATCH(_3DSTATE_SO_BUFFER << 16 | (8 - 2));
81 OUT_BATCH(GEN8_SO_BUFFER_ENABLE | (i << SO_BUFFER_INDEX_SHIFT) |
82 GEN8_SO_BUFFER_OFFSET_WRITE_ENABLE |
83 GEN8_SO_BUFFER_OFFSET_ADDRESS_ENABLE |
84 (mocs_wb << 22));
85 OUT_RELOC64(bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, start);
86 OUT_BATCH(xfb_obj->Size[i] / 4 - 1);
87 OUT_RELOC64(brw_obj->offset_bo,
88 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
89 i * sizeof(uint32_t));
90 if (brw_obj->zero_offsets)
91 OUT_BATCH(0); /* Zero out the offset and write that to offset_bo */
92 else
93 OUT_BATCH(0xFFFFFFFF); /* Use offset_bo as the "Stream Offset." */
94 ADVANCE_BATCH();
95 }
96 brw_obj->zero_offsets = false;
97 }
98
99 static void
100 gen8_upload_3dstate_streamout(struct brw_context *brw, bool active,
101 struct brw_vue_map *vue_map)
102 {
103 struct gl_context *ctx = &brw->ctx;
104
105 /* BRW_NEW_TRANSFORM_FEEDBACK */
106 struct gl_transform_feedback_object *xfb_obj =
107 ctx->TransformFeedback.CurrentObject;
108 const struct gl_transform_feedback_info *linked_xfb_info =
109 &xfb_obj->shader_program->LinkedTransformFeedback;
110 uint32_t dw1 = 0, dw2 = 0, dw3 = 0, dw4 = 0;
111
112 if (active) {
113 int urb_entry_read_offset = 0;
114 int urb_entry_read_length = (vue_map->num_slots + 1) / 2 -
115 urb_entry_read_offset;
116
117 dw1 |= SO_FUNCTION_ENABLE;
118 dw1 |= SO_STATISTICS_ENABLE;
119
120 /* _NEW_LIGHT */
121 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION)
122 dw1 |= SO_REORDER_TRAILING;
123
124 /* We always read the whole vertex. This could be reduced at some
125 * point by reading less and offsetting the register index in the
126 * SO_DECLs.
127 */
128 dw2 |= SET_FIELD(urb_entry_read_offset, SO_STREAM_0_VERTEX_READ_OFFSET);
129 dw2 |= SET_FIELD(urb_entry_read_length - 1, SO_STREAM_0_VERTEX_READ_LENGTH);
130
131 dw2 |= SET_FIELD(urb_entry_read_offset, SO_STREAM_1_VERTEX_READ_OFFSET);
132 dw2 |= SET_FIELD(urb_entry_read_length - 1, SO_STREAM_1_VERTEX_READ_LENGTH);
133
134 dw2 |= SET_FIELD(urb_entry_read_offset, SO_STREAM_2_VERTEX_READ_OFFSET);
135 dw2 |= SET_FIELD(urb_entry_read_length - 1, SO_STREAM_2_VERTEX_READ_LENGTH);
136
137 dw2 |= SET_FIELD(urb_entry_read_offset, SO_STREAM_3_VERTEX_READ_OFFSET);
138 dw2 |= SET_FIELD(urb_entry_read_length - 1, SO_STREAM_3_VERTEX_READ_LENGTH);
139
140 /* Set buffer pitches; 0 means unbound. */
141 if (xfb_obj->Buffers[0])
142 dw3 |= linked_xfb_info->BufferStride[0] * 4;
143 if (xfb_obj->Buffers[1])
144 dw3 |= (linked_xfb_info->BufferStride[1] * 4) << 16;
145 if (xfb_obj->Buffers[2])
146 dw4 |= linked_xfb_info->BufferStride[2] * 4;
147 if (xfb_obj->Buffers[3])
148 dw4 |= (linked_xfb_info->BufferStride[3] * 4) << 16;
149 }
150
151 BEGIN_BATCH(5);
152 OUT_BATCH(_3DSTATE_STREAMOUT << 16 | (5 - 2));
153 OUT_BATCH(dw1);
154 OUT_BATCH(dw2);
155 OUT_BATCH(dw3);
156 OUT_BATCH(dw4);
157 ADVANCE_BATCH();
158 }
159
160 static void
161 upload_sol_state(struct brw_context *brw)
162 {
163 struct gl_context *ctx = &brw->ctx;
164 /* BRW_NEW_TRANSFORM_FEEDBACK */
165 bool active = _mesa_is_xfb_active_and_unpaused(ctx);
166
167 if (active) {
168 gen8_upload_3dstate_so_buffers(brw);
169 /* BRW_NEW_VUE_MAP_GEOM_OUT */
170 gen7_upload_3dstate_so_decl_list(brw, &brw->vue_map_geom_out);
171 }
172
173 gen8_upload_3dstate_streamout(brw, active, &brw->vue_map_geom_out);
174 }
175
176 const struct brw_tracked_state gen8_sol_state = {
177 .dirty = {
178 .mesa = _NEW_LIGHT,
179 .brw = BRW_NEW_BATCH |
180 BRW_NEW_TRANSFORM_FEEDBACK |
181 BRW_NEW_VUE_MAP_GEOM_OUT,
182 },
183 .emit = upload_sol_state,
184 };