gallium: split transfer_inline_write into buffer and texture callbacks
[mesa.git] / src / gallium / tests / graw / graw_util.h
1
2 #include "state_tracker/graw.h"
3
4 #include "pipe/p_context.h"
5 #include "pipe/p_defines.h"
6 #include "pipe/p_screen.h"
7 #include "pipe/p_shader_tokens.h"
8 #include "pipe/p_state.h"
9
10 #include "util/u_box.h"
11 #include "util/u_debug.h"
12 #include "util/u_debug_image.h"
13 #include "util/u_draw_quad.h"
14 #include "util/u_format.h"
15 #include "util/u_inlines.h"
16 #include "util/u_memory.h"
17
18
19 struct graw_info
20 {
21 struct pipe_screen *screen;
22 struct pipe_context *ctx;
23 struct pipe_resource *color_buf[PIPE_MAX_COLOR_BUFS], *zs_buf;
24 struct pipe_surface *color_surf[PIPE_MAX_COLOR_BUFS], *zs_surf;
25 void *window;
26 };
27
28
29
30 static inline boolean
31 graw_util_create_window(struct graw_info *info,
32 int width, int height,
33 int num_cbufs, bool zstencil_buf)
34 {
35 static const enum pipe_format formats[] = {
36 PIPE_FORMAT_RGBA8888_UNORM,
37 PIPE_FORMAT_BGRA8888_UNORM,
38 PIPE_FORMAT_NONE
39 };
40 enum pipe_format format;
41 struct pipe_resource resource_temp;
42 struct pipe_surface surface_temp;
43 int i;
44
45 memset(info, 0, sizeof(*info));
46
47 /* It's hard to say whether window or screen should be created
48 * first. Different environments would prefer one or the other.
49 *
50 * Also, no easy way of querying supported formats if the screen
51 * cannot be created first.
52 */
53 for (i = 0; info->window == NULL && formats[i] != PIPE_FORMAT_NONE; i++) {
54 info->screen = graw_create_window_and_screen(0, 0, width, height,
55 formats[i],
56 &info->window);
57 format = formats[i];
58 }
59 if (!info->screen || !info->window) {
60 debug_printf("graw: Failed to create screen/window\n");
61 return FALSE;
62 }
63
64 info->ctx = info->screen->context_create(info->screen, NULL, 0);
65 if (info->ctx == NULL) {
66 debug_printf("graw: Failed to create context\n");
67 return FALSE;
68 }
69
70 for (i = 0; i < num_cbufs; i++) {
71 /* create color texture */
72 resource_temp.target = PIPE_TEXTURE_2D;
73 resource_temp.format = format;
74 resource_temp.width0 = width;
75 resource_temp.height0 = height;
76 resource_temp.depth0 = 1;
77 resource_temp.array_size = 1;
78 resource_temp.last_level = 0;
79 resource_temp.nr_samples = 1;
80 resource_temp.bind = (PIPE_BIND_RENDER_TARGET |
81 PIPE_BIND_DISPLAY_TARGET);
82 info->color_buf[i] = info->screen->resource_create(info->screen,
83 &resource_temp);
84 if (info->color_buf[i] == NULL) {
85 debug_printf("graw: Failed to create color texture\n");
86 return FALSE;
87 }
88
89 /* create color surface */
90 surface_temp.format = resource_temp.format;
91 surface_temp.u.tex.level = 0;
92 surface_temp.u.tex.first_layer = 0;
93 surface_temp.u.tex.last_layer = 0;
94 info->color_surf[i] = info->ctx->create_surface(info->ctx,
95 info->color_buf[i],
96 &surface_temp);
97 if (info->color_surf[i] == NULL) {
98 debug_printf("graw: Failed to get color surface\n");
99 return FALSE;
100 }
101 }
102
103 /* create Z texture (XXX try other Z/S formats if needed) */
104 resource_temp.target = PIPE_TEXTURE_2D;
105 resource_temp.format = PIPE_FORMAT_S8_UINT_Z24_UNORM;
106 resource_temp.width0 = width;
107 resource_temp.height0 = height;
108 resource_temp.depth0 = 1;
109 resource_temp.array_size = 1;
110 resource_temp.last_level = 0;
111 resource_temp.nr_samples = 1;
112 resource_temp.bind = PIPE_BIND_DEPTH_STENCIL;
113 info->zs_buf = info->screen->resource_create(info->screen, &resource_temp);
114 if (!info->zs_buf) {
115 debug_printf("graw: Failed to create Z texture\n");
116 return FALSE;
117 }
118
119 /* create z surface */
120 surface_temp.format = resource_temp.format;
121 surface_temp.u.tex.level = 0;
122 surface_temp.u.tex.first_layer = 0;
123 surface_temp.u.tex.last_layer = 0;
124 info->zs_surf = info->ctx->create_surface(info->ctx,
125 info->zs_buf,
126 &surface_temp);
127 if (info->zs_surf == NULL) {
128 debug_printf("graw: Failed to get Z surface\n");
129 return FALSE;
130 }
131
132 {
133 struct pipe_framebuffer_state fb;
134 memset(&fb, 0, sizeof fb);
135 fb.nr_cbufs = num_cbufs;
136 fb.width = width;
137 fb.height = height;
138 for (i = 0; i < num_cbufs; i++)
139 fb.cbufs[i] = info->color_surf[i];
140 fb.zsbuf = info->zs_surf;
141 info->ctx->set_framebuffer_state(info->ctx, &fb);
142 }
143
144 return TRUE;
145 }
146
147
148 static inline void
149 graw_util_default_state(struct graw_info *info, boolean depth_test)
150 {
151 {
152 struct pipe_blend_state blend;
153 void *handle;
154 memset(&blend, 0, sizeof blend);
155 blend.rt[0].colormask = PIPE_MASK_RGBA;
156 handle = info->ctx->create_blend_state(info->ctx, &blend);
157 info->ctx->bind_blend_state(info->ctx, handle);
158 }
159
160 {
161 struct pipe_depth_stencil_alpha_state depthStencilAlpha;
162 void *handle;
163 memset(&depthStencilAlpha, 0, sizeof depthStencilAlpha);
164 depthStencilAlpha.depth.enabled = depth_test;
165 depthStencilAlpha.depth.writemask = 1;
166 depthStencilAlpha.depth.func = PIPE_FUNC_LESS;
167 handle = info->ctx->create_depth_stencil_alpha_state(info->ctx,
168 &depthStencilAlpha);
169 info->ctx->bind_depth_stencil_alpha_state(info->ctx, handle);
170 }
171
172 {
173 struct pipe_rasterizer_state rasterizer;
174 void *handle;
175 memset(&rasterizer, 0, sizeof rasterizer);
176 rasterizer.cull_face = PIPE_FACE_NONE;
177 rasterizer.half_pixel_center = 1;
178 rasterizer.bottom_edge_rule = 1;
179 handle = info->ctx->create_rasterizer_state(info->ctx, &rasterizer);
180 info->ctx->bind_rasterizer_state(info->ctx, handle);
181 }
182 }
183
184
185 static inline void
186 graw_util_viewport(struct graw_info *info,
187 float x, float y,
188 float width, float height,
189 float zNear, float zFar)
190 {
191 float z = zNear;
192 float half_width = width / 2.0f;
193 float half_height = height / 2.0f;
194 float half_depth = (zFar - zNear) / 2.0f;
195 struct pipe_viewport_state vp;
196
197 vp.scale[0] = half_width;
198 vp.scale[1] = half_height;
199 vp.scale[2] = half_depth;
200
201 vp.translate[0] = half_width + x;
202 vp.translate[1] = half_height + y;
203 vp.translate[2] = half_depth + z;
204
205 info->ctx->set_viewport_states(info->ctx, 0, 1, &vp);
206 }
207
208
209 static inline void
210 graw_util_flush_front(const struct graw_info *info)
211 {
212 info->screen->flush_frontbuffer(info->screen, info->color_buf[0],
213 0, 0, info->window, NULL);
214 }
215
216
217 static inline struct pipe_resource *
218 graw_util_create_tex2d(const struct graw_info *info,
219 int width, int height, enum pipe_format format,
220 const void *data)
221 {
222 const int row_stride = width * util_format_get_blocksize(format);
223 const int image_bytes = row_stride * height;
224 struct pipe_resource temp, *tex;
225 struct pipe_box box;
226
227 temp.target = PIPE_TEXTURE_2D;
228 temp.format = format;
229 temp.width0 = width;
230 temp.height0 = height;
231 temp.depth0 = 1;
232 temp.last_level = 0;
233 temp.array_size = 1;
234 temp.nr_samples = 1;
235 temp.bind = PIPE_BIND_SAMPLER_VIEW;
236
237 tex = info->screen->resource_create(info->screen, &temp);
238 if (!tex) {
239 debug_printf("graw: failed to create texture\n");
240 return NULL;
241 }
242
243 u_box_2d(0, 0, width, height, &box);
244
245 info->ctx->texture_subdata(info->ctx,
246 tex,
247 0,
248 PIPE_TRANSFER_WRITE,
249 &box,
250 data,
251 row_stride,
252 image_bytes);
253
254 /* Possibly read back & compare against original data:
255 */
256 #if 0
257 {
258 struct pipe_transfer *t;
259 uint32_t *ptr;
260 t = pipe_transfer_map(info->ctx, samptex,
261 0, 0, /* level, layer */
262 PIPE_TRANSFER_READ,
263 0, 0, SIZE, SIZE); /* x, y, width, height */
264
265 ptr = info->ctx->transfer_map(info->ctx, t);
266
267 if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
268 assert(0);
269 exit(9);
270 }
271
272 info->ctx->transfer_unmap(info->ctx, t);
273
274 info->ctx->transfer_destroy(info->ctx, t);
275 }
276 #endif
277
278 return tex;
279 }
280
281
282 static inline void *
283 graw_util_create_simple_sampler(const struct graw_info *info,
284 unsigned wrap_mode,
285 unsigned img_filter)
286 {
287 struct pipe_sampler_state sampler_desc;
288 void *sampler;
289
290 memset(&sampler_desc, 0, sizeof sampler_desc);
291 sampler_desc.wrap_s =
292 sampler_desc.wrap_t =
293 sampler_desc.wrap_r = wrap_mode;
294 sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
295 sampler_desc.min_img_filter =
296 sampler_desc.mag_img_filter = img_filter;
297 sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
298 sampler_desc.compare_func = 0;
299 sampler_desc.normalized_coords = 1;
300 sampler_desc.max_anisotropy = 0;
301
302 sampler = info->ctx->create_sampler_state(info->ctx, &sampler_desc);
303
304 return sampler;
305 }
306
307
308 static inline struct pipe_sampler_view *
309 graw_util_create_simple_sampler_view(const struct graw_info *info,
310 struct pipe_resource *texture)
311 {
312 struct pipe_sampler_view sv_temp;
313 struct pipe_sampler_view *sv;
314
315 memset(&sv_temp, 0, sizeof(sv_temp));
316 sv_temp.format = texture->format;
317 sv_temp.texture = texture;
318 sv_temp.swizzle_r = PIPE_SWIZZLE_X;
319 sv_temp.swizzle_g = PIPE_SWIZZLE_Y;
320 sv_temp.swizzle_b = PIPE_SWIZZLE_Z;
321 sv_temp.swizzle_a = PIPE_SWIZZLE_W;
322
323 sv = info->ctx->create_sampler_view(info->ctx, texture, &sv_temp);
324
325 return sv;
326 }
327