i965: Tell the unit states how many binding table entries we have.
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_gs_state.c
1 /*
2 * Copyright © 2013 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "brw_context.h"
25 #include "brw_state.h"
26 #include "brw_defines.h"
27 #include "intel_batchbuffer.h"
28
29
30 static void
31 gen7_upload_gs_push_constants(struct brw_context *brw)
32 {
33 /* BRW_NEW_GEOMETRY_PROGRAM */
34 const struct brw_geometry_program *vp =
35 (struct brw_geometry_program *) brw->geometry_program;
36 if (!vp)
37 return;
38
39 /* CACHE_NEW_GS_PROG */
40 const struct brw_vec4_prog_data *prog_data = &brw->gs.prog_data->base;
41 struct brw_stage_state *stage_state = &brw->gs.base;
42
43 gen6_upload_vec4_push_constants(brw, &vp->program.Base, prog_data,
44 stage_state, AUB_TRACE_VS_CONSTANTS);
45 }
46
47 const struct brw_tracked_state gen7_gs_push_constants = {
48 .dirty = {
49 .mesa = _NEW_TRANSFORM | _NEW_PROGRAM_CONSTANTS,
50 .brw = (BRW_NEW_BATCH |
51 BRW_NEW_GEOMETRY_PROGRAM),
52 .cache = CACHE_NEW_GS_PROG,
53 },
54 .emit = gen7_upload_gs_push_constants,
55 };
56
57
58 static void
59 upload_gs_state(struct brw_context *brw)
60 {
61 const struct brw_stage_state *stage_state = &brw->gs.base;
62 const int max_threads_shift = brw->is_haswell ?
63 HSW_GS_MAX_THREADS_SHIFT : GEN6_GS_MAX_THREADS_SHIFT;
64 /* BRW_NEW_GEOMETRY_PROGRAM */
65 bool active = brw->geometry_program;
66 /* CACHE_NEW_GS_PROG */
67 const struct brw_vec4_prog_data *prog_data = &brw->gs.prog_data->base;
68
69 /* BRW_NEW_GS_BINDING_TABLE */
70 BEGIN_BATCH(2);
71 OUT_BATCH(_3DSTATE_BINDING_TABLE_POINTERS_GS << 16 | (2 - 2));
72 OUT_BATCH(stage_state->bind_bo_offset);
73 ADVANCE_BATCH();
74
75 /* CACHE_NEW_SAMPLER */
76 BEGIN_BATCH(2);
77 OUT_BATCH(_3DSTATE_SAMPLER_STATE_POINTERS_GS << 16 | (2 - 2));
78 OUT_BATCH(stage_state->sampler_offset);
79 ADVANCE_BATCH();
80
81 gen7_upload_constant_state(brw, stage_state, active, _3DSTATE_CONSTANT_GS);
82
83 if (active) {
84 BEGIN_BATCH(7);
85 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2));
86 OUT_BATCH(stage_state->prog_offset);
87 OUT_BATCH(((ALIGN(stage_state->sampler_count, 4)/4) <<
88 GEN6_GS_SAMPLER_COUNT_SHIFT) |
89 ((brw->gs.prog_data->base.base.binding_table.size_bytes / 4) <<
90 GEN6_GS_BINDING_TABLE_ENTRY_COUNT_SHIFT));
91
92 if (brw->gs.prog_data->base.total_scratch) {
93 OUT_RELOC(stage_state->scratch_bo,
94 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
95 ffs(brw->gs.prog_data->base.total_scratch) - 11);
96 } else {
97 OUT_BATCH(0);
98 }
99
100 uint32_t dw4 =
101 ((brw->gs.prog_data->output_vertex_size_hwords * 2 - 1) <<
102 GEN7_GS_OUTPUT_VERTEX_SIZE_SHIFT) |
103 (brw->gs.prog_data->output_topology <<
104 GEN7_GS_OUTPUT_TOPOLOGY_SHIFT) |
105 (prog_data->urb_read_length <<
106 GEN6_GS_URB_READ_LENGTH_SHIFT) |
107 (0 << GEN6_GS_URB_ENTRY_READ_OFFSET_SHIFT) |
108 (prog_data->dispatch_grf_start_reg <<
109 GEN6_GS_DISPATCH_START_GRF_SHIFT);
110
111 /* Note: the meaning of the GEN7_GS_REORDER_TRAILING bit changes between
112 * Ivy Bridge and Haswell.
113 *
114 * On Ivy Bridge, setting this bit causes the vertices of a triangle
115 * strip to be delivered to the geometry shader in an order that does
116 * not strictly follow the OpenGL spec, but preserves triangle
117 * orientation. For example, if the vertices are (1, 2, 3, 4, 5), then
118 * the geometry shader sees triangles:
119 *
120 * (1, 2, 3), (2, 4, 3), (3, 4, 5)
121 *
122 * (Clearing the bit is even worse, because it fails to preserve
123 * orientation).
124 *
125 * Triangle strips with adjacency always ordered in a way that preserves
126 * triangle orientation but does not strictly follow the OpenGL spec,
127 * regardless of the setting of this bit.
128 *
129 * On Haswell, both triangle strips and triangle strips with adjacency
130 * are always ordered in a way that preserves triangle orientation.
131 * Setting this bit causes the ordering to strictly follow the OpenGL
132 * spec.
133 *
134 * So in either case we want to set the bit. Unfortunately on Ivy
135 * Bridge this will get the order close to correct but not perfect.
136 */
137 uint32_t dw5 =
138 ((brw->max_gs_threads - 1) << max_threads_shift) |
139 (brw->gs.prog_data->control_data_header_size_hwords <<
140 GEN7_GS_CONTROL_DATA_HEADER_SIZE_SHIFT) |
141 (brw->gs.prog_data->dual_instanced_dispatch ?
142 GEN7_GS_DISPATCH_MODE_DUAL_INSTANCE :
143 GEN7_GS_DISPATCH_MODE_DUAL_OBJECT) |
144 GEN6_GS_STATISTICS_ENABLE |
145 (brw->gs.prog_data->include_primitive_id ?
146 GEN7_GS_INCLUDE_PRIMITIVE_ID : 0) |
147 GEN7_GS_REORDER_TRAILING |
148 GEN7_GS_ENABLE;
149 uint32_t dw6 = 0;
150
151 if (brw->is_haswell) {
152 dw6 |= brw->gs.prog_data->control_data_format <<
153 HSW_GS_CONTROL_DATA_FORMAT_SHIFT;
154 } else {
155 dw5 |= brw->gs.prog_data->control_data_format <<
156 IVB_GS_CONTROL_DATA_FORMAT_SHIFT;
157 }
158
159 OUT_BATCH(dw4);
160 OUT_BATCH(dw5);
161 OUT_BATCH(dw6);
162 ADVANCE_BATCH();
163 } else {
164 BEGIN_BATCH(7);
165 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2));
166 OUT_BATCH(0); /* prog_bo */
167 OUT_BATCH((0 << GEN6_GS_SAMPLER_COUNT_SHIFT) |
168 (0 << GEN6_GS_BINDING_TABLE_ENTRY_COUNT_SHIFT));
169 OUT_BATCH(0); /* scratch space base offset */
170 OUT_BATCH((1 << GEN6_GS_DISPATCH_START_GRF_SHIFT) |
171 (0 << GEN6_GS_URB_READ_LENGTH_SHIFT) |
172 GEN7_GS_INCLUDE_VERTEX_HANDLES |
173 (0 << GEN6_GS_URB_ENTRY_READ_OFFSET_SHIFT));
174 OUT_BATCH((0 << GEN6_GS_MAX_THREADS_SHIFT) |
175 GEN6_GS_STATISTICS_ENABLE);
176 OUT_BATCH(0);
177 ADVANCE_BATCH();
178 }
179 }
180
181 const struct brw_tracked_state gen7_gs_state = {
182 .dirty = {
183 .mesa = _NEW_TRANSFORM | _NEW_PROGRAM_CONSTANTS,
184 .brw = (BRW_NEW_CONTEXT |
185 BRW_NEW_GEOMETRY_PROGRAM |
186 BRW_NEW_GS_BINDING_TABLE |
187 BRW_NEW_BATCH |
188 BRW_NEW_PUSH_CONSTANT_ALLOCATION),
189 .cache = CACHE_NEW_GS_PROG | CACHE_NEW_SAMPLER
190 },
191 .emit = upload_gs_state,
192 };