r600g: implement output modifiers and use them to further optimize LRP
[mesa.git] / src / gallium / drivers / r600 / r600_state_common.c
1 /*
2 * Copyright 2010 Red Hat Inc.
3 * 2010 Jerome Glisse
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie <airlied@redhat.com>
25 * Jerome Glisse <jglisse@redhat.com>
26 */
27 #include <util/u_memory.h>
28 #include <util/u_format.h>
29 #include <pipebuffer/pb_buffer.h>
30 #include "r600_pipe.h"
31
32 /* common state between evergreen and r600 */
33 void r600_bind_blend_state(struct pipe_context *ctx, void *state)
34 {
35 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
36 struct r600_pipe_blend *blend = (struct r600_pipe_blend *)state;
37 struct r600_pipe_state *rstate;
38
39 if (state == NULL)
40 return;
41 rstate = &blend->rstate;
42 rctx->states[rstate->id] = rstate;
43 rctx->cb_target_mask = blend->cb_target_mask;
44 r600_context_pipe_state_set(&rctx->ctx, rstate);
45 }
46
47 void r600_bind_rs_state(struct pipe_context *ctx, void *state)
48 {
49 struct r600_pipe_rasterizer *rs = (struct r600_pipe_rasterizer *)state;
50 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
51
52 if (state == NULL)
53 return;
54
55 rctx->flatshade = rs->flatshade;
56 rctx->sprite_coord_enable = rs->sprite_coord_enable;
57 rctx->rasterizer = rs;
58
59 rctx->states[rs->rstate.id] = &rs->rstate;
60 r600_context_pipe_state_set(&rctx->ctx, &rs->rstate);
61
62 if (rctx->family >= CHIP_CEDAR) {
63 evergreen_polygon_offset_update(rctx);
64 } else {
65 r600_polygon_offset_update(rctx);
66 }
67 }
68
69 void r600_delete_rs_state(struct pipe_context *ctx, void *state)
70 {
71 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
72 struct r600_pipe_rasterizer *rs = (struct r600_pipe_rasterizer *)state;
73
74 if (rctx->rasterizer == rs) {
75 rctx->rasterizer = NULL;
76 }
77 if (rctx->states[rs->rstate.id] == &rs->rstate) {
78 rctx->states[rs->rstate.id] = NULL;
79 }
80 free(rs);
81 }
82
83 void r600_sampler_view_destroy(struct pipe_context *ctx,
84 struct pipe_sampler_view *state)
85 {
86 struct r600_pipe_sampler_view *resource = (struct r600_pipe_sampler_view *)state;
87
88 pipe_resource_reference(&state->texture, NULL);
89 FREE(resource);
90 }
91
92 void r600_bind_state(struct pipe_context *ctx, void *state)
93 {
94 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
95 struct r600_pipe_state *rstate = (struct r600_pipe_state *)state;
96
97 if (state == NULL)
98 return;
99 rctx->states[rstate->id] = rstate;
100 r600_context_pipe_state_set(&rctx->ctx, rstate);
101 }
102
103 void r600_delete_state(struct pipe_context *ctx, void *state)
104 {
105 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
106 struct r600_pipe_state *rstate = (struct r600_pipe_state *)state;
107
108 if (rctx->states[rstate->id] == rstate) {
109 rctx->states[rstate->id] = NULL;
110 }
111 for (int i = 0; i < rstate->nregs; i++) {
112 r600_bo_reference(rctx->radeon, &rstate->regs[i].bo, NULL);
113 }
114 free(rstate);
115 }
116
117 void r600_bind_vertex_elements(struct pipe_context *ctx, void *state)
118 {
119 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
120 struct r600_vertex_element *v = (struct r600_vertex_element*)state;
121
122 /* delete previous translated vertex elements */
123 if (rctx->tran.new_velems) {
124 r600_end_vertex_translate(rctx);
125 }
126
127 rctx->vertex_elements = v;
128 if (v) {
129 rctx->states[v->rstate.id] = &v->rstate;
130 r600_context_pipe_state_set(&rctx->ctx, &v->rstate);
131 if (rctx->family >= CHIP_CEDAR) {
132 evergreen_vertex_buffer_update(rctx);
133 } else {
134 r600_vertex_buffer_update(rctx);
135 }
136 }
137
138 if (v) {
139 // rctx->vs_rebuild = TRUE;
140 }
141 }
142
143 void r600_delete_vertex_element(struct pipe_context *ctx, void *state)
144 {
145 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
146 struct r600_vertex_element *v = (struct r600_vertex_element*)state;
147
148 if (rctx->states[v->rstate.id] == &v->rstate) {
149 rctx->states[v->rstate.id] = NULL;
150 }
151 if (rctx->vertex_elements == state)
152 rctx->vertex_elements = NULL;
153
154 r600_bo_reference(rctx->radeon, &v->fetch_shader, NULL);
155 FREE(state);
156 }
157
158
159 void r600_set_index_buffer(struct pipe_context *ctx,
160 const struct pipe_index_buffer *ib)
161 {
162 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
163
164 if (ib) {
165 pipe_resource_reference(&rctx->index_buffer.buffer, ib->buffer);
166 memcpy(&rctx->index_buffer, ib, sizeof(rctx->index_buffer));
167 } else {
168 pipe_resource_reference(&rctx->index_buffer.buffer, NULL);
169 memset(&rctx->index_buffer, 0, sizeof(rctx->index_buffer));
170 }
171
172 /* TODO make this more like a state */
173 }
174
175 void r600_set_vertex_buffers(struct pipe_context *ctx, unsigned count,
176 const struct pipe_vertex_buffer *buffers)
177 {
178 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
179 struct pipe_vertex_buffer *vbo;
180 unsigned max_index = (unsigned)-1;
181
182 if (rctx->family >= CHIP_CEDAR) {
183 for (int i = 0; i < rctx->nvertex_buffer; i++) {
184 pipe_resource_reference(&rctx->vertex_buffer[i].buffer, NULL);
185 evergreen_context_pipe_state_set_fs_resource(&rctx->ctx, NULL, i);
186 }
187 } else {
188 for (int i = 0; i < rctx->nvertex_buffer; i++) {
189 pipe_resource_reference(&rctx->vertex_buffer[i].buffer, NULL);
190 r600_context_pipe_state_set_fs_resource(&rctx->ctx, NULL, i);
191 }
192 }
193 memcpy(rctx->vertex_buffer, buffers, sizeof(struct pipe_vertex_buffer) * count);
194
195 for (int i = 0; i < count; i++) {
196 vbo = (struct pipe_vertex_buffer*)&buffers[i];
197
198 rctx->vertex_buffer[i].buffer = NULL;
199 if (buffers[i].buffer == NULL)
200 continue;
201 if (r600_buffer_is_user_buffer(buffers[i].buffer))
202 rctx->any_user_vbs = TRUE;
203 pipe_resource_reference(&rctx->vertex_buffer[i].buffer, buffers[i].buffer);
204
205 /* The stride of zero means we will be fetching only the first
206 * vertex, so don't care about max_index. */
207 if (!vbo->stride)
208 continue;
209
210 if (vbo->max_index == ~0) {
211 vbo->max_index = (vbo->buffer->width0 - vbo->buffer_offset) / vbo->stride;
212 }
213 max_index = MIN2(vbo->max_index, max_index);
214 }
215 rctx->nvertex_buffer = count;
216 rctx->vb_max_index = max_index;
217 if (rctx->family >= CHIP_CEDAR) {
218 evergreen_vertex_buffer_update(rctx);
219 } else {
220 r600_vertex_buffer_update(rctx);
221 }
222 }
223
224
225 #define FORMAT_REPLACE(what, withwhat) \
226 case PIPE_FORMAT_##what: *format = PIPE_FORMAT_##withwhat; break
227
228 void *r600_create_vertex_elements(struct pipe_context *ctx,
229 unsigned count,
230 const struct pipe_vertex_element *elements)
231 {
232 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
233 struct r600_vertex_element *v = CALLOC_STRUCT(r600_vertex_element);
234 enum pipe_format *format;
235 int i;
236
237 assert(count < 32);
238 if (!v)
239 return NULL;
240
241 v->count = count;
242 memcpy(v->elements, elements, count * sizeof(struct pipe_vertex_element));
243
244 for (i = 0; i < count; i++) {
245 v->hw_format[i] = v->elements[i].src_format;
246 format = &v->hw_format[i];
247
248 switch (*format) {
249 FORMAT_REPLACE(R64_FLOAT, R32_FLOAT);
250 FORMAT_REPLACE(R64G64_FLOAT, R32G32_FLOAT);
251 FORMAT_REPLACE(R64G64B64_FLOAT, R32G32B32_FLOAT);
252 FORMAT_REPLACE(R64G64B64A64_FLOAT, R32G32B32A32_FLOAT);
253 default:;
254 }
255 v->incompatible_layout =
256 v->incompatible_layout ||
257 v->elements[i].src_format != v->hw_format[i];
258
259 v->hw_format_size[i] = align(util_format_get_blocksize(v->hw_format[i]), 4);
260 }
261
262 if (r600_vertex_elements_build_fetch_shader(rctx, v)) {
263 FREE(v);
264 return NULL;
265 }
266
267 return v;
268 }
269
270 void *r600_create_shader_state(struct pipe_context *ctx,
271 const struct pipe_shader_state *state)
272 {
273 struct r600_pipe_shader *shader = CALLOC_STRUCT(r600_pipe_shader);
274 int r;
275
276 r = r600_pipe_shader_create(ctx, shader, state->tokens);
277 if (r) {
278 return NULL;
279 }
280 return shader;
281 }
282
283 void r600_bind_ps_shader(struct pipe_context *ctx, void *state)
284 {
285 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
286
287 /* TODO delete old shader */
288 rctx->ps_shader = (struct r600_pipe_shader *)state;
289 if (state) {
290 r600_context_pipe_state_set(&rctx->ctx, &rctx->ps_shader->rstate);
291 }
292 }
293
294 void r600_bind_vs_shader(struct pipe_context *ctx, void *state)
295 {
296 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
297
298 /* TODO delete old shader */
299 rctx->vs_shader = (struct r600_pipe_shader *)state;
300 if (state) {
301 r600_context_pipe_state_set(&rctx->ctx, &rctx->vs_shader->rstate);
302 }
303 }
304
305 void r600_delete_ps_shader(struct pipe_context *ctx, void *state)
306 {
307 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
308 struct r600_pipe_shader *shader = (struct r600_pipe_shader *)state;
309
310 if (rctx->ps_shader == shader) {
311 rctx->ps_shader = NULL;
312 }
313
314 r600_pipe_shader_destroy(ctx, shader);
315 free(shader);
316 }
317
318 void r600_delete_vs_shader(struct pipe_context *ctx, void *state)
319 {
320 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
321 struct r600_pipe_shader *shader = (struct r600_pipe_shader *)state;
322
323 if (rctx->vs_shader == shader) {
324 rctx->vs_shader = NULL;
325 }
326
327 r600_pipe_shader_destroy(ctx, shader);
328 free(shader);
329 }