st/mesa: move PBO vertex and geometry shader creation to st_pbo.c
[mesa.git] / src / mesa / state_tracker / st_pbo.c
1 /*
2 * Copyright 2007 VMware, Inc.
3 * Copyright 2016 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file
27 *
28 * Common helper functions for PBO up- and downloads.
29 */
30
31 #include "state_tracker/st_context.h"
32 #include "state_tracker/st_pbo.h"
33
34 #include "pipe/p_context.h"
35 #include "pipe/p_defines.h"
36 #include "pipe/p_screen.h"
37 #include "cso_cache/cso_context.h"
38 #include "tgsi/tgsi_ureg.h"
39
40 void *
41 st_pbo_create_vs(struct st_context *st)
42 {
43 struct ureg_program *ureg;
44 struct ureg_src in_pos;
45 struct ureg_src in_instanceid;
46 struct ureg_dst out_pos;
47 struct ureg_dst out_layer;
48
49 ureg = ureg_create(PIPE_SHADER_VERTEX);
50 if (!ureg)
51 return NULL;
52
53 in_pos = ureg_DECL_vs_input(ureg, TGSI_SEMANTIC_POSITION);
54
55 out_pos = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
56
57 if (st->pbo_upload.upload_layers) {
58 in_instanceid = ureg_DECL_system_value(ureg, TGSI_SEMANTIC_INSTANCEID, 0);
59
60 if (!st->pbo_upload.use_gs)
61 out_layer = ureg_DECL_output(ureg, TGSI_SEMANTIC_LAYER, 0);
62 }
63
64 /* out_pos = in_pos */
65 ureg_MOV(ureg, out_pos, in_pos);
66
67 if (st->pbo_upload.upload_layers) {
68 if (st->pbo_upload.use_gs) {
69 /* out_pos.z = i2f(gl_InstanceID) */
70 ureg_I2F(ureg, ureg_writemask(out_pos, TGSI_WRITEMASK_Z),
71 ureg_scalar(in_instanceid, TGSI_SWIZZLE_X));
72 } else {
73 /* out_layer = gl_InstanceID */
74 ureg_MOV(ureg, out_layer, in_instanceid);
75 }
76 }
77
78 ureg_END(ureg);
79
80 return ureg_create_shader_and_destroy(ureg, st->pipe);
81 }
82
83 void *
84 st_pbo_create_gs(struct st_context *st)
85 {
86 static const int zero = 0;
87 struct ureg_program *ureg;
88 struct ureg_dst out_pos;
89 struct ureg_dst out_layer;
90 struct ureg_src in_pos;
91 struct ureg_src imm;
92 unsigned i;
93
94 ureg = ureg_create(PIPE_SHADER_GEOMETRY);
95 if (!ureg)
96 return NULL;
97
98 ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM, PIPE_PRIM_TRIANGLES);
99 ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM, PIPE_PRIM_TRIANGLE_STRIP);
100 ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES, 3);
101
102 out_pos = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
103 out_layer = ureg_DECL_output(ureg, TGSI_SEMANTIC_LAYER, 0);
104
105 in_pos = ureg_DECL_input(ureg, TGSI_SEMANTIC_POSITION, 0, 0, 1);
106
107 imm = ureg_DECL_immediate_int(ureg, &zero, 1);
108
109 for (i = 0; i < 3; ++i) {
110 struct ureg_src in_pos_vertex = ureg_src_dimension(in_pos, i);
111
112 /* out_pos = in_pos[i] */
113 ureg_MOV(ureg, out_pos, in_pos_vertex);
114
115 /* out_layer.x = f2i(in_pos[i].z) */
116 ureg_F2I(ureg, ureg_writemask(out_layer, TGSI_WRITEMASK_X),
117 ureg_scalar(in_pos_vertex, TGSI_SWIZZLE_Z));
118
119 ureg_EMIT(ureg, ureg_scalar(imm, TGSI_SWIZZLE_X));
120 }
121
122 ureg_END(ureg);
123
124 return ureg_create_shader_and_destroy(ureg, st->pipe);
125 }
126
127 void
128 st_init_pbo_helpers(struct st_context *st)
129 {
130 struct pipe_context *pipe = st->pipe;
131 struct pipe_screen *screen = pipe->screen;
132
133 st->pbo_upload.enabled =
134 screen->get_param(screen, PIPE_CAP_TEXTURE_BUFFER_OBJECTS) &&
135 screen->get_param(screen, PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT) >= 1 &&
136 screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_INTEGERS);
137 if (!st->pbo_upload.enabled)
138 return;
139
140 st->pbo_upload.rgba_only =
141 screen->get_param(screen, PIPE_CAP_BUFFER_SAMPLER_VIEW_RGBA_ONLY);
142
143 if (screen->get_param(screen, PIPE_CAP_TGSI_INSTANCEID)) {
144 if (screen->get_param(screen, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT)) {
145 st->pbo_upload.upload_layers = true;
146 } else if (screen->get_param(screen, PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES) >= 3) {
147 st->pbo_upload.upload_layers = true;
148 st->pbo_upload.use_gs = true;
149 }
150 }
151
152 /* Blend state */
153 memset(&st->pbo_upload.blend, 0, sizeof(struct pipe_blend_state));
154 st->pbo_upload.blend.rt[0].colormask = PIPE_MASK_RGBA;
155
156 /* Rasterizer state */
157 memset(&st->pbo_upload.raster, 0, sizeof(struct pipe_rasterizer_state));
158 st->pbo_upload.raster.half_pixel_center = 1;
159 }
160
161 void
162 st_destroy_pbo_helpers(struct st_context *st)
163 {
164 if (st->pbo_upload.fs) {
165 cso_delete_fragment_shader(st->cso_context, st->pbo_upload.fs);
166 st->pbo_upload.fs = NULL;
167 }
168
169 if (st->pbo_upload.gs) {
170 cso_delete_geometry_shader(st->cso_context, st->pbo_upload.gs);
171 st->pbo_upload.gs = NULL;
172 }
173
174 if (st->pbo_upload.vs) {
175 cso_delete_vertex_shader(st->cso_context, st->pbo_upload.vs);
176 st->pbo_upload.vs = NULL;
177 }
178 }