1/*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 (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 NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 *    Rob Clark <robclark@freedesktop.org>
25 */
26
27#include "pipe/p_screen.h"
28#include "util/format/u_format.h"
29
30#include "fd4_context.h"
31#include "fd4_emit.h"
32#include "fd4_format.h"
33#include "fd4_resource.h"
34#include "fd4_screen.h"
35
36#include "ir3/ir3_compiler.h"
37
38static bool
39fd4_screen_is_format_supported(struct pipe_screen *pscreen,
40                               enum pipe_format format,
41                               enum pipe_texture_target target,
42                               unsigned sample_count,
43                               unsigned storage_sample_count, unsigned usage)
44{
45   unsigned retval = 0;
46
47   if ((target >= PIPE_MAX_TEXTURE_TYPES) ||
48       (sample_count > 1)) { /* TODO add MSAA */
49      DBG("not supported: format=%s, target=%d, sample_count=%d, usage=%x",
50          util_format_name(format), target, sample_count, usage);
51      return false;
52   }
53
54   if (MAX2(1, sample_count) != MAX2(1, storage_sample_count))
55      return false;
56
57   if ((usage & PIPE_BIND_VERTEX_BUFFER) &&
58       (fd4_pipe2vtx(format) != VFMT4_NONE)) {
59      retval |= PIPE_BIND_VERTEX_BUFFER;
60   }
61
62   if ((usage & PIPE_BIND_SAMPLER_VIEW) &&
63       (fd4_pipe2tex(format) != TFMT4_NONE) &&
64       (target == PIPE_BUFFER || util_format_get_blocksize(format) != 12)) {
65      retval |= PIPE_BIND_SAMPLER_VIEW;
66   }
67
68   if ((usage & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DISPLAY_TARGET |
69                 PIPE_BIND_SCANOUT | PIPE_BIND_SHARED)) &&
70       (fd4_pipe2color(format) != RB4_NONE) &&
71       (fd4_pipe2tex(format) != TFMT4_NONE)) {
72      retval |= usage & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DISPLAY_TARGET |
73                         PIPE_BIND_SCANOUT | PIPE_BIND_SHARED);
74   }
75
76   /* For ARB_framebuffer_no_attachments: */
77   if ((usage & PIPE_BIND_RENDER_TARGET) && (format == PIPE_FORMAT_NONE)) {
78      retval |= usage & PIPE_BIND_RENDER_TARGET;
79   }
80
81   if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
82       (fd4_pipe2depth(format) != (enum a4xx_depth_format) ~0) &&
83       (fd4_pipe2tex(format) != TFMT4_NONE)) {
84      retval |= PIPE_BIND_DEPTH_STENCIL;
85   }
86
87   if ((usage & PIPE_BIND_INDEX_BUFFER) &&
88       (fd_pipe2index(format) != (enum pc_di_index_size) ~0)) {
89      retval |= PIPE_BIND_INDEX_BUFFER;
90   }
91
92   if (retval != usage) {
93      DBG("not supported: format=%s, target=%d, sample_count=%d, "
94          "usage=%x, retval=%x",
95          util_format_name(format), target, sample_count, usage, retval);
96   }
97
98   return retval == usage;
99}
100
101/* clang-format off */
102static const uint8_t primtypes[] = {
103   [PIPE_PRIM_POINTS]         = DI_PT_POINTLIST,
104   [PIPE_PRIM_LINES]          = DI_PT_LINELIST,
105   [PIPE_PRIM_LINE_STRIP]     = DI_PT_LINESTRIP,
106   [PIPE_PRIM_LINE_LOOP]      = DI_PT_LINELOOP,
107   [PIPE_PRIM_TRIANGLES]      = DI_PT_TRILIST,
108   [PIPE_PRIM_TRIANGLE_STRIP] = DI_PT_TRISTRIP,
109   [PIPE_PRIM_TRIANGLE_FAN]   = DI_PT_TRIFAN,
110   [PIPE_PRIM_MAX]            = DI_PT_RECTLIST,  /* internal clear blits */
111};
112/* clang-format on */
113
114void
115fd4_screen_init(struct pipe_screen *pscreen)
116{
117   struct fd_screen *screen = fd_screen(pscreen);
118   screen->max_rts = A4XX_MAX_RENDER_TARGETS;
119   screen->setup_slices = fd4_setup_slices;
120   pscreen->context_create = fd4_context_create;
121   pscreen->is_format_supported = fd4_screen_is_format_supported;
122   fd4_emit_init_screen(pscreen);
123   ir3_screen_init(pscreen);
124
125   screen->primtypes = primtypes;
126}
127