1 /**************************************************************************
2  *
3  * Copyright 2015 Advanced Micro Devices, Inc.
4  * Copyright 2008 VMware, Inc.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * on the rights to use, copy, modify, merge, publish, distribute, sub
11  * license, and/or sell copies of the Software, and to permit persons to whom
12  * the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #ifndef DD_H_
29 #define DD_H_
30 
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
33 #include "pipe/p_screen.h"
34 #include "dd_util.h"
35 #include "os/os_thread.h"
36 #include "util/list.h"
37 #include "util/u_log.h"
38 #include "util/u_queue.h"
39 
40 struct dd_context;
41 
42 enum dd_dump_mode {
43    DD_DUMP_ONLY_HANGS,
44    DD_DUMP_ALL_CALLS,
45    DD_DUMP_APITRACE_CALL,
46 };
47 
48 struct dd_screen
49 {
50    struct pipe_screen base;
51    struct pipe_screen *screen;
52    unsigned timeout_ms;
53    enum dd_dump_mode dump_mode;
54    bool flush_always;
55    bool transfers;
56    bool verbose;
57    unsigned skip_count;
58    unsigned apitrace_dump_call;
59 };
60 
61 enum call_type
62 {
63    CALL_FLUSH,
64    CALL_DRAW_VBO,
65    CALL_LAUNCH_GRID,
66    CALL_RESOURCE_COPY_REGION,
67    CALL_BLIT,
68    CALL_FLUSH_RESOURCE,
69    CALL_CLEAR,
70    CALL_CLEAR_BUFFER,
71    CALL_CLEAR_TEXTURE,
72    CALL_CLEAR_RENDER_TARGET,
73    CALL_CLEAR_DEPTH_STENCIL,
74    CALL_GENERATE_MIPMAP,
75    CALL_GET_QUERY_RESULT_RESOURCE,
76    CALL_TRANSFER_MAP,
77    CALL_TRANSFER_FLUSH_REGION,
78    CALL_TRANSFER_UNMAP,
79    CALL_BUFFER_SUBDATA,
80    CALL_TEXTURE_SUBDATA,
81 };
82 
83 struct call_resource_copy_region
84 {
85    struct pipe_resource *dst;
86    unsigned dst_level;
87    unsigned dstx, dsty, dstz;
88    struct pipe_resource *src;
89    unsigned src_level;
90    struct pipe_box src_box;
91 };
92 
93 struct call_clear
94 {
95    unsigned buffers;
96    struct pipe_scissor_state scissor_state;
97    union pipe_color_union color;
98    double depth;
99    unsigned stencil;
100 };
101 
102 struct call_clear_buffer
103 {
104    struct pipe_resource *res;
105    unsigned offset;
106    unsigned size;
107    const void *clear_value;
108    int clear_value_size;
109 };
110 
111 struct call_generate_mipmap {
112    struct pipe_resource *res;
113    enum pipe_format format;
114    unsigned base_level;
115    unsigned last_level;
116    unsigned first_layer;
117    unsigned last_layer;
118 };
119 
120 struct call_flush {
121    unsigned flags;
122 };
123 
124 struct call_draw_info {
125    struct pipe_draw_info draw;
126    struct pipe_draw_indirect_info indirect;
127 };
128 
129 struct call_get_query_result_resource {
130    struct pipe_query *query;
131    enum pipe_query_type query_type;
132    bool wait;
133    enum pipe_query_value_type result_type;
134    int index;
135    struct pipe_resource *resource;
136    unsigned offset;
137 };
138 
139 struct call_transfer_map {
140    struct pipe_transfer *transfer_ptr;
141    struct pipe_transfer transfer;
142    void *ptr;
143 };
144 
145 struct call_transfer_flush_region {
146    struct pipe_transfer *transfer_ptr;
147    struct pipe_transfer transfer;
148    struct pipe_box box;
149 };
150 
151 struct call_transfer_unmap {
152    struct pipe_transfer *transfer_ptr;
153    struct pipe_transfer transfer;
154 };
155 
156 struct call_buffer_subdata {
157    struct pipe_resource *resource;
158    unsigned usage;
159    unsigned offset;
160    unsigned size;
161    const void *data;
162 };
163 
164 struct call_texture_subdata {
165    struct pipe_resource *resource;
166    unsigned level;
167    unsigned usage;
168    struct pipe_box box;
169    const void *data;
170    unsigned stride;
171    unsigned layer_stride;
172 };
173 
174 struct dd_call
175 {
176    enum call_type type;
177 
178    union {
179       struct call_flush flush;
180       struct call_draw_info draw_vbo;
181       struct pipe_grid_info launch_grid;
182       struct call_resource_copy_region resource_copy_region;
183       struct pipe_blit_info blit;
184       struct pipe_resource *flush_resource;
185       struct call_clear clear;
186       struct call_clear_buffer clear_buffer;
187       struct call_generate_mipmap generate_mipmap;
188       struct call_get_query_result_resource get_query_result_resource;
189       struct call_transfer_map transfer_map;
190       struct call_transfer_flush_region transfer_flush_region;
191       struct call_transfer_unmap transfer_unmap;
192       struct call_buffer_subdata buffer_subdata;
193       struct call_texture_subdata texture_subdata;
194    } info;
195 };
196 
197 struct dd_query
198 {
199    unsigned type;
200    struct pipe_query *query;
201 };
202 
203 struct dd_state
204 {
205    void *cso;
206 
207    union {
208       struct pipe_blend_state blend;
209       struct pipe_depth_stencil_alpha_state dsa;
210       struct pipe_rasterizer_state rs;
211       struct pipe_sampler_state sampler;
212       struct {
213          struct pipe_vertex_element velems[PIPE_MAX_ATTRIBS];
214          unsigned count;
215       } velems;
216       struct pipe_shader_state shader;
217    } state;
218 };
219 
220 struct dd_draw_state
221 {
222    struct {
223       struct dd_query *query;
224       bool condition;
225       unsigned mode;
226    } render_cond;
227 
228    struct pipe_vertex_buffer vertex_buffers[PIPE_MAX_ATTRIBS];
229 
230    unsigned num_so_targets;
231    struct pipe_stream_output_target *so_targets[PIPE_MAX_SO_BUFFERS];
232    unsigned so_offsets[PIPE_MAX_SO_BUFFERS];
233 
234    struct dd_state *shaders[PIPE_SHADER_TYPES];
235    struct pipe_constant_buffer constant_buffers[PIPE_SHADER_TYPES][PIPE_MAX_CONSTANT_BUFFERS];
236    struct pipe_sampler_view *sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
237    struct dd_state *sampler_states[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
238    struct pipe_image_view shader_images[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_IMAGES];
239    struct pipe_shader_buffer shader_buffers[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_BUFFERS];
240 
241    struct dd_state *velems;
242    struct dd_state *rs;
243    struct dd_state *dsa;
244    struct dd_state *blend;
245 
246    struct pipe_blend_color blend_color;
247    struct pipe_stencil_ref stencil_ref;
248    unsigned sample_mask;
249    unsigned min_samples;
250    struct pipe_clip_state clip_state;
251    struct pipe_framebuffer_state framebuffer_state;
252    struct pipe_poly_stipple polygon_stipple;
253    struct pipe_scissor_state scissors[PIPE_MAX_VIEWPORTS];
254    struct pipe_viewport_state viewports[PIPE_MAX_VIEWPORTS];
255    float tess_default_levels[6];
256 
257    unsigned apitrace_call_number;
258 };
259 
260 struct dd_draw_state_copy
261 {
262    struct dd_draw_state base;
263 
264    /* dd_draw_state_copy does not reference real CSOs. Instead, it points to
265     * these variables, which serve as storage.
266     */
267    struct dd_query render_cond;
268    struct dd_state shaders[PIPE_SHADER_TYPES];
269    struct dd_state sampler_states[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
270    struct dd_state velems;
271    struct dd_state rs;
272    struct dd_state dsa;
273    struct dd_state blend;
274 };
275 
276 struct dd_draw_record {
277    struct list_head list;
278    struct dd_context *dctx;
279 
280    int64_t time_before;
281    int64_t time_after;
282    unsigned draw_call;
283 
284    /* The fence pointers are guaranteed to be valid once driver_finished is signalled */
285    struct pipe_fence_handle *prev_bottom_of_pipe;
286    struct pipe_fence_handle *top_of_pipe;
287    struct pipe_fence_handle *bottom_of_pipe;
288 
289    struct dd_call call;
290    struct dd_draw_state_copy draw_state;
291 
292    struct util_queue_fence driver_finished;
293    struct u_log_page *log_page;
294 };
295 
296 struct dd_context
297 {
298    struct pipe_context base;
299    struct pipe_context *pipe;
300 
301    struct dd_draw_state draw_state;
302    unsigned num_draw_calls;
303 
304    struct u_log_context log;
305 
306    /* Pipelined hang detection.
307     *
308     * Before each draw call, a new dd_draw_record is created that contains
309     * a copy of all states. After each draw call, the driver's log is added
310     * to this record. Additionally, deferred fences are associated to each
311     * record both before and after the draw.
312     *
313     * The records are handed off to a separate thread which waits on the
314     * records' fences. Records with signalled fences are freed. When a timeout
315     * is detected, the thread dumps the records of in-flight draws.
316     */
317    thrd_t thread;
318    mtx_t mutex;
319    cnd_t cond;
320    struct list_head records; /* oldest record first */
321    unsigned num_records;
322    bool kill_thread;
323    bool api_stalled;
324 };
325 
326 
327 struct pipe_context *
328 dd_context_create(struct dd_screen *dscreen, struct pipe_context *pipe);
329 
330 void
331 dd_init_draw_functions(struct dd_context *dctx);
332 
333 void
334 dd_thread_join(struct dd_context *dctx);
335 int
336 dd_thread_main(void *input);
337 
338 FILE *
339 dd_get_file_stream(struct dd_screen *dscreen, unsigned apitrace_call_number);
340 
341 static inline struct dd_context *
dd_context(struct pipe_context * pipe)342 dd_context(struct pipe_context *pipe)
343 {
344    return (struct dd_context *)pipe;
345 }
346 
347 static inline struct dd_screen *
dd_screen(struct pipe_screen * screen)348 dd_screen(struct pipe_screen *screen)
349 {
350    return (struct dd_screen*)screen;
351 }
352 
353 static inline struct dd_query *
dd_query(struct pipe_query * query)354 dd_query(struct pipe_query *query)
355 {
356    return (struct dd_query *)query;
357 }
358 
359 static inline struct pipe_query *
dd_query_unwrap(struct pipe_query * query)360 dd_query_unwrap(struct pipe_query *query)
361 {
362    if (query) {
363       return dd_query(query)->query;
364    } else {
365       return NULL;
366    }
367 }
368 
369 
370 #define CTX_INIT(_member) \
371    dctx->base._member = dctx->pipe->_member ? dd_context_##_member : NULL
372 
373 #endif /* DD_H_ */
374