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 (at) anholt.net> 25 * 26 */ 27 28 #include "brw_context.h" 29 #include "brw_state.h" 30 #include "brw_defines.h" 31 #include "intel_batchbuffer.h" 32 #include "main/shaderapi.h" 33 34 static void 35 gen6_upload_gs_push_constants(struct brw_context *brw) 36 { 37 struct brw_stage_state *stage_state = &brw->gs.base; 38 39 /* BRW_NEW_GEOMETRY_PROGRAM */ 40 const struct brw_program *gp = brw_program_const(brw->geometry_program); 41 42 if (gp) { 43 /* BRW_NEW_GS_PROG_DATA */ 44 struct brw_stage_prog_data *prog_data = brw->gs.base.prog_data; 45 46 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_GEOMETRY); 47 gen6_upload_push_constants(brw, &gp->program, prog_data, stage_state, 48 AUB_TRACE_VS_CONSTANTS); 49 } 50 51 if (brw->gen >= 7) 52 gen7_upload_constant_state(brw, stage_state, gp, _3DSTATE_CONSTANT_GS); 53 } 54 55 const struct brw_tracked_state gen6_gs_push_constants = { 56 .dirty = { 57 .mesa = _NEW_PROGRAM_CONSTANTS | 58 _NEW_TRANSFORM, 59 .brw = BRW_NEW_BATCH | 60 BRW_NEW_BLORP | 61 BRW_NEW_GEOMETRY_PROGRAM | 62 BRW_NEW_GS_PROG_DATA | 63 BRW_NEW_PUSH_CONSTANT_ALLOCATION, 64 }, 65 .emit = gen6_upload_gs_push_constants, 66 }; 67 68 static void 69 upload_gs_state_for_tf(struct brw_context *brw) 70 { 71 const struct gen_device_info *devinfo = &brw->screen->devinfo; 72 73 BEGIN_BATCH(7); 74 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2)); 75 OUT_BATCH(brw->ff_gs.prog_offset); 76 OUT_BATCH(GEN6_GS_SPF_MODE | GEN6_GS_VECTOR_MASK_ENABLE); 77 OUT_BATCH(0); /* no scratch space */ 78 OUT_BATCH((2 << GEN6_GS_DISPATCH_START_GRF_SHIFT) | 79 (brw->ff_gs.prog_data->urb_read_length << GEN6_GS_URB_READ_LENGTH_SHIFT)); 80 OUT_BATCH(((devinfo->max_gs_threads - 1) << GEN6_GS_MAX_THREADS_SHIFT) | 81 GEN6_GS_STATISTICS_ENABLE | 82 GEN6_GS_SO_STATISTICS_ENABLE | 83 GEN6_GS_RENDERING_ENABLE); 84 OUT_BATCH(GEN6_GS_SVBI_PAYLOAD_ENABLE | 85 GEN6_GS_SVBI_POSTINCREMENT_ENABLE | 86 (brw->ff_gs.prog_data->svbi_postincrement_value << 87 GEN6_GS_SVBI_POSTINCREMENT_VALUE_SHIFT) | 88 GEN6_GS_ENABLE); 89 ADVANCE_BATCH(); 90 } 91 92 static void 93 upload_gs_state(struct brw_context *brw) 94 { 95 const struct gen_device_info *devinfo = &brw->screen->devinfo; 96 /* BRW_NEW_GEOMETRY_PROGRAM */ 97 bool active = brw->geometry_program; 98 /* BRW_NEW_GS_PROG_DATA */ 99 const struct brw_stage_state *stage_state = &brw->gs.base; 100 const struct brw_stage_prog_data *prog_data = stage_state->prog_data; 101 const struct brw_vue_prog_data *vue_prog_data = 102 brw_vue_prog_data(stage_state->prog_data); 103 104 if (!active || stage_state->push_const_size == 0) { 105 /* Disable the push constant buffers. */ 106 BEGIN_BATCH(5); 107 OUT_BATCH(_3DSTATE_CONSTANT_GS << 16 | (5 - 2)); 108 OUT_BATCH(0); 109 OUT_BATCH(0); 110 OUT_BATCH(0); 111 OUT_BATCH(0); 112 ADVANCE_BATCH(); 113 } else { 114 BEGIN_BATCH(5); 115 OUT_BATCH(_3DSTATE_CONSTANT_GS << 16 | 116 GEN6_CONSTANT_BUFFER_0_ENABLE | 117 (5 - 2)); 118 /* Pointer to the GS constant buffer. Covered by the set of 119 * state flags from gen6_upload_vs_constants 120 */ 121 OUT_BATCH(stage_state->push_const_offset + 122 stage_state->push_const_size - 1); 123 OUT_BATCH(0); 124 OUT_BATCH(0); 125 OUT_BATCH(0); 126 ADVANCE_BATCH(); 127 } 128 129 if (active) { 130 BEGIN_BATCH(7); 131 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2)); 132 OUT_BATCH(stage_state->prog_offset); 133 134 /* GEN6_GS_SPF_MODE and GEN6_GS_VECTOR_MASK_ENABLE are enabled as it 135 * was previously done for gen6. 136 * 137 * TODO: test with both disabled to see if the HW is behaving 138 * as expected, like in gen7. 139 */ 140 OUT_BATCH(GEN6_GS_SPF_MODE | GEN6_GS_VECTOR_MASK_ENABLE | 141 ((ALIGN(stage_state->sampler_count, 4)/4) << 142 GEN6_GS_SAMPLER_COUNT_SHIFT) | 143 ((prog_data->binding_table.size_bytes / 4) << 144 GEN6_GS_BINDING_TABLE_ENTRY_COUNT_SHIFT)); 145 146 if (prog_data->total_scratch) { 147 OUT_RELOC(stage_state->scratch_bo, 148 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 149 ffs(stage_state->per_thread_scratch) - 11); 150 } else { 151 OUT_BATCH(0); /* no scratch space */ 152 } 153 154 OUT_BATCH((vue_prog_data->urb_read_length << 155 GEN6_GS_URB_READ_LENGTH_SHIFT) | 156 (0 << GEN6_GS_URB_ENTRY_READ_OFFSET_SHIFT) | 157 (prog_data->dispatch_grf_start_reg << 158 GEN6_GS_DISPATCH_START_GRF_SHIFT)); 159 160 OUT_BATCH(((devinfo->max_gs_threads - 1) << GEN6_GS_MAX_THREADS_SHIFT) | 161 GEN6_GS_STATISTICS_ENABLE | 162 GEN6_GS_SO_STATISTICS_ENABLE | 163 GEN6_GS_RENDERING_ENABLE); 164 165 if (brw->geometry_program->info.has_transform_feedback_varyings) { 166 /* GEN6_GS_REORDER is equivalent to GEN7_GS_REORDER_TRAILING 167 * in gen7. SNB and IVB specs are the same regarding the reordering of 168 * TRISTRIP/TRISTRIP_REV vertices and triangle orientation, so we do 169 * the same thing in both generations. For more details, see the 170 * comment in gen7_gs_state.c 171 */ 172 OUT_BATCH(GEN6_GS_REORDER | 173 GEN6_GS_SVBI_PAYLOAD_ENABLE | 174 GEN6_GS_ENABLE); 175 } else { 176 OUT_BATCH(GEN6_GS_REORDER | GEN6_GS_ENABLE); 177 } 178 ADVANCE_BATCH(); 179 } else if (brw->ff_gs.prog_active) { 180 /* In gen6, transform feedback for the VS stage is done with an ad-hoc GS 181 * program. This function provides the needed 3DSTATE_GS for this. 182 */ 183 upload_gs_state_for_tf(brw); 184 } else { 185 /* No GS function required */ 186 BEGIN_BATCH(7); 187 OUT_BATCH(_3DSTATE_GS << 16 | (7 - 2)); 188 OUT_BATCH(0); /* prog_bo */ 189 OUT_BATCH((0 << GEN6_GS_SAMPLER_COUNT_SHIFT) | 190 (0 << GEN6_GS_BINDING_TABLE_ENTRY_COUNT_SHIFT)); 191 OUT_BATCH(0); /* scratch space base offset */ 192 OUT_BATCH((1 << GEN6_GS_DISPATCH_START_GRF_SHIFT) | 193 (0 << GEN6_GS_URB_READ_LENGTH_SHIFT) | 194 (0 << GEN6_GS_URB_ENTRY_READ_OFFSET_SHIFT)); 195 OUT_BATCH((0 << GEN6_GS_MAX_THREADS_SHIFT) | 196 GEN6_GS_STATISTICS_ENABLE | 197 GEN6_GS_RENDERING_ENABLE); 198 OUT_BATCH(0); 199 ADVANCE_BATCH(); 200 } 201 brw->gs.enabled = active; 202 } 203 204 const struct brw_tracked_state gen6_gs_state = { 205 .dirty = { 206 .mesa = _NEW_PROGRAM_CONSTANTS | 207 _NEW_TRANSFORM, 208 .brw = BRW_NEW_BATCH | 209 BRW_NEW_BLORP | 210 BRW_NEW_CONTEXT | 211 BRW_NEW_FF_GS_PROG_DATA | 212 BRW_NEW_GEOMETRY_PROGRAM | 213 BRW_NEW_GS_PROG_DATA | 214 BRW_NEW_PUSH_CONSTANT_ALLOCATION, 215 }, 216 .emit = upload_gs_state, 217 }; 218