iris: Initial commit of a new 'iris' driver for Intel Gen8+ GPUs.
[mesa.git] / src / gallium / drivers / iris / iris_program.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 <errno.h>
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "pipe/p_context.h"
28 #include "pipe/p_screen.h"
29 #include "util/u_atomic.h"
30 #include "compiler/nir/nir.h"
31 #include "compiler/nir/nir_builder.h"
32 #include "intel/compiler/brw_compiler.h"
33 #include "intel/compiler/brw_nir.h"
34 #include "iris_context.h"
35
36 static unsigned
37 get_new_program_id(struct iris_screen *screen)
38 {
39 return p_atomic_inc_return(&screen->program_id);
40 }
41
42 struct iris_uncompiled_shader {
43 struct pipe_shader_state base;
44 unsigned program_id;
45 };
46
47 static void *
48 iris_create_shader_state(struct pipe_context *ctx,
49 const struct pipe_shader_state *state)
50 {
51 struct iris_context *ice = (struct iris_context *)ctx;
52 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
53
54 assert(state->type == PIPE_SHADER_IR_NIR);
55
56 nir_shader *nir = state->ir.nir;
57
58 struct iris_uncompiled_shader *cso =
59 calloc(1, sizeof(struct iris_uncompiled_shader));
60 if (!cso)
61 return NULL;
62
63 nir = brw_preprocess_nir(screen->compiler, nir);
64
65 cso->program_id = get_new_program_id(screen);
66 cso->base.type = PIPE_SHADER_IR_NIR;
67 cso->base.ir.nir = nir;
68
69 return cso;
70 }
71
72 static void
73 iris_delete_shader_state(struct pipe_context *ctx, void *hwcso)
74 {
75 struct iris_uncompiled_shader *cso = hwcso;
76
77 ralloc_free(cso->base.ir.nir);
78 free(cso);
79 }
80
81 void
82 iris_init_program_functions(struct pipe_context *ctx)
83 {
84 ctx->create_vs_state = iris_create_shader_state;
85 ctx->create_tcs_state = iris_create_shader_state;
86 ctx->create_tes_state = iris_create_shader_state;
87 ctx->create_gs_state = iris_create_shader_state;
88 ctx->create_fs_state = iris_create_shader_state;
89
90 ctx->delete_vs_state = iris_delete_shader_state;
91 ctx->delete_tcs_state = iris_delete_shader_state;
92 ctx->delete_tes_state = iris_delete_shader_state;
93 ctx->delete_gs_state = iris_delete_shader_state;
94 ctx->delete_fs_state = iris_delete_shader_state;
95 }