iris: import program cache code
[mesa.git] / src / gallium / drivers / iris / iris_context.c
1 /*
2 * Copyright © 2017 Intel Corporation
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <stdio.h>
24 #include <time.h>
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "util/ralloc.h"
28 #include "util/u_inlines.h"
29 #include "util/u_format.h"
30 #include "util/u_upload_mgr.h"
31 #include "iris_context.h"
32 #include "iris_resource.h"
33 #include "iris_screen.h"
34
35 static void
36 iris_flush(struct pipe_context *ctx,
37 struct pipe_fence_handle **fence,
38 unsigned flags)
39 {
40 if (fence)
41 *fence = NULL;
42 }
43
44 /**
45 * For debugging purposes, this returns a time in seconds.
46 */
47 double
48 get_time(void)
49 {
50 struct timespec tp;
51
52 clock_gettime(CLOCK_MONOTONIC, &tp);
53
54 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
55 }
56
57 static void
58 iris_set_debug_callback(struct pipe_context *ctx,
59 const struct pipe_debug_callback *cb)
60 {
61 struct iris_context *ice = (struct iris_context *)ctx;
62
63 if (cb)
64 ice->dbg = *cb;
65 else
66 memset(&ice->dbg, 0, sizeof(ice->dbg));
67 }
68
69 static void
70 iris_destroy_context(struct pipe_context *ctx)
71 {
72 struct iris_context *ice = (struct iris_context *)ctx;
73
74 if (ctx->stream_uploader)
75 u_upload_destroy(ctx->stream_uploader);
76
77 ralloc_free(ice);
78 }
79
80 struct pipe_context *
81 iris_create_context(struct pipe_screen *pscreen, void *priv, unsigned flags)
82 {
83 struct iris_screen *screen = (struct iris_screen*)pscreen;
84 struct iris_context *ice = rzalloc(NULL, struct iris_context);
85
86 if (!ice)
87 return NULL;
88
89 struct pipe_context *ctx = &ice->ctx;
90
91 ctx->screen = pscreen;
92 ctx->priv = priv;
93
94 ctx->stream_uploader = u_upload_create_default(ctx);
95 if (!ctx->stream_uploader) {
96 free(ctx);
97 return NULL;
98 }
99 ctx->const_uploader = ctx->stream_uploader;
100
101 ctx->destroy = iris_destroy_context;
102 ctx->flush = iris_flush;
103 ctx->set_debug_callback = iris_set_debug_callback;
104
105 iris_init_blit_functions(ctx);
106 iris_init_clear_functions(ctx);
107 iris_init_program_functions(ctx);
108 iris_init_resource_functions(ctx);
109 iris_init_state_functions(ctx);
110 iris_init_query_functions(ctx);
111
112 iris_init_program_cache(ice);
113
114 iris_batch_init(&ice->render_batch, screen, &ice->dbg);
115
116 return ctx;
117 }