vc4: Switch simulator to using kernel validator
[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
111 if (!(vc4_debug & VC4_DEBUG_NORAST)) {
112 int ret;
113
114 #ifndef USE_VC4_SIMULATOR
115 ret = drmIoctl(vc4->fd, DRM_IOCTL_VC4_SUBMIT_CL, &submit);
116 #else
117 ret = vc4_simulator_flush(vc4, &submit, csurf);
118 #endif
119 if (ret)
120 errx(1, "VC4 submit failed\n");
121 }
122
123 vc4_reset_cl(&vc4->bcl);
124 vc4_reset_cl(&vc4->rcl);
125 vc4_reset_cl(&vc4->shader_rec);
126 vc4_reset_cl(&vc4->bo_handles);
127 #ifdef USE_VC4_SIMULATOR
128 vc4_reset_cl(&vc4->bo_pointers);
129 #endif
130 vc4->shader_rec_count = 0;
131
132 vc4->needs_flush = false;
133 vc4->dirty = ~0;
134
135 dump_fbo(vc4, ctex->bo);
136 }
137
138 static void
139 vc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
140 unsigned flags)
141 {
142 vc4_flush(pctx);
143 }
144
145 static void
146 vc4_context_destroy(struct pipe_context *pctx)
147 {
148 struct vc4_context *vc4 = vc4_context(pctx);
149
150 if (vc4->blitter)
151 util_blitter_destroy(vc4->blitter);
152
153 if (vc4->primconvert)
154 util_primconvert_destroy(vc4->primconvert);
155
156 util_slab_destroy(&vc4->transfer_pool);
157
158 free(vc4);
159 }
160
161 struct pipe_context *
162 vc4_context_create(struct pipe_screen *pscreen, void *priv)
163 {
164 struct vc4_screen *screen = vc4_screen(pscreen);
165 struct vc4_context *vc4;
166
167 /* Prevent dumping of the shaders built during context setup. */
168 uint32_t saved_shaderdb_flag = vc4_debug & VC4_DEBUG_SHADERDB;
169 vc4_debug &= ~VC4_DEBUG_SHADERDB;
170
171 vc4 = CALLOC_STRUCT(vc4_context);
172 if (vc4 == NULL)
173 return NULL;
174 struct pipe_context *pctx = &vc4->base;
175
176 vc4->screen = screen;
177
178 pctx->screen = pscreen;
179 pctx->priv = priv;
180 pctx->destroy = vc4_context_destroy;
181 pctx->flush = vc4_pipe_flush;
182
183 vc4_draw_init(pctx);
184 vc4_state_init(pctx);
185 vc4_program_init(pctx);
186 vc4_resource_context_init(pctx);
187
188 vc4_init_cl(vc4, &vc4->bcl);
189 vc4_init_cl(vc4, &vc4->rcl);
190 vc4_init_cl(vc4, &vc4->shader_rec);
191 vc4_init_cl(vc4, &vc4->bo_handles);
192
193 vc4->dirty = ~0;
194 vc4->fd = screen->fd;
195
196 util_slab_create(&vc4->transfer_pool, sizeof(struct pipe_transfer),
197 16, UTIL_SLAB_SINGLETHREADED);
198 vc4->blitter = util_blitter_create(pctx);
199 if (!vc4->blitter)
200 goto fail;
201
202 vc4->primconvert = util_primconvert_create(pctx,
203 !((1 << PIPE_PRIM_QUADS) - 1));
204 if (!vc4->primconvert)
205 goto fail;
206
207 vc4_debug |= saved_shaderdb_flag;
208
209 return &vc4->base;
210
211 fail:
212 pctx->destroy(pctx);
213 return NULL;
214 }