i965/miptree: Set refcount before failing via _release()
[mesa.git] / src / mesa / drivers / dri / i965 / brw_conditional_render.c
1 /*
2 * Copyright © 2014 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 * Neil Roberts <neil@linux.intel.com>
25 */
26
27 /** @file brw_conditional_render.c
28 *
29 * Support for conditional rendering based on query objects
30 * (GL_NV_conditional_render, GL_ARB_conditional_render_inverted) on Gen7+.
31 */
32
33 #include "main/imports.h"
34 #include "main/condrender.h"
35
36 #include "brw_context.h"
37 #include "brw_defines.h"
38 #include "intel_batchbuffer.h"
39
40 static void
41 set_predicate_enable(struct brw_context *brw,
42 bool value)
43 {
44 if (value)
45 brw->predicate.state = BRW_PREDICATE_STATE_RENDER;
46 else
47 brw->predicate.state = BRW_PREDICATE_STATE_DONT_RENDER;
48 }
49
50 static void
51 set_predicate_for_overflow_query(struct brw_context *brw,
52 struct brw_query_object *query,
53 int stream_start, int count)
54 {
55 if (!can_do_mi_math_and_lrr(brw->screen)) {
56 brw->predicate.state = BRW_PREDICATE_STATE_STALL_FOR_QUERY;
57 return;
58 }
59
60 brw->predicate.state = BRW_PREDICATE_STATE_USE_BIT;
61
62 /* Needed to ensure the memory is coherent for the MI_LOAD_REGISTER_MEM
63 * command when loading the values into the predicate source registers for
64 * conditional rendering.
65 */
66 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_FLUSH_ENABLE);
67
68 hsw_overflow_result_to_gpr0(brw, query, count);
69 brw_load_register_reg64(brw, HSW_CS_GPR(0), MI_PREDICATE_SRC0);
70 brw_load_register_imm64(brw, MI_PREDICATE_SRC1, 0ull);
71 }
72
73 static void
74 set_predicate_for_occlusion_query(struct brw_context *brw,
75 struct brw_query_object *query)
76 {
77 if (!brw->predicate.supported) {
78 brw->predicate.state = BRW_PREDICATE_STATE_STALL_FOR_QUERY;
79 return;
80 }
81
82 brw->predicate.state = BRW_PREDICATE_STATE_USE_BIT;
83
84 /* Needed to ensure the memory is coherent for the MI_LOAD_REGISTER_MEM
85 * command when loading the values into the predicate source registers for
86 * conditional rendering.
87 */
88 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_FLUSH_ENABLE);
89
90 brw_load_register_mem64(brw,
91 MI_PREDICATE_SRC0,
92 query->bo,
93 I915_GEM_DOMAIN_INSTRUCTION,
94 0, /* write domain */
95 0 /* offset */);
96 brw_load_register_mem64(brw,
97 MI_PREDICATE_SRC1,
98 query->bo,
99 I915_GEM_DOMAIN_INSTRUCTION,
100 0, /* write domain */
101 8 /* offset */);
102 }
103
104 static void
105 set_predicate_for_result(struct brw_context *brw,
106 struct brw_query_object *query,
107 bool inverted)
108 {
109 int load_op;
110
111 assert(query->bo != NULL);
112
113 switch (query->Base.Target) {
114 case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB:
115 set_predicate_for_overflow_query(brw, query, 0, 1);
116 break;
117 case GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB:
118 set_predicate_for_overflow_query(brw, query, 0, MAX_VERTEX_STREAMS);
119 break;
120 default:
121 set_predicate_for_occlusion_query(brw, query);
122 }
123
124 if (brw->predicate.state == BRW_PREDICATE_STATE_USE_BIT) {
125 if (inverted)
126 load_op = MI_PREDICATE_LOADOP_LOAD;
127 else
128 load_op = MI_PREDICATE_LOADOP_LOADINV;
129
130 BEGIN_BATCH(1);
131 OUT_BATCH(GEN7_MI_PREDICATE |
132 load_op |
133 MI_PREDICATE_COMBINEOP_SET |
134 MI_PREDICATE_COMPAREOP_SRCS_EQUAL);
135 ADVANCE_BATCH();
136 }
137 }
138
139 static void
140 brw_begin_conditional_render(struct gl_context *ctx,
141 struct gl_query_object *q,
142 GLenum mode)
143 {
144 struct brw_context *brw = brw_context(ctx);
145 struct brw_query_object *query = (struct brw_query_object *) q;
146 bool inverted;
147
148 switch (mode) {
149 case GL_QUERY_WAIT:
150 case GL_QUERY_NO_WAIT:
151 case GL_QUERY_BY_REGION_WAIT:
152 case GL_QUERY_BY_REGION_NO_WAIT:
153 inverted = false;
154 break;
155 case GL_QUERY_WAIT_INVERTED:
156 case GL_QUERY_NO_WAIT_INVERTED:
157 case GL_QUERY_BY_REGION_WAIT_INVERTED:
158 case GL_QUERY_BY_REGION_NO_WAIT_INVERTED:
159 inverted = true;
160 break;
161 default:
162 unreachable("Unexpected conditional render mode");
163 }
164
165 /* If there are already samples from a BLT operation or if the query object
166 * is ready then we can avoid looking at the values in the buffer and just
167 * decide whether to draw using the CPU without stalling.
168 */
169 if (query->Base.Result || query->Base.Ready)
170 set_predicate_enable(brw, (query->Base.Result != 0) ^ inverted);
171 else
172 set_predicate_for_result(brw, query, inverted);
173 }
174
175 static void
176 brw_end_conditional_render(struct gl_context *ctx,
177 struct gl_query_object *q)
178 {
179 struct brw_context *brw = brw_context(ctx);
180
181 /* When there is no longer a conditional render in progress it should
182 * always render.
183 */
184 brw->predicate.state = BRW_PREDICATE_STATE_RENDER;
185 }
186
187 void
188 brw_init_conditional_render_functions(struct dd_function_table *functions)
189 {
190 functions->BeginConditionalRender = brw_begin_conditional_render;
191 functions->EndConditionalRender = brw_end_conditional_render;
192 }
193
194 bool
195 brw_check_conditional_render(struct brw_context *brw)
196 {
197 if (brw->predicate.state == BRW_PREDICATE_STATE_STALL_FOR_QUERY) {
198 perf_debug("Conditional rendering is implemented in software and may "
199 "stall.\n");
200 return _mesa_check_conditional_render(&brw->ctx);
201 }
202
203 return brw->predicate.state != BRW_PREDICATE_STATE_DONT_RENDER;
204 }