08e85ed6312e538b8f240501d12d94f44563c07a
[mesa.git] / src / gallium / drivers / vc4 / vc4_context.c
1 /*
2 * Copyright © 2014 Broadcom
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
24 #include <xf86drm.h>
25 #include <err.h>
26 #include <stdio.h>
27
28 #include "pipe/p_defines.h"
29 #include "util/u_inlines.h"
30 #include "util/u_memory.h"
31 #include "util/u_blitter.h"
32 #include "indices/u_primconvert.h"
33 #include "pipe/p_screen.h"
34
35 #include "vc4_screen.h"
36 #include "vc4_context.h"
37 #include "vc4_resource.h"
38
39 static void
40 dump_fbo(struct vc4_context *vc4, struct vc4_bo *fbo)
41 {
42 #ifndef USE_VC4_SIMULATOR
43 uint32_t *map = vc4_bo_map(fbo);
44 uint32_t width = vc4->framebuffer.width;
45 uint32_t height = vc4->framebuffer.height;
46 uint32_t chunk_w = width / 79;
47 uint32_t chunk_h = height / 40;
48 uint32_t found_colors[10];
49 uint32_t num_found_colors = 0;
50
51 for (int by = 0; by < height; by += chunk_h) {
52 for (int bx = 0; bx < width; bx += chunk_w) {
53 bool on = false, black = false;
54
55 for (int y = by; y < MIN2(height, by + chunk_h); y++) {
56 for (int x = bx; x < MIN2(width, bx + chunk_w); x++) {
57 uint32_t pix = map[y * width + x];
58 on |= pix != 0;
59 black |= pix == 0xff000000;
60
61 int i;
62 for (i = 0; i < num_found_colors; i++) {
63 if (pix == found_colors[i])
64 break;
65 }
66 if (i == num_found_colors &&
67 num_found_colors < Elements(found_colors))
68 found_colors[num_found_colors++] = pix;
69 }
70 }
71 if (black)
72 fprintf(stderr, "O");
73 else if (on)
74 fprintf(stderr, "X");
75 else
76 fprintf(stderr, ".");
77 }
78 fprintf(stderr, "\n");
79 }
80
81 for (int i = 0; i < num_found_colors; i++) {
82 fprintf(stderr, "color %d: 0x%08x\n", i, found_colors[i]);
83 }
84 #endif
85 }
86
87 void
88 vc4_flush(struct pipe_context *pctx)
89 {
90 struct vc4_context *vc4 = vc4_context(pctx);
91
92 if (!vc4->needs_flush)
93 return;
94
95 struct vc4_surface *csurf = vc4_surface(vc4->framebuffer.cbufs[0]);
96 struct vc4_resource *ctex = vc4_resource(csurf->base.texture);
97 struct drm_vc4_submit_cl submit;
98 memset(&submit, 0, sizeof(submit));
99
100 submit.bo_handles = vc4->bo_handles.base;
101 submit.bo_handle_count = (vc4->bo_handles.next -
102 vc4->bo_handles.base) / 4;
103 submit.bin_cl = vc4->bcl.base;
104 submit.bin_cl_len = vc4->bcl.next - vc4->bcl.base;
105 submit.render_cl = vc4->rcl.base;
106 submit.render_cl_len = vc4->rcl.next - vc4->rcl.base;
107 submit.shader_records = vc4->shader_rec.base;
108 submit.shader_record_len = vc4->shader_rec.next - vc4->shader_rec.base;
109 submit.shader_record_count = vc4->shader_rec_count;
110 submit.uniforms = vc4->uniforms.base;
111 submit.uniforms_len = vc4->uniforms.next - vc4->uniforms.base;
112
113 if (!(vc4_debug & VC4_DEBUG_NORAST)) {
114 int ret;
115
116 #ifndef USE_VC4_SIMULATOR
117 ret = drmIoctl(vc4->fd, DRM_IOCTL_VC4_SUBMIT_CL, &submit);
118 #else
119 ret = vc4_simulator_flush(vc4, &submit, csurf);
120 #endif
121 if (ret)
122 errx(1, "VC4 submit failed\n");
123 }
124
125 vc4_reset_cl(&vc4->bcl);
126 vc4_reset_cl(&vc4->rcl);
127 vc4_reset_cl(&vc4->shader_rec);
128 vc4_reset_cl(&vc4->uniforms);
129 vc4_reset_cl(&vc4->bo_handles);
130 #ifdef USE_VC4_SIMULATOR
131 vc4_reset_cl(&vc4->bo_pointers);
132 #endif
133 vc4->shader_rec_count = 0;
134
135 vc4->needs_flush = false;
136 vc4->dirty = ~0;
137
138 dump_fbo(vc4, ctex->bo);
139 }
140
141 static void
142 vc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
143 unsigned flags)
144 {
145 vc4_flush(pctx);
146 }
147
148 static void
149 vc4_context_destroy(struct pipe_context *pctx)
150 {
151 struct vc4_context *vc4 = vc4_context(pctx);
152
153 if (vc4->blitter)
154 util_blitter_destroy(vc4->blitter);
155
156 if (vc4->primconvert)
157 util_primconvert_destroy(vc4->primconvert);
158
159 util_slab_destroy(&vc4->transfer_pool);
160
161 free(vc4);
162 }
163
164 struct pipe_context *
165 vc4_context_create(struct pipe_screen *pscreen, void *priv)
166 {
167 struct vc4_screen *screen = vc4_screen(pscreen);
168 struct vc4_context *vc4;
169
170 /* Prevent dumping of the shaders built during context setup. */
171 uint32_t saved_shaderdb_flag = vc4_debug & VC4_DEBUG_SHADERDB;
172 vc4_debug &= ~VC4_DEBUG_SHADERDB;
173
174 vc4 = CALLOC_STRUCT(vc4_context);
175 if (vc4 == NULL)
176 return NULL;
177 struct pipe_context *pctx = &vc4->base;
178
179 vc4->screen = screen;
180
181 pctx->screen = pscreen;
182 pctx->priv = priv;
183 pctx->destroy = vc4_context_destroy;
184 pctx->flush = vc4_pipe_flush;
185
186 vc4_draw_init(pctx);
187 vc4_state_init(pctx);
188 vc4_program_init(pctx);
189 vc4_resource_context_init(pctx);
190
191 vc4_init_cl(vc4, &vc4->bcl);
192 vc4_init_cl(vc4, &vc4->rcl);
193 vc4_init_cl(vc4, &vc4->shader_rec);
194 vc4_init_cl(vc4, &vc4->bo_handles);
195
196 vc4->dirty = ~0;
197 vc4->fd = screen->fd;
198
199 util_slab_create(&vc4->transfer_pool, sizeof(struct pipe_transfer),
200 16, UTIL_SLAB_SINGLETHREADED);
201 vc4->blitter = util_blitter_create(pctx);
202 if (!vc4->blitter)
203 goto fail;
204
205 vc4->primconvert = util_primconvert_create(pctx,
206 !((1 << PIPE_PRIM_QUADS) - 1));
207 if (!vc4->primconvert)
208 goto fail;
209
210 vc4_debug |= saved_shaderdb_flag;
211
212 return &vc4->base;
213
214 fail:
215 pctx->destroy(pctx);
216 return NULL;
217 }