r600g: use blitter to do db->cb flushing.
[mesa.git] / src / gallium / drivers / r600 / r600_context.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse
25 * Corbin Simpson
26 */
27 #include <stdio.h>
28 #include <util/u_inlines.h>
29 #include <util/u_format.h>
30 #include <util/u_memory.h>
31 #include <util/u_upload_mgr.h>
32 #include <util/u_blitter.h>
33 #include "r600_screen.h"
34 #include "r600_context.h"
35 #include "r600_resource.h"
36
37 static void r600_destroy_context(struct pipe_context *context)
38 {
39 struct r600_context *rctx = r600_context(context);
40
41 rctx->rasterizer = r600_context_state_decref(rctx->rasterizer);
42 rctx->poly_stipple = r600_context_state_decref(rctx->poly_stipple);
43 rctx->scissor = r600_context_state_decref(rctx->scissor);
44 rctx->clip = r600_context_state_decref(rctx->clip);
45 rctx->ps_shader = r600_context_state_decref(rctx->ps_shader);
46 rctx->vs_shader = r600_context_state_decref(rctx->vs_shader);
47 rctx->depth = r600_context_state_decref(rctx->depth);
48 rctx->stencil = r600_context_state_decref(rctx->stencil);
49 rctx->alpha = r600_context_state_decref(rctx->alpha);
50 rctx->dsa = r600_context_state_decref(rctx->dsa);
51 rctx->blend = r600_context_state_decref(rctx->blend);
52 rctx->stencil_ref = r600_context_state_decref(rctx->stencil_ref);
53 rctx->viewport = r600_context_state_decref(rctx->viewport);
54 rctx->framebuffer = r600_context_state_decref(rctx->framebuffer);
55
56 free(rctx->ps_constant);
57 free(rctx->vs_constant);
58 free(rctx->vs_resource);
59
60 u_upload_destroy(rctx->upload_vb);
61 u_upload_destroy(rctx->upload_ib);
62
63 radeon_ctx_fini(rctx->ctx);
64 FREE(rctx);
65 }
66
67 void r600_flush(struct pipe_context *ctx, unsigned flags,
68 struct pipe_fence_handle **fence)
69 {
70 struct r600_context *rctx = r600_context(ctx);
71 struct r600_query *rquery = NULL;
72 #if 1
73 static int dc = 0;
74 char dname[256];
75 #endif
76
77 /* flush upload buffers */
78 u_upload_flush(rctx->upload_vb);
79 u_upload_flush(rctx->upload_ib);
80
81 /* suspend queries */
82 r600_queries_suspend(ctx);
83
84
85 #if 0
86 sprintf(dname, "gallium-%08d.bof", dc);
87 if (dc < 20) {
88 radeon_ctx_dump_bof(rctx->ctx, dname);
89 R600_ERR("dumped %s\n", dname);
90 }
91 dc++;
92 #endif
93
94 radeon_ctx_submit(rctx->ctx);
95
96 LIST_FOR_EACH_ENTRY(rquery, &rctx->query_list, list) {
97 rquery->flushed = TRUE;
98 }
99
100 radeon_ctx_clear(rctx->ctx);
101 /* resume queries */
102 r600_queries_resume(ctx);
103 }
104
105 struct pipe_context *r600_create_context(struct pipe_screen *screen, void *priv)
106 {
107 struct r600_context *rctx = CALLOC_STRUCT(r600_context);
108 struct r600_screen* rscreen = r600_screen(screen);
109
110 if (rctx == NULL)
111 return NULL;
112 rctx->context.winsys = rscreen->screen.winsys;
113 rctx->context.screen = screen;
114 rctx->context.priv = priv;
115 rctx->context.destroy = r600_destroy_context;
116 rctx->context.draw_vbo = r600_draw_vbo;
117 rctx->context.flush = r600_flush;
118
119 /* Easy accessing of screen/winsys. */
120 rctx->screen = rscreen;
121 rctx->rw = rscreen->rw;
122
123 if (radeon_get_family_class(rscreen->rw) == EVERGREEN)
124 rctx->vtbl = &eg_hw_state_vtbl;
125 else
126 rctx->vtbl = &r600_hw_state_vtbl;
127
128 r600_init_query_functions(rctx);
129 r600_init_state_functions(rctx);
130 r600_init_context_resource_functions(rctx);
131
132 r600_init_blit_functions(rctx);
133
134 rctx->blitter = util_blitter_create(&rctx->context);
135 if (rctx->blitter == NULL) {
136 FREE(rctx);
137 return NULL;
138 }
139
140 rctx->vtbl->init_config(rctx);
141
142 rctx->upload_ib = u_upload_create(&rctx->context, 32 * 1024, 16,
143 PIPE_BIND_INDEX_BUFFER);
144 if (rctx->upload_ib == NULL) {
145 goto out_free;
146 }
147
148 rctx->upload_vb = u_upload_create(&rctx->context, 128 * 1024, 16,
149 PIPE_BIND_VERTEX_BUFFER);
150 if (rctx->upload_vb == NULL) {
151 goto out_free;
152 }
153
154 rctx->vs_constant = (struct radeon_state *)calloc(R600_MAX_CONSTANT, sizeof(struct radeon_state));
155 if (!rctx->vs_constant) {
156 goto out_free;
157 }
158
159 rctx->ps_constant = (struct radeon_state *)calloc(R600_MAX_CONSTANT, sizeof(struct radeon_state));
160 if (!rctx->ps_constant) {
161 goto out_free;
162 }
163
164 rctx->vs_resource = (struct radeon_state *)calloc(R600_MAX_RESOURCE, sizeof(struct radeon_state));
165 if (!rctx->vs_resource) {
166 goto out_free;
167 }
168
169 rctx->ctx = radeon_ctx_init(rscreen->rw);
170 radeon_draw_init(&rctx->draw, rscreen->rw);
171 return &rctx->context;
172 out_free:
173 FREE(rctx);
174 return NULL;
175 }