2 * Copyright © 2014-2017 Broadcom
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:
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
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
27 #include "pipe/p_defines.h"
28 #include "util/hash_table.h"
29 #include "util/ralloc.h"
30 #include "util/u_inlines.h"
31 #include "util/u_memory.h"
32 #include "util/u_blitter.h"
33 #include "util/u_upload_mgr.h"
34 #include "util/u_prim.h"
35 #include "indices/u_primconvert.h"
36 #include "pipe/p_screen.h"
38 #include "v3d_screen.h"
39 #include "v3d_context.h"
40 #include "v3d_resource.h"
41 #include "broadcom/compiler/v3d_compiler.h"
44 v3d_flush(struct pipe_context
*pctx
)
46 struct v3d_context
*v3d
= v3d_context(pctx
);
48 hash_table_foreach(v3d
->jobs
, entry
) {
49 struct v3d_job
*job
= entry
->data
;
50 v3d_job_submit(v3d
, job
);
55 v3d_pipe_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fence
,
58 struct v3d_context
*v3d
= v3d_context(pctx
);
63 struct pipe_screen
*screen
= pctx
->screen
;
64 struct v3d_fence
*f
= v3d_fence_create(v3d
);
65 screen
->fence_reference(screen
, fence
, NULL
);
66 *fence
= (struct pipe_fence_handle
*)f
;
71 v3d_memory_barrier(struct pipe_context
*pctx
, unsigned int flags
)
73 struct v3d_context
*v3d
= v3d_context(pctx
);
75 /* We only need to flush for SSBOs and images, because for everything
76 * else we flush the job automatically when we needed.
78 const unsigned int flush_flags
= PIPE_BARRIER_SHADER_BUFFER
|
81 if (!(flags
& flush_flags
))
84 /* We only need to flush jobs writing to SSBOs/images. */
85 perf_debug("Flushing all jobs for glMemoryBarrier(), could do better");
90 v3d_set_debug_callback(struct pipe_context
*pctx
,
91 const struct pipe_debug_callback
*cb
)
93 struct v3d_context
*v3d
= v3d_context(pctx
);
98 memset(&v3d
->debug
, 0, sizeof(v3d
->debug
));
102 v3d_invalidate_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
104 struct v3d_context
*v3d
= v3d_context(pctx
);
105 struct v3d_resource
*rsc
= v3d_resource(prsc
);
107 rsc
->initialized_buffers
= 0;
109 struct hash_entry
*entry
= _mesa_hash_table_search(v3d
->write_jobs
,
114 struct v3d_job
*job
= entry
->data
;
115 if (job
->key
.zsbuf
&& job
->key
.zsbuf
->texture
== prsc
)
116 job
->store
&= ~(PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
);
120 * Flushes the current job to get up-to-date primive counts written to the
121 * primitive counts BO, then accumulates the transform feedback primitive count
122 * in the context and the corresponding vertex counts in the bound stream
126 v3d_update_primitive_counters(struct v3d_context
*v3d
)
128 struct v3d_job
*job
= v3d_get_job_for_fbo(v3d
);
129 if (job
->draw_calls_queued
== 0)
132 /* In order to get up-to-date primitive counts we need to submit
133 * the job for execution so we get the counts written to memory.
134 * Notice that this will require a sync wait for the buffer write.
136 uint32_t prims_before
= v3d
->tf_prims_generated
;
137 v3d_job_submit(v3d
, job
);
138 uint32_t prims_after
= v3d
->tf_prims_generated
;
139 if (prims_before
== prims_after
)
142 enum pipe_prim_type prim_type
= u_base_prim_type(v3d
->prim_mode
);
143 uint32_t num_verts
= u_vertices_for_prims(prim_type
,
144 prims_after
- prims_before
);
145 for (int i
= 0; i
< v3d
->streamout
.num_targets
; i
++) {
146 struct v3d_stream_output_target
*so
=
147 v3d_stream_output_target(v3d
->streamout
.targets
[i
]);
148 so
->recorded_vertex_count
+= num_verts
;
153 v3d_context_destroy(struct pipe_context
*pctx
)
155 struct v3d_context
*v3d
= v3d_context(pctx
);
160 util_blitter_destroy(v3d
->blitter
);
162 if (v3d
->primconvert
)
163 util_primconvert_destroy(v3d
->primconvert
);
166 u_upload_destroy(v3d
->uploader
);
167 if (v3d
->state_uploader
)
168 u_upload_destroy(v3d
->state_uploader
);
170 if (v3d
->prim_counts
)
171 pipe_resource_reference(&v3d
->prim_counts
, NULL
);
173 slab_destroy_child(&v3d
->transfer_pool
);
175 pipe_surface_reference(&v3d
->framebuffer
.cbufs
[0], NULL
);
176 pipe_surface_reference(&v3d
->framebuffer
.zsbuf
, NULL
);
178 v3d_program_fini(pctx
);
184 v3d_get_sample_position(struct pipe_context
*pctx
,
185 unsigned sample_count
, unsigned sample_index
,
188 struct v3d_context
*v3d
= v3d_context(pctx
);
190 if (sample_count
<= 1) {
194 static const int xoffsets_v33
[] = { 1, -3, 3, -1 };
195 static const int xoffsets_v42
[] = { -1, 3, -3, 1 };
196 const int *xoffsets
= (v3d
->screen
->devinfo
.ver
>= 42 ?
197 xoffsets_v42
: xoffsets_v33
);
199 xy
[0] = 0.5 + xoffsets
[sample_index
] * .125;
200 xy
[1] = .125 + sample_index
* .25;
204 struct pipe_context
*
205 v3d_context_create(struct pipe_screen
*pscreen
, void *priv
, unsigned flags
)
207 struct v3d_screen
*screen
= v3d_screen(pscreen
);
208 struct v3d_context
*v3d
;
210 /* Prevent dumping of the shaders built during context setup. */
211 uint32_t saved_shaderdb_flag
= V3D_DEBUG
& V3D_DEBUG_SHADERDB
;
212 V3D_DEBUG
&= ~V3D_DEBUG_SHADERDB
;
214 v3d
= rzalloc(NULL
, struct v3d_context
);
217 struct pipe_context
*pctx
= &v3d
->base
;
219 v3d
->screen
= screen
;
221 int ret
= drmSyncobjCreate(screen
->fd
, DRM_SYNCOBJ_CREATE_SIGNALED
,
228 pctx
->screen
= pscreen
;
230 pctx
->destroy
= v3d_context_destroy
;
231 pctx
->flush
= v3d_pipe_flush
;
232 pctx
->memory_barrier
= v3d_memory_barrier
;
233 pctx
->set_debug_callback
= v3d_set_debug_callback
;
234 pctx
->invalidate_resource
= v3d_invalidate_resource
;
235 pctx
->get_sample_position
= v3d_get_sample_position
;
237 if (screen
->devinfo
.ver
>= 41) {
238 v3d41_draw_init(pctx
);
239 v3d41_state_init(pctx
);
241 v3d33_draw_init(pctx
);
242 v3d33_state_init(pctx
);
244 v3d_program_init(pctx
);
245 v3d_query_init(pctx
);
246 v3d_resource_context_init(pctx
);
250 v3d
->fd
= screen
->fd
;
252 slab_create_child(&v3d
->transfer_pool
, &screen
->transfer_pool
);
254 v3d
->uploader
= u_upload_create_default(&v3d
->base
);
255 v3d
->base
.stream_uploader
= v3d
->uploader
;
256 v3d
->base
.const_uploader
= v3d
->uploader
;
257 v3d
->state_uploader
= u_upload_create(&v3d
->base
,
259 PIPE_BIND_CONSTANT_BUFFER
,
260 PIPE_USAGE_STREAM
, 0);
262 v3d
->blitter
= util_blitter_create(pctx
);
265 v3d
->blitter
->use_index_buffer
= true;
267 v3d
->primconvert
= util_primconvert_create(pctx
,
268 (1 << PIPE_PRIM_QUADS
) - 1);
269 if (!v3d
->primconvert
)
272 V3D_DEBUG
|= saved_shaderdb_flag
;
274 v3d
->sample_mask
= (1 << V3D_MAX_SAMPLES
) - 1;
275 v3d
->active_queries
= true;