1b8e80941Smrg/*
2b8e80941Smrg * Copyright 2018-2019 Alyssa Rosenzweig
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20b8e80941Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21b8e80941Smrg * SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg */
24b8e80941Smrg
25b8e80941Smrg#include "pan_context.h"
26b8e80941Smrg#include "pan_util.h"
27b8e80941Smrg#include "pan_format.h"
28b8e80941Smrg
29b8e80941Smrg#include "util/u_format.h"
30b8e80941Smrg
31b8e80941Smrgstatic unsigned
32b8e80941Smrgpanfrost_sfbd_format(struct pipe_surface *surf)
33b8e80941Smrg{
34b8e80941Smrg        /* TODO */
35b8e80941Smrg        return 0xb84e0281; /* RGB32, no MSAA */
36b8e80941Smrg}
37b8e80941Smrg
38b8e80941Smrgstatic void
39b8e80941Smrgpanfrost_sfbd_clear(
40b8e80941Smrg                struct panfrost_job *job,
41b8e80941Smrg                struct mali_single_framebuffer *sfbd)
42b8e80941Smrg{
43b8e80941Smrg        struct panfrost_context *ctx = job->ctx;
44b8e80941Smrg
45b8e80941Smrg        if (job->clear & PIPE_CLEAR_COLOR) {
46b8e80941Smrg                sfbd->clear_color_1 = job->clear_color;
47b8e80941Smrg                sfbd->clear_color_2 = job->clear_color;
48b8e80941Smrg                sfbd->clear_color_3 = job->clear_color;
49b8e80941Smrg                sfbd->clear_color_4 = job->clear_color;
50b8e80941Smrg        }
51b8e80941Smrg
52b8e80941Smrg        if (job->clear & PIPE_CLEAR_DEPTH) {
53b8e80941Smrg                sfbd->clear_depth_1 = job->clear_depth;
54b8e80941Smrg                sfbd->clear_depth_2 = job->clear_depth;
55b8e80941Smrg                sfbd->clear_depth_3 = job->clear_depth;
56b8e80941Smrg                sfbd->clear_depth_4 = job->clear_depth;
57b8e80941Smrg
58b8e80941Smrg                sfbd->depth_buffer = ctx->depth_stencil_buffer.gpu;
59b8e80941Smrg                sfbd->depth_buffer_enable = MALI_DEPTH_STENCIL_ENABLE;
60b8e80941Smrg        }
61b8e80941Smrg
62b8e80941Smrg        if (job->clear & PIPE_CLEAR_STENCIL) {
63b8e80941Smrg                sfbd->clear_stencil = job->clear_stencil;
64b8e80941Smrg
65b8e80941Smrg                sfbd->stencil_buffer = ctx->depth_stencil_buffer.gpu;
66b8e80941Smrg                sfbd->stencil_buffer_enable = MALI_DEPTH_STENCIL_ENABLE;
67b8e80941Smrg        }
68b8e80941Smrg
69b8e80941Smrg        /* Set flags based on what has been cleared, for the SFBD case */
70b8e80941Smrg        /* XXX: What do these flags mean? */
71b8e80941Smrg        int clear_flags = 0x101100;
72b8e80941Smrg
73b8e80941Smrg        if (!(job->clear & ~(PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
74b8e80941Smrg                /* On a tiler like this, it's fastest to clear all three buffers at once */
75b8e80941Smrg
76b8e80941Smrg                clear_flags |= MALI_CLEAR_FAST;
77b8e80941Smrg        } else {
78b8e80941Smrg                clear_flags |= MALI_CLEAR_SLOW;
79b8e80941Smrg
80b8e80941Smrg                if (job->clear & PIPE_CLEAR_STENCIL)
81b8e80941Smrg                        clear_flags |= MALI_CLEAR_SLOW_STENCIL;
82b8e80941Smrg        }
83b8e80941Smrg
84b8e80941Smrg        sfbd->clear_flags = clear_flags;
85b8e80941Smrg}
86b8e80941Smrg
87b8e80941Smrgstatic void
88b8e80941Smrgpanfrost_sfbd_set_cbuf(
89b8e80941Smrg                struct mali_single_framebuffer *fb,
90b8e80941Smrg                struct pipe_surface *surf,
91b8e80941Smrg                bool flip_y)
92b8e80941Smrg{
93b8e80941Smrg        struct panfrost_resource *rsrc = pan_resource(surf->texture);
94b8e80941Smrg
95b8e80941Smrg        signed stride = rsrc->bo->slices[0].stride;
96b8e80941Smrg
97b8e80941Smrg        fb->format = panfrost_sfbd_format(surf);
98b8e80941Smrg
99b8e80941Smrg        if (rsrc->bo->layout == PAN_LINEAR) {
100b8e80941Smrg                mali_ptr framebuffer = rsrc->bo->gpu;
101b8e80941Smrg
102b8e80941Smrg                /* The default is upside down from OpenGL's perspective. */
103b8e80941Smrg                if (flip_y) {
104b8e80941Smrg                        framebuffer += stride * (surf->texture->height0 - 1);
105b8e80941Smrg                        stride = -stride;
106b8e80941Smrg                }
107b8e80941Smrg
108b8e80941Smrg                fb->framebuffer = framebuffer;
109b8e80941Smrg                fb->stride = stride;
110b8e80941Smrg        } else {
111b8e80941Smrg                fprintf(stderr, "Invalid render layout\n");
112b8e80941Smrg                assert(0);
113b8e80941Smrg        }
114b8e80941Smrg}
115b8e80941Smrg
116b8e80941Smrg/* Creates an SFBD for the FRAGMENT section of the bound framebuffer */
117b8e80941Smrg
118b8e80941Smrgmali_ptr
119b8e80941Smrgpanfrost_sfbd_fragment(struct panfrost_context *ctx, bool flip_y)
120b8e80941Smrg{
121b8e80941Smrg        struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
122b8e80941Smrg        struct mali_single_framebuffer fb = panfrost_emit_sfbd(ctx);
123b8e80941Smrg
124b8e80941Smrg        panfrost_sfbd_clear(job, &fb);
125b8e80941Smrg
126b8e80941Smrg        /* SFBD does not support MRT natively; sanity check */
127b8e80941Smrg        assert(ctx->pipe_framebuffer.nr_cbufs == 1);
128b8e80941Smrg        panfrost_sfbd_set_cbuf(&fb, ctx->pipe_framebuffer.cbufs[0], flip_y);
129b8e80941Smrg
130b8e80941Smrg        if (ctx->pipe_framebuffer.zsbuf) {
131b8e80941Smrg                /* TODO */
132b8e80941Smrg        }
133b8e80941Smrg
134b8e80941Smrg        if (job->requirements & PAN_REQ_MSAA)
135b8e80941Smrg                fb.format |= MALI_FRAMEBUFFER_MSAA_A | MALI_FRAMEBUFFER_MSAA_B;
136b8e80941Smrg
137b8e80941Smrg        return panfrost_upload_transient(ctx, &fb, sizeof(fb)) | MALI_SFBD;
138b8e80941Smrg}
139