2 * Copyright 2007 VMware, Inc.
3 * Copyright 2016 Advanced Micro Devices, Inc.
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:
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
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.
28 * Common helper functions for PBO up- and downloads.
31 #include "state_tracker/st_context.h"
32 #include "state_tracker/st_pbo.h"
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"
41 st_pbo_create_vs(struct st_context
*st
)
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
;
49 ureg
= ureg_create(PIPE_SHADER_VERTEX
);
53 in_pos
= ureg_DECL_vs_input(ureg
, TGSI_SEMANTIC_POSITION
);
55 out_pos
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
57 if (st
->pbo_upload
.upload_layers
) {
58 in_instanceid
= ureg_DECL_system_value(ureg
, TGSI_SEMANTIC_INSTANCEID
, 0);
60 if (!st
->pbo_upload
.use_gs
)
61 out_layer
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_LAYER
, 0);
64 /* out_pos = in_pos */
65 ureg_MOV(ureg
, out_pos
, in_pos
);
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
));
73 /* out_layer = gl_InstanceID */
74 ureg_MOV(ureg
, out_layer
, in_instanceid
);
80 return ureg_create_shader_and_destroy(ureg
, st
->pipe
);
84 st_pbo_create_gs(struct st_context
*st
)
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
;
94 ureg
= ureg_create(PIPE_SHADER_GEOMETRY
);
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);
102 out_pos
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0);
103 out_layer
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_LAYER
, 0);
105 in_pos
= ureg_DECL_input(ureg
, TGSI_SEMANTIC_POSITION
, 0, 0, 1);
107 imm
= ureg_DECL_immediate_int(ureg
, &zero
, 1);
109 for (i
= 0; i
< 3; ++i
) {
110 struct ureg_src in_pos_vertex
= ureg_src_dimension(in_pos
, i
);
112 /* out_pos = in_pos[i] */
113 ureg_MOV(ureg
, out_pos
, in_pos_vertex
);
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
));
119 ureg_EMIT(ureg
, ureg_scalar(imm
, TGSI_SWIZZLE_X
));
124 return ureg_create_shader_and_destroy(ureg
, st
->pipe
);
128 st_init_pbo_helpers(struct st_context
*st
)
130 struct pipe_context
*pipe
= st
->pipe
;
131 struct pipe_screen
*screen
= pipe
->screen
;
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
)
140 st
->pbo_upload
.rgba_only
=
141 screen
->get_param(screen
, PIPE_CAP_BUFFER_SAMPLER_VIEW_RGBA_ONLY
);
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;
153 memset(&st
->pbo_upload
.blend
, 0, sizeof(struct pipe_blend_state
));
154 st
->pbo_upload
.blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
156 /* Rasterizer state */
157 memset(&st
->pbo_upload
.raster
, 0, sizeof(struct pipe_rasterizer_state
));
158 st
->pbo_upload
.raster
.half_pixel_center
= 1;
162 st_destroy_pbo_helpers(struct st_context
*st
)
164 if (st
->pbo_upload
.fs
) {
165 cso_delete_fragment_shader(st
->cso_context
, st
->pbo_upload
.fs
);
166 st
->pbo_upload
.fs
= NULL
;
169 if (st
->pbo_upload
.gs
) {
170 cso_delete_geometry_shader(st
->cso_context
, st
->pbo_upload
.gs
);
171 st
->pbo_upload
.gs
= NULL
;
174 if (st
->pbo_upload
.vs
) {
175 cso_delete_vertex_shader(st
->cso_context
, st
->pbo_upload
.vs
);
176 st
->pbo_upload
.vs
= NULL
;