Merge remote-tracking branch 'public/master' into vulkan
[mesa.git] / src / gallium / drivers / freedreno / a4xx / fd4_context.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29
30 #include "fd4_context.h"
31 #include "fd4_blend.h"
32 #include "fd4_draw.h"
33 #include "fd4_emit.h"
34 #include "fd4_gmem.h"
35 #include "fd4_program.h"
36 #include "fd4_query.h"
37 #include "fd4_rasterizer.h"
38 #include "fd4_texture.h"
39 #include "fd4_zsa.h"
40
41 static void
42 fd4_context_destroy(struct pipe_context *pctx)
43 {
44 struct fd4_context *fd4_ctx = fd4_context(fd_context(pctx));
45
46 fd_bo_del(fd4_ctx->vs_pvt_mem);
47 fd_bo_del(fd4_ctx->fs_pvt_mem);
48 fd_bo_del(fd4_ctx->vsc_size_mem);
49
50 pctx->delete_vertex_elements_state(pctx, fd4_ctx->solid_vbuf_state.vtx);
51 pctx->delete_vertex_elements_state(pctx, fd4_ctx->blit_vbuf_state.vtx);
52
53 pipe_resource_reference(&fd4_ctx->solid_vbuf, NULL);
54 pipe_resource_reference(&fd4_ctx->blit_texcoord_vbuf, NULL);
55
56 u_upload_destroy(fd4_ctx->border_color_uploader);
57
58 fd_context_destroy(pctx);
59 }
60
61 /* TODO we could combine a few of these small buffers (solid_vbuf,
62 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
63 * save a tiny bit of memory
64 */
65
66 static struct pipe_resource *
67 create_solid_vertexbuf(struct pipe_context *pctx)
68 {
69 static const float init_shader_const[] = {
70 -1.000000, +1.000000, +1.000000,
71 +1.000000, -1.000000, +1.000000,
72 };
73 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
74 PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, sizeof(init_shader_const));
75 pipe_buffer_write(pctx, prsc, 0,
76 sizeof(init_shader_const), init_shader_const);
77 return prsc;
78 }
79
80 static struct pipe_resource *
81 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
82 {
83 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
84 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
85 return prsc;
86 }
87
88 static const uint8_t primtypes[PIPE_PRIM_MAX] = {
89 [PIPE_PRIM_POINTS] = DI_PT_POINTLIST,
90 [PIPE_PRIM_LINES] = DI_PT_LINELIST,
91 [PIPE_PRIM_LINE_STRIP] = DI_PT_LINESTRIP,
92 [PIPE_PRIM_LINE_LOOP] = DI_PT_LINELOOP,
93 [PIPE_PRIM_TRIANGLES] = DI_PT_TRILIST,
94 [PIPE_PRIM_TRIANGLE_STRIP] = DI_PT_TRISTRIP,
95 [PIPE_PRIM_TRIANGLE_FAN] = DI_PT_TRIFAN,
96 };
97
98 struct pipe_context *
99 fd4_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
100 {
101 struct fd_screen *screen = fd_screen(pscreen);
102 struct fd4_context *fd4_ctx = CALLOC_STRUCT(fd4_context);
103 struct pipe_context *pctx;
104
105 if (!fd4_ctx)
106 return NULL;
107
108 pctx = &fd4_ctx->base.base;
109
110 fd4_ctx->base.dev = fd_device_ref(screen->dev);
111 fd4_ctx->base.screen = fd_screen(pscreen);
112
113 pctx->destroy = fd4_context_destroy;
114 pctx->create_blend_state = fd4_blend_state_create;
115 pctx->create_rasterizer_state = fd4_rasterizer_state_create;
116 pctx->create_depth_stencil_alpha_state = fd4_zsa_state_create;
117
118 fd4_draw_init(pctx);
119 fd4_gmem_init(pctx);
120 fd4_texture_init(pctx);
121 fd4_prog_init(pctx);
122 fd4_emit_init(pctx);
123
124 pctx = fd_context_init(&fd4_ctx->base, pscreen, primtypes, priv);
125 if (!pctx)
126 return NULL;
127
128 fd4_ctx->vs_pvt_mem = fd_bo_new(screen->dev, 0x2000,
129 DRM_FREEDRENO_GEM_TYPE_KMEM);
130
131 fd4_ctx->fs_pvt_mem = fd_bo_new(screen->dev, 0x2000,
132 DRM_FREEDRENO_GEM_TYPE_KMEM);
133
134 fd4_ctx->vsc_size_mem = fd_bo_new(screen->dev, 0x1000,
135 DRM_FREEDRENO_GEM_TYPE_KMEM);
136
137 fd4_ctx->solid_vbuf = create_solid_vertexbuf(pctx);
138 fd4_ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
139
140 /* setup solid_vbuf_state: */
141 fd4_ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state(
142 pctx, 1, (struct pipe_vertex_element[]){{
143 .vertex_buffer_index = 0,
144 .src_offset = 0,
145 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
146 }});
147 fd4_ctx->solid_vbuf_state.vertexbuf.count = 1;
148 fd4_ctx->solid_vbuf_state.vertexbuf.vb[0].stride = 12;
149 fd4_ctx->solid_vbuf_state.vertexbuf.vb[0].buffer = fd4_ctx->solid_vbuf;
150
151 /* setup blit_vbuf_state: */
152 fd4_ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state(
153 pctx, 2, (struct pipe_vertex_element[]){{
154 .vertex_buffer_index = 0,
155 .src_offset = 0,
156 .src_format = PIPE_FORMAT_R32G32_FLOAT,
157 }, {
158 .vertex_buffer_index = 1,
159 .src_offset = 0,
160 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
161 }});
162 fd4_ctx->blit_vbuf_state.vertexbuf.count = 2;
163 fd4_ctx->blit_vbuf_state.vertexbuf.vb[0].stride = 8;
164 fd4_ctx->blit_vbuf_state.vertexbuf.vb[0].buffer = fd4_ctx->blit_texcoord_vbuf;
165 fd4_ctx->blit_vbuf_state.vertexbuf.vb[1].stride = 12;
166 fd4_ctx->blit_vbuf_state.vertexbuf.vb[1].buffer = fd4_ctx->solid_vbuf;
167
168 fd4_query_context_init(pctx);
169
170 fd4_ctx->border_color_uploader = u_upload_create(pctx, 4096, 0,
171 PIPE_USAGE_STREAM);
172
173 return pctx;
174 }