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