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
27 #include "pipe/p_defines.h"
28 #include "util/ralloc.h"
29 #include "util/u_inlines.h"
30 #include "util/u_memory.h"
31 #include "util/u_blitter.h"
32 #include "util/u_upload_mgr.h"
33 #include "indices/u_primconvert.h"
34 #include "pipe/p_screen.h"
35
36 #include "vc4_screen.h"
37 #include "vc4_context.h"
38 #include "vc4_resource.h"
39
40 void
vc4_flush(struct pipe_context * pctx)41 vc4_flush(struct pipe_context *pctx)
42 {
43 struct vc4_context *vc4 = vc4_context(pctx);
44
45 struct hash_entry *entry;
46 hash_table_foreach(vc4->jobs, entry) {
47 struct vc4_job *job = entry->data;
48 vc4_job_submit(vc4, job);
49 }
50 }
51
52 static void
vc4_pipe_flush(struct pipe_context * pctx,struct pipe_fence_handle ** fence,unsigned flags)53 vc4_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
54 unsigned flags)
55 {
56 struct vc4_context *vc4 = vc4_context(pctx);
57
58 vc4_flush(pctx);
59
60 if (fence) {
61 struct pipe_screen *screen = pctx->screen;
62 struct vc4_fence *f = vc4_fence_create(vc4->screen,
63 vc4->last_emit_seqno);
64 screen->fence_reference(screen, fence, NULL);
65 *fence = (struct pipe_fence_handle *)f;
66 }
67 }
68
69 /* We can't flush the texture cache within rendering a tile, so we have to
70 * flush all rendering to the kernel so that the next job reading from the
71 * tile gets a flushed cache.
72 */
73 static void
vc4_texture_barrier(struct pipe_context * pctx,unsigned flags)74 vc4_texture_barrier(struct pipe_context *pctx, unsigned flags)
75 {
76 vc4_flush(pctx);
77 }
78
79 static void
vc4_invalidate_resource(struct pipe_context * pctx,struct pipe_resource * prsc)80 vc4_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
81 {
82 struct vc4_context *vc4 = vc4_context(pctx);
83 struct vc4_resource *rsc = vc4_resource(prsc);
84
85 rsc->initialized_buffers = 0;
86
87 struct hash_entry *entry = _mesa_hash_table_search(vc4->write_jobs,
88 prsc);
89 if (!entry)
90 return;
91
92 struct vc4_job *job = entry->data;
93 if (job->key.zsbuf && job->key.zsbuf->texture == prsc)
94 job->resolve &= ~(PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL);
95 }
96
97 static void
vc4_context_destroy(struct pipe_context * pctx)98 vc4_context_destroy(struct pipe_context *pctx)
99 {
100 struct vc4_context *vc4 = vc4_context(pctx);
101
102 vc4_flush(pctx);
103
104 if (vc4->blitter)
105 util_blitter_destroy(vc4->blitter);
106
107 if (vc4->primconvert)
108 util_primconvert_destroy(vc4->primconvert);
109
110 if (vc4->uploader)
111 u_upload_destroy(vc4->uploader);
112
113 slab_destroy_child(&vc4->transfer_pool);
114
115 pipe_surface_reference(&vc4->framebuffer.cbufs[0], NULL);
116 pipe_surface_reference(&vc4->framebuffer.zsbuf, NULL);
117
118 vc4_program_fini(pctx);
119
120 ralloc_free(vc4);
121 }
122
123 struct pipe_context *
vc4_context_create(struct pipe_screen * pscreen,void * priv,unsigned flags)124 vc4_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
125 {
126 struct vc4_screen *screen = vc4_screen(pscreen);
127 struct vc4_context *vc4;
128
129 /* Prevent dumping of the shaders built during context setup. */
130 uint32_t saved_shaderdb_flag = vc4_debug & VC4_DEBUG_SHADERDB;
131 vc4_debug &= ~VC4_DEBUG_SHADERDB;
132
133 vc4 = rzalloc(NULL, struct vc4_context);
134 if (!vc4)
135 return NULL;
136 struct pipe_context *pctx = &vc4->base;
137
138 vc4->screen = screen;
139
140 pctx->screen = pscreen;
141 pctx->priv = priv;
142 pctx->destroy = vc4_context_destroy;
143 pctx->flush = vc4_pipe_flush;
144 pctx->invalidate_resource = vc4_invalidate_resource;
145 pctx->texture_barrier = vc4_texture_barrier;
146
147 vc4_draw_init(pctx);
148 vc4_state_init(pctx);
149 vc4_program_init(pctx);
150 vc4_query_init(pctx);
151 vc4_resource_context_init(pctx);
152
153 vc4_job_init(vc4);
154
155 vc4->fd = screen->fd;
156
157 slab_create_child(&vc4->transfer_pool, &screen->transfer_pool);
158
159 vc4->uploader = u_upload_create_default(&vc4->base);
160 vc4->base.stream_uploader = vc4->uploader;
161 vc4->base.const_uploader = vc4->uploader;
162
163 vc4->blitter = util_blitter_create(pctx);
164 if (!vc4->blitter)
165 goto fail;
166
167 vc4->primconvert = util_primconvert_create(pctx,
168 (1 << PIPE_PRIM_QUADS) - 1);
169 if (!vc4->primconvert)
170 goto fail;
171
172 vc4_debug |= saved_shaderdb_flag;
173
174 vc4->sample_mask = (1 << VC4_MAX_SAMPLES) - 1;
175
176 return &vc4->base;
177
178 fail:
179 pctx->destroy(pctx);
180 return NULL;
181 }
182