1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2021 Alyssa Rosenzweig
3bf215546Sopenharmony_ci * Copyright (C) 2020-2021 Collabora, Ltd.
4bf215546Sopenharmony_ci * Copyright (C) 2014 Broadcom
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
14bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
15bf215546Sopenharmony_ci * Software.
16bf215546Sopenharmony_ci *
17bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23bf215546Sopenharmony_ci * SOFTWARE.
24bf215546Sopenharmony_ci */
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "agx_state.h"
27bf215546Sopenharmony_ci#include "compiler/nir/nir_builder.h"
28bf215546Sopenharmony_ci#include "asahi/compiler/agx_compile.h"
29bf215546Sopenharmony_ci#include "gallium/auxiliary/util/u_blitter.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_cistatic void
32bf215546Sopenharmony_ciagx_build_reload_shader(struct agx_device *dev)
33bf215546Sopenharmony_ci{
34bf215546Sopenharmony_ci   nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT,
35bf215546Sopenharmony_ci         &agx_nir_options, "agx_reload");
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci   nir_variable *out = nir_variable_create(b.shader, nir_var_shader_out,
38bf215546Sopenharmony_ci         glsl_vector_type(GLSL_TYPE_FLOAT, 4), "output");
39bf215546Sopenharmony_ci   out->data.location = FRAG_RESULT_DATA0;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   nir_ssa_def *fragcoord = nir_load_frag_coord(&b);
42bf215546Sopenharmony_ci   nir_ssa_def *coord = nir_channels(&b, fragcoord, 0x3);
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   nir_tex_instr *tex = nir_tex_instr_create(b.shader, 1);
45bf215546Sopenharmony_ci   tex->dest_type = nir_type_float32;
46bf215546Sopenharmony_ci   tex->sampler_dim = GLSL_SAMPLER_DIM_RECT;
47bf215546Sopenharmony_ci   tex->op = nir_texop_tex;
48bf215546Sopenharmony_ci   tex->src[0].src_type = nir_tex_src_coord;
49bf215546Sopenharmony_ci   tex->src[0].src = nir_src_for_ssa(coord);
50bf215546Sopenharmony_ci   tex->coord_components = 2;
51bf215546Sopenharmony_ci   nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
52bf215546Sopenharmony_ci   nir_builder_instr_insert(&b, &tex->instr);
53bf215546Sopenharmony_ci   nir_store_var(&b, out, &tex->dest.ssa, 0xFF);
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci   unsigned offset = 0;
56bf215546Sopenharmony_ci   unsigned bo_size = 4096;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   struct agx_bo *bo = agx_bo_create(dev, bo_size, AGX_MEMORY_TYPE_SHADER);
59bf215546Sopenharmony_ci   dev->reload.bo = bo;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   for (unsigned i = 0; i < AGX_NUM_FORMATS; ++i) {
62bf215546Sopenharmony_ci      struct util_dynarray binary;
63bf215546Sopenharmony_ci      util_dynarray_init(&binary, NULL);
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci      nir_shader *s = nir_shader_clone(NULL, b.shader);
66bf215546Sopenharmony_ci      struct agx_shader_info info;
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci      struct agx_shader_key key = {
69bf215546Sopenharmony_ci         .fs.tib_formats[0] = i
70bf215546Sopenharmony_ci      };
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci      agx_compile_shader_nir(s, &key, &binary, &info);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci      assert(offset + binary.size < bo_size);
75bf215546Sopenharmony_ci      memcpy(((uint8_t *) bo->ptr.cpu) + offset, binary.data, binary.size);
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci      dev->reload.format[i] = bo->ptr.gpu + offset;
78bf215546Sopenharmony_ci      offset += ALIGN_POT(binary.size, 128);
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci      util_dynarray_fini(&binary);
81bf215546Sopenharmony_ci   }
82bf215546Sopenharmony_ci}
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_cistatic void
85bf215546Sopenharmony_ciagx_blitter_save(struct agx_context *ctx, struct blitter_context *blitter,
86bf215546Sopenharmony_ci                 bool render_cond)
87bf215546Sopenharmony_ci{
88bf215546Sopenharmony_ci   util_blitter_save_vertex_buffer_slot(blitter, ctx->vertex_buffers);
89bf215546Sopenharmony_ci   util_blitter_save_vertex_elements(blitter, ctx->attributes);
90bf215546Sopenharmony_ci   util_blitter_save_vertex_shader(blitter, ctx->stage[PIPE_SHADER_VERTEX].shader);
91bf215546Sopenharmony_ci   util_blitter_save_rasterizer(blitter, ctx->rast);
92bf215546Sopenharmony_ci   util_blitter_save_viewport(blitter, &ctx->viewport);
93bf215546Sopenharmony_ci   util_blitter_save_scissor(blitter, &ctx->scissor);
94bf215546Sopenharmony_ci   util_blitter_save_fragment_shader(blitter, ctx->stage[PIPE_SHADER_FRAGMENT].shader);
95bf215546Sopenharmony_ci   util_blitter_save_blend(blitter, ctx->blend);
96bf215546Sopenharmony_ci   util_blitter_save_depth_stencil_alpha(blitter, &ctx->zs);
97bf215546Sopenharmony_ci   util_blitter_save_stencil_ref(blitter, &ctx->stencil_ref);
98bf215546Sopenharmony_ci   util_blitter_save_so_targets(blitter, 0, NULL);
99bf215546Sopenharmony_ci   util_blitter_save_sample_mask(blitter, ctx->sample_mask, 0);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   util_blitter_save_framebuffer(blitter, &ctx->framebuffer);
102bf215546Sopenharmony_ci   util_blitter_save_fragment_sampler_states(blitter,
103bf215546Sopenharmony_ci         ctx->stage[PIPE_SHADER_FRAGMENT].sampler_count,
104bf215546Sopenharmony_ci         (void **)(ctx->stage[PIPE_SHADER_FRAGMENT].samplers));
105bf215546Sopenharmony_ci   util_blitter_save_fragment_sampler_views(blitter,
106bf215546Sopenharmony_ci         ctx->stage[PIPE_SHADER_FRAGMENT].texture_count,
107bf215546Sopenharmony_ci         (struct pipe_sampler_view **)ctx->stage[PIPE_SHADER_FRAGMENT].textures);
108bf215546Sopenharmony_ci   util_blitter_save_fragment_constant_buffer_slot(blitter,
109bf215546Sopenharmony_ci         ctx->stage[PIPE_SHADER_FRAGMENT].cb);
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   if (!render_cond) {
112bf215546Sopenharmony_ci      util_blitter_save_render_condition(blitter,
113bf215546Sopenharmony_ci            (struct pipe_query *) ctx->cond_query,
114bf215546Sopenharmony_ci            ctx->cond_cond, ctx->cond_mode);
115bf215546Sopenharmony_ci   }
116bf215546Sopenharmony_ci}
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_civoid
119bf215546Sopenharmony_ciagx_blit(struct pipe_context *pipe,
120bf215546Sopenharmony_ci              const struct pipe_blit_info *info)
121bf215546Sopenharmony_ci{
122bf215546Sopenharmony_ci   //if (info->render_condition_enable &&
123bf215546Sopenharmony_ci   //    !agx_render_condition_check(pan_context(pipe)))
124bf215546Sopenharmony_ci   //        return;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   struct agx_context *ctx = agx_context(pipe);
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   if (!util_blitter_is_blit_supported(ctx->blitter, info))
129bf215546Sopenharmony_ci      unreachable("Unsupported blit\n");
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   agx_blitter_save(ctx, ctx->blitter, info->render_condition_enable);
132bf215546Sopenharmony_ci   util_blitter_blit(ctx->blitter, info);
133bf215546Sopenharmony_ci}
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci/* We need some fixed shaders for common rendering tasks. When colour buffer
136bf215546Sopenharmony_ci * reload is not in use, a shader is used to clear a particular colour. At the
137bf215546Sopenharmony_ci * end of rendering a tile, a shader is used to write it out. These shaders are
138bf215546Sopenharmony_ci * too trivial to go through the compiler at this stage. */
139bf215546Sopenharmony_ci#define AGX_STOP \
140bf215546Sopenharmony_ci	0x88, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, \
141bf215546Sopenharmony_ci	0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00 \
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci#define AGX_BLEND \
144bf215546Sopenharmony_ci	0x09, 0x00, 0x00, 0x04, 0xf0, 0xfc, 0x80, 0x03
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci/* Clears the tilebuffer, where u6-u7 are preloaded with the FP16 clear colour
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci   0: 7e018c098040         bitop_mov        r0, u6
149bf215546Sopenharmony_ci   6: 7e058e098000         bitop_mov        r1, u7
150bf215546Sopenharmony_ci   c: 09000004f0fc8003     TODO.blend
151bf215546Sopenharmony_ci   */
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_cistatic uint8_t shader_clear[] = {
154bf215546Sopenharmony_ci   0x7e, 0x01, 0x8c, 0x09, 0x80, 0x40,
155bf215546Sopenharmony_ci   0x7e, 0x05, 0x8e, 0x09, 0x80, 0x00,
156bf215546Sopenharmony_ci   AGX_BLEND,
157bf215546Sopenharmony_ci   AGX_STOP
158bf215546Sopenharmony_ci};
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_cistatic uint8_t shader_store[] = {
161bf215546Sopenharmony_ci   0x7e, 0x00, 0x04, 0x09, 0x80, 0x00,
162bf215546Sopenharmony_ci   0xb1, 0x80, 0x00, 0x80, 0x00, 0x4a, 0x00, 0x00, 0x0a, 0x00,
163bf215546Sopenharmony_ci   AGX_STOP
164bf215546Sopenharmony_ci};
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_civoid
167bf215546Sopenharmony_ciagx_internal_shaders(struct agx_device *dev)
168bf215546Sopenharmony_ci{
169bf215546Sopenharmony_ci   unsigned clear_offset = 0;
170bf215546Sopenharmony_ci   unsigned store_offset = 1024;
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   struct agx_bo *bo = agx_bo_create(dev, 4096, AGX_MEMORY_TYPE_SHADER);
173bf215546Sopenharmony_ci   memcpy(((uint8_t *) bo->ptr.cpu) + clear_offset, shader_clear, sizeof(shader_clear));
174bf215546Sopenharmony_ci   memcpy(((uint8_t *) bo->ptr.cpu) + store_offset, shader_store, sizeof(shader_store));
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci   dev->internal.bo = bo;
177bf215546Sopenharmony_ci   dev->internal.clear = bo->ptr.gpu + clear_offset;
178bf215546Sopenharmony_ci   dev->internal.store = bo->ptr.gpu + store_offset;
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci   agx_build_reload_shader(dev);
181bf215546Sopenharmony_ci}
182