60bf8a537202094f62d9c32fbb4067c66a672c1c
[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 * 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 shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
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 "drm-uapi/i915_drm.h"
32 #include "iris_context.h"
33 #include "iris_resource.h"
34 #include "iris_screen.h"
35 #include "common/gen_sample_positions.h"
36
37 /**
38 * For debugging purposes, this returns a time in seconds.
39 */
40 double
41 get_time(void)
42 {
43 struct timespec tp;
44
45 clock_gettime(CLOCK_MONOTONIC, &tp);
46
47 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
48 }
49
50 /**
51 * The pipe->set_debug_callback() driver hook.
52 */
53 static void
54 iris_set_debug_callback(struct pipe_context *ctx,
55 const struct pipe_debug_callback *cb)
56 {
57 struct iris_context *ice = (struct iris_context *)ctx;
58
59 if (cb)
60 ice->dbg = *cb;
61 else
62 memset(&ice->dbg, 0, sizeof(ice->dbg));
63 }
64
65 static void
66 iris_get_sample_position(struct pipe_context *ctx,
67 unsigned sample_count,
68 unsigned sample_index,
69 float *out_value)
70 {
71 union {
72 struct {
73 float x[16];
74 float y[16];
75 } a;
76 struct {
77 float _0XOffset, _1XOffset, _2XOffset, _3XOffset,
78 _4XOffset, _5XOffset, _6XOffset, _7XOffset,
79 _8XOffset, _9XOffset, _10XOffset, _11XOffset,
80 _12XOffset, _13XOffset, _14XOffset, _15XOffset;
81 float _0YOffset, _1YOffset, _2YOffset, _3YOffset,
82 _4YOffset, _5YOffset, _6YOffset, _7YOffset,
83 _8YOffset, _9YOffset, _10YOffset, _11YOffset,
84 _12YOffset, _13YOffset, _14YOffset, _15YOffset;
85 } v;
86 } u;
87 switch (sample_count) {
88 case 1: GEN_SAMPLE_POS_1X(u.v._); break;
89 case 2: GEN_SAMPLE_POS_2X(u.v._); break;
90 case 4: GEN_SAMPLE_POS_4X(u.v._); break;
91 case 8: GEN_SAMPLE_POS_8X(u.v._); break;
92 case 16: GEN_SAMPLE_POS_16X(u.v._); break;
93 default: unreachable("invalid sample count");
94 }
95
96 out_value[0] = u.a.x[sample_index];
97 out_value[1] = u.a.y[sample_index];
98 }
99
100 /**
101 * Destroy a context, freeing any associated memory.
102 */
103 static void
104 iris_destroy_context(struct pipe_context *ctx)
105 {
106 struct iris_context *ice = (struct iris_context *)ctx;
107
108 if (ctx->stream_uploader)
109 u_upload_destroy(ctx->stream_uploader);
110
111 ice->vtbl.destroy_state(ice);
112 iris_destroy_program_cache(ice);
113 iris_destroy_border_color_pool(ice);
114 u_upload_destroy(ice->state.surface_uploader);
115 u_upload_destroy(ice->state.dynamic_uploader);
116 u_upload_destroy(ice->query_buffer_uploader);
117
118 slab_destroy_child(&ice->transfer_pool);
119
120 iris_batch_free(&ice->batches[IRIS_BATCH_RENDER]);
121 iris_batch_free(&ice->batches[IRIS_BATCH_COMPUTE]);
122 iris_destroy_binder(&ice->state.binder);
123
124 ralloc_free(ice);
125 }
126
127 #define genX_call(devinfo, func, ...) \
128 switch (devinfo->gen) { \
129 case 11: \
130 gen11_##func(__VA_ARGS__); \
131 break; \
132 case 10: \
133 gen10_##func(__VA_ARGS__); \
134 break; \
135 case 9: \
136 gen9_##func(__VA_ARGS__); \
137 break; \
138 case 8: \
139 gen8_##func(__VA_ARGS__); \
140 break; \
141 default: \
142 unreachable("Unknown hardware generation"); \
143 }
144
145 /**
146 * Create a context.
147 *
148 * This is where each context begins.
149 */
150 struct pipe_context *
151 iris_create_context(struct pipe_screen *pscreen, void *priv, unsigned flags)
152 {
153 struct iris_screen *screen = (struct iris_screen*)pscreen;
154 const struct gen_device_info *devinfo = &screen->devinfo;
155 struct iris_context *ice = rzalloc(NULL, struct iris_context);
156
157 if (!ice)
158 return NULL;
159
160 struct pipe_context *ctx = &ice->ctx;
161
162 ctx->screen = pscreen;
163 ctx->priv = priv;
164
165 ctx->stream_uploader = u_upload_create_default(ctx);
166 if (!ctx->stream_uploader) {
167 free(ctx);
168 return NULL;
169 }
170 ctx->const_uploader = ctx->stream_uploader;
171
172 ctx->destroy = iris_destroy_context;
173 ctx->set_debug_callback = iris_set_debug_callback;
174 ctx->get_sample_position = iris_get_sample_position;
175
176 ice->shaders.urb_size = devinfo->urb.size;
177
178 iris_init_context_fence_functions(ctx);
179 iris_init_blit_functions(ctx);
180 iris_init_clear_functions(ctx);
181 iris_init_program_functions(ctx);
182 iris_init_resource_functions(ctx);
183 iris_init_query_functions(ctx);
184 iris_init_flush_functions(ctx);
185
186 iris_init_program_cache(ice);
187 iris_init_border_color_pool(ice);
188 iris_init_binder(ice);
189
190 slab_create_child(&ice->transfer_pool, &screen->transfer_pool);
191
192 ice->state.surface_uploader =
193 u_upload_create(ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
194 IRIS_RESOURCE_FLAG_SURFACE_MEMZONE);
195 ice->state.dynamic_uploader =
196 u_upload_create(ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
197 IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE);
198
199 ice->query_buffer_uploader =
200 u_upload_create(ctx, 4096, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
201 0);
202
203 genX_call(devinfo, init_state, ice);
204 genX_call(devinfo, init_blorp, ice);
205
206 for (int i = 0; i < IRIS_BATCH_COUNT; i++) {
207 iris_init_batch(&ice->batches[i], screen, &ice->vtbl, &ice->dbg,
208 ice->batches, (enum iris_batch_name) i,
209 I915_EXEC_RENDER);
210 }
211
212 ice->vtbl.init_render_context(screen, &ice->batches[IRIS_BATCH_RENDER],
213 &ice->vtbl, &ice->dbg);
214 ice->vtbl.init_compute_context(screen, &ice->batches[IRIS_BATCH_COMPUTE],
215 &ice->vtbl, &ice->dbg);
216
217 return ctx;
218 }