i965: Enable EGL_KHR_gl_texture_3D_image
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_vs_state.c
1 /*
2 * Copyright © 2009 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 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "brw_context.h"
29 #include "brw_state.h"
30 #include "brw_defines.h"
31 #include "brw_util.h"
32 #include "program/prog_parameter.h"
33 #include "program/prog_statevars.h"
34 #include "main/shaderapi.h"
35 #include "intel_batchbuffer.h"
36
37 static void
38 gen6_upload_vs_push_constants(struct brw_context *brw)
39 {
40 struct brw_stage_state *stage_state = &brw->vs.base;
41
42 /* _BRW_NEW_VERTEX_PROGRAM */
43 const struct brw_vertex_program *vp =
44 brw_vertex_program_const(brw->vertex_program);
45 /* BRW_NEW_VS_PROG_DATA */
46 const struct brw_stage_prog_data *prog_data = &brw->vs.prog_data->base.base;
47
48 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_VERTEX);
49 gen6_upload_push_constants(brw, &vp->program.Base, prog_data,
50 stage_state, AUB_TRACE_VS_CONSTANTS);
51
52 if (brw->gen >= 7) {
53 if (brw->gen == 7 && !brw->is_haswell && !brw->is_baytrail)
54 gen7_emit_vs_workaround_flush(brw);
55
56 gen7_upload_constant_state(brw, stage_state, true /* active */,
57 _3DSTATE_CONSTANT_VS);
58 }
59 }
60
61 const struct brw_tracked_state gen6_vs_push_constants = {
62 .dirty = {
63 .mesa = _NEW_PROGRAM_CONSTANTS |
64 _NEW_TRANSFORM,
65 .brw = BRW_NEW_BATCH |
66 BRW_NEW_BLORP |
67 BRW_NEW_PUSH_CONSTANT_ALLOCATION |
68 BRW_NEW_VERTEX_PROGRAM |
69 BRW_NEW_VS_PROG_DATA,
70 },
71 .emit = gen6_upload_vs_push_constants,
72 };
73
74 static void
75 upload_vs_state(struct brw_context *brw)
76 {
77 const struct gen_device_info *devinfo = &brw->screen->devinfo;
78 const struct brw_stage_state *stage_state = &brw->vs.base;
79 uint32_t floating_point_mode = 0;
80
81 /* From the BSpec, 3D Pipeline > Geometry > Vertex Shader > State,
82 * 3DSTATE_VS, Dword 5.0 "VS Function Enable":
83 *
84 * [DevSNB] A pipeline flush must be programmed prior to a 3DSTATE_VS
85 * command that causes the VS Function Enable to toggle. Pipeline
86 * flush can be executed by sending a PIPE_CONTROL command with CS
87 * stall bit set and a post sync operation.
88 *
89 * We've already done such a flush at the start of state upload, so we
90 * don't need to do another one here.
91 */
92
93 if (stage_state->push_const_size == 0) {
94 /* Disable the push constant buffers. */
95 BEGIN_BATCH(5);
96 OUT_BATCH(_3DSTATE_CONSTANT_VS << 16 | (5 - 2));
97 OUT_BATCH(0);
98 OUT_BATCH(0);
99 OUT_BATCH(0);
100 OUT_BATCH(0);
101 ADVANCE_BATCH();
102 } else {
103 BEGIN_BATCH(5);
104 OUT_BATCH(_3DSTATE_CONSTANT_VS << 16 |
105 GEN6_CONSTANT_BUFFER_0_ENABLE |
106 (5 - 2));
107 /* Pointer to the VS constant buffer. Covered by the set of
108 * state flags from gen6_upload_vs_constants
109 */
110 OUT_BATCH(stage_state->push_const_offset +
111 stage_state->push_const_size - 1);
112 OUT_BATCH(0);
113 OUT_BATCH(0);
114 OUT_BATCH(0);
115 ADVANCE_BATCH();
116 }
117
118 if (brw->vs.prog_data->base.base.use_alt_mode)
119 floating_point_mode = GEN6_VS_FLOATING_POINT_MODE_ALT;
120
121 BEGIN_BATCH(6);
122 OUT_BATCH(_3DSTATE_VS << 16 | (6 - 2));
123 OUT_BATCH(stage_state->prog_offset);
124 OUT_BATCH(floating_point_mode |
125 ((ALIGN(stage_state->sampler_count, 4)/4) << GEN6_VS_SAMPLER_COUNT_SHIFT) |
126 ((brw->vs.prog_data->base.base.binding_table.size_bytes / 4) <<
127 GEN6_VS_BINDING_TABLE_ENTRY_COUNT_SHIFT));
128
129 if (brw->vs.prog_data->base.base.total_scratch) {
130 OUT_RELOC(stage_state->scratch_bo,
131 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
132 ffs(stage_state->per_thread_scratch) - 11);
133 } else {
134 OUT_BATCH(0);
135 }
136
137 OUT_BATCH((brw->vs.prog_data->base.base.dispatch_grf_start_reg <<
138 GEN6_VS_DISPATCH_START_GRF_SHIFT) |
139 (brw->vs.prog_data->base.urb_read_length << GEN6_VS_URB_READ_LENGTH_SHIFT) |
140 (0 << GEN6_VS_URB_ENTRY_READ_OFFSET_SHIFT));
141
142 OUT_BATCH(((devinfo->max_vs_threads - 1) << GEN6_VS_MAX_THREADS_SHIFT) |
143 GEN6_VS_STATISTICS_ENABLE |
144 GEN6_VS_ENABLE);
145 ADVANCE_BATCH();
146
147 /* Based on my reading of the simulator, the VS constants don't get
148 * pulled into the VS FF unit until an appropriate pipeline flush
149 * happens, and instead the 3DSTATE_CONSTANT_VS packet just adds
150 * references to them into a little FIFO. The flushes are common,
151 * but don't reliably happen between this and a 3DPRIMITIVE, causing
152 * the primitive to use the wrong constants. Then the FIFO
153 * containing the constant setup gets added to again on the next
154 * constants change, and eventually when a flush does happen the
155 * unit is overwhelmed by constant changes and dies.
156 *
157 * To avoid this, send a PIPE_CONTROL down the line that will
158 * update the unit immediately loading the constants. The flush
159 * type bits here were those set by the STATE_BASE_ADDRESS whose
160 * move in a82a43e8d99e1715dd11c9c091b5ab734079b6a6 triggered the
161 * bug reports that led to this workaround, and may be more than
162 * what is strictly required to avoid the issue.
163 */
164 brw_emit_pipe_control_flush(brw,
165 PIPE_CONTROL_DEPTH_STALL |
166 PIPE_CONTROL_INSTRUCTION_INVALIDATE |
167 PIPE_CONTROL_STATE_CACHE_INVALIDATE);
168 }
169
170 const struct brw_tracked_state gen6_vs_state = {
171 .dirty = {
172 .mesa = _NEW_PROGRAM_CONSTANTS |
173 _NEW_TRANSFORM,
174 .brw = BRW_NEW_BATCH |
175 BRW_NEW_BLORP |
176 BRW_NEW_CONTEXT |
177 BRW_NEW_PUSH_CONSTANT_ALLOCATION |
178 BRW_NEW_VERTEX_PROGRAM |
179 BRW_NEW_VS_PROG_DATA,
180 },
181 .emit = upload_vs_state,
182 };