1 /**************************************************************************
2 *
3 * Copyright 2019 Sonny Jiang <[email protected]>
4 * Copyright 2019 Advanced Micro Devices, 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
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "pipe/p_context.h"
30 #include "pipe/p_state.h"
31
32 #include "u_bitcast.h"
33 #include "util/format/u_format.h"
34 #include "u_sampler.h"
35 #include "nir/nir_builder.h"
36 #include "u_inlines.h"
37 #include "u_compute.h"
38
blit_compute_shader(struct pipe_context * ctx)39 static void *blit_compute_shader(struct pipe_context *ctx)
40 {
41 /*
42 #version 450
43
44 layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
45 layout (binding = 0) uniform sampler2DArray samp;
46 layout (binding = 0, rgba32f) uniform writeonly image2D image;
47
48 layout (std140, binding = 0) uniform ubo
49 {
50 vec4 src;
51 vec4 scale;
52 ivec4 dst;
53 vec4 coord_max;
54 };
55
56 void main()
57 {
58 ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
59 vec3 tex_pos = vec3(pos.x + 0.5, pos.y + 0.5, pos.z);
60 tex_pos = tex_pos * scale.xyz + src.xyz;
61 tex_pos.xy = min(tex_pos.xy, coord_max.xy);
62 vec4 color = texture(samp, tex_pos);
63 ivec2 image_pos = pos.xy + dst.xy;
64 imageStore(image, image_pos, color);
65 }
66 */
67 const struct glsl_type *sampler_type =
68 glsl_sampler_type(GLSL_SAMPLER_DIM_2D, /*is_shadow*/ false, /*is_array*/ true, GLSL_TYPE_FLOAT);
69 const struct glsl_type *image_type =
70 glsl_image_type(GLSL_SAMPLER_DIM_2D, /*is_array*/ true, GLSL_TYPE_FLOAT);
71 const nir_shader_compiler_options *options =
72 ctx->screen->get_compiler_options(ctx->screen, PIPE_SHADER_IR_NIR, PIPE_SHADER_COMPUTE);
73
74 nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, options, "blit_cs");
75 b.shader->info.workgroup_size[0] = 64;
76 b.shader->info.workgroup_size[1] = 1;
77 b.shader->info.workgroup_size[2] = 1;
78 b.shader->info.num_ubos = 1;
79
80 nir_def *zero = nir_imm_int(&b, 0);
81 nir_def *undef32 = nir_undef(&b, 1, 32);
82
83 nir_def *params[4];
84 b.shader->num_uniforms = ARRAY_SIZE(params);
85 for (unsigned i = 0; i < b.shader->num_uniforms; ++i)
86 params[i] = nir_load_ubo(&b, 4, 32, zero, nir_imm_int(&b, i * 16), .align_mul = 4, .range = ~0);
87
88 nir_variable *sampler = nir_variable_create(b.shader, nir_var_uniform, sampler_type, "sampler");
89 sampler->data.binding = 0;
90 BITSET_SET(b.shader->info.textures_used, 0);
91 BITSET_SET(b.shader->info.samplers_used, 0);
92
93 nir_variable *image = nir_variable_create(b.shader, nir_var_image, image_type, "image");
94 image->data.binding = 0;
95 image->data.image.format = PIPE_FORMAT_R32G32B32A32_FLOAT;
96 BITSET_SET(b.shader->info.images_used, 0);
97
98 nir_def *block_ids = nir_load_workgroup_id(&b);
99 nir_def *local_ids = nir_load_local_invocation_id(&b);
100 nir_def *ids = nir_iadd(&b, nir_imul(&b, block_ids, nir_imm_ivec3(&b, 64, 1, 1)), local_ids);
101
102 nir_def *tex_pos = nir_u2f32(&b, ids);
103 tex_pos = nir_fadd(&b, tex_pos, nir_imm_vec3(&b, 0.5f, 0.5f, 0.0f));
104 tex_pos = nir_ffma(&b, tex_pos, params[1], params[0]);
105 nir_def *z = nir_channel(&b, tex_pos, 2);
106 tex_pos = nir_fmin(&b, tex_pos, params[3]);
107 tex_pos = nir_vector_insert_imm(&b, tex_pos, z, 2);
108 tex_pos = nir_channels(&b, tex_pos, 0x7);
109
110 nir_deref_instr *tex_deref = nir_build_deref_var(&b, sampler);
111 nir_def *color = nir_tex_deref(&b, tex_deref, tex_deref, tex_pos);
112
113 nir_def *image_pos = nir_pad_vector_imm_int(&b, ids, 0, 4);
114 image_pos = nir_iadd(&b, image_pos, params[2]);
115 nir_image_deref_store(&b, &nir_build_deref_var(&b, image)->def, image_pos, undef32, color, zero);
116
117 ctx->screen->finalize_nir(ctx->screen, b.shader);
118
119 struct pipe_compute_state state = {0};
120 state.ir_type = PIPE_SHADER_IR_NIR;
121 state.prog = b.shader;
122
123 return ctx->create_compute_state(ctx, &state);
124 }
125
util_compute_blit(struct pipe_context * ctx,struct pipe_blit_info * blit_info,void ** compute_state)126 void util_compute_blit(struct pipe_context *ctx, struct pipe_blit_info *blit_info,
127 void **compute_state)
128 {
129 if (blit_info->src.box.width == 0 || blit_info->src.box.height == 0 ||
130 blit_info->dst.box.width == 0 || blit_info->dst.box.height == 0)
131 return;
132
133 struct pipe_resource *src = blit_info->src.resource;
134 struct pipe_resource *dst = blit_info->dst.resource;
135 struct pipe_sampler_view src_templ = {0}, *src_view;
136 void *sampler_state_p;
137 unsigned width = blit_info->dst.box.width;
138 unsigned height = blit_info->dst.box.height;
139 float x_scale = blit_info->src.box.width / (float)blit_info->dst.box.width;
140 float y_scale = blit_info->src.box.height / (float)blit_info->dst.box.height;
141 float z_scale = blit_info->src.box.depth / (float)blit_info->dst.box.depth;
142
143 unsigned data[] = {u_bitcast_f2u(blit_info->src.box.x / (float)src->width0),
144 u_bitcast_f2u(blit_info->src.box.y / (float)src->height0),
145 u_bitcast_f2u(blit_info->src.box.z),
146 u_bitcast_f2u(0),
147 u_bitcast_f2u(x_scale / src->width0),
148 u_bitcast_f2u(y_scale / src->height0),
149 u_bitcast_f2u(z_scale),
150 u_bitcast_f2u(0),
151 blit_info->dst.box.x,
152 blit_info->dst.box.y,
153 blit_info->dst.box.z,
154 0,
155 u_bitcast_f2u((blit_info->src.box.x + blit_info->src.box.width - 0.5) /
156 (float)src->width0),
157 u_bitcast_f2u((blit_info->src.box.y + blit_info->src.box.height - 0.5) /
158 (float)src->height0),
159 0,
160 0};
161
162 struct pipe_constant_buffer cb = {0};
163 cb.buffer_size = sizeof(data);
164 cb.user_buffer = data;
165 ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, false, &cb);
166
167 struct pipe_image_view image = {0};
168 image.resource = dst;
169 image.shader_access = image.access = PIPE_IMAGE_ACCESS_WRITE;
170 image.format = util_format_linear(blit_info->dst.format);
171 image.u.tex.level = blit_info->dst.level;
172 image.u.tex.first_layer = 0;
173 image.u.tex.last_layer = (unsigned)(dst->array_size - 1);
174
175 ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 1, 0, &image);
176
177 struct pipe_sampler_state sampler_state={0};
178 sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
179 sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
180 sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
181
182 if (blit_info->filter == PIPE_TEX_FILTER_LINEAR) {
183 sampler_state.min_img_filter = PIPE_TEX_FILTER_LINEAR;
184 sampler_state.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
185 }
186
187 sampler_state_p = ctx->create_sampler_state(ctx, &sampler_state);
188 ctx->bind_sampler_states(ctx, PIPE_SHADER_COMPUTE, 0, 1, &sampler_state_p);
189
190 /* Initialize the sampler view. */
191 u_sampler_view_default_template(&src_templ, src, src->format);
192 src_templ.format = util_format_linear(blit_info->src.format);
193 src_view = ctx->create_sampler_view(ctx, src, &src_templ);
194 ctx->set_sampler_views(ctx, PIPE_SHADER_COMPUTE, 0, 1, 0, false, &src_view);
195
196 if (!*compute_state)
197 *compute_state = blit_compute_shader(ctx);
198 ctx->bind_compute_state(ctx, *compute_state);
199
200 struct pipe_grid_info grid_info = {0};
201 grid_info.block[0] = 64;
202 grid_info.last_block[0] = width % 64;
203 grid_info.block[1] = 1;
204 grid_info.block[2] = 1;
205 grid_info.grid[0] = DIV_ROUND_UP(width, 64);
206 grid_info.grid[1] = height;
207 grid_info.grid[2] = 1;
208
209 ctx->launch_grid(ctx, &grid_info);
210
211 ctx->memory_barrier(ctx, PIPE_BARRIER_ALL);
212
213 ctx->set_shader_images(ctx, PIPE_SHADER_COMPUTE, 0, 0, 1, NULL);
214 ctx->set_constant_buffer(ctx, PIPE_SHADER_COMPUTE, 0, false, NULL);
215 ctx->set_sampler_views(ctx, PIPE_SHADER_COMPUTE, 0, 0, 1, false, NULL);
216 pipe_sampler_view_reference(&src_view, NULL);
217 ctx->delete_sampler_state(ctx, sampler_state_p);
218 ctx->bind_compute_state(ctx, NULL);
219 }
220