aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/tests/graw/graw_util.h
blob: 5d72bb9d4a7cb653456d99f12b5a859b17f5f07b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327

#include "state_tracker/graw.h"

#include "pipe/p_context.h"
#include "pipe/p_defines.h"
#include "pipe/p_screen.h"
#include "pipe/p_shader_tokens.h"
#include "pipe/p_state.h"

#include "util/u_box.h"    
#include "util/u_debug.h"
#include "util/u_debug_image.h"
#include "util/u_draw_quad.h"
#include "util/u_format.h"
#include "util/u_inlines.h"
#include "util/u_memory.h"


struct graw_info
{
   struct pipe_screen *screen;
   struct pipe_context *ctx;
   struct pipe_resource *color_buf[PIPE_MAX_COLOR_BUFS], *zs_buf;
   struct pipe_surface *color_surf[PIPE_MAX_COLOR_BUFS], *zs_surf;
   void *window;
};



static inline boolean
graw_util_create_window(struct graw_info *info,
                        int width, int height,
                        int num_cbufs, bool zstencil_buf)
{
   static const enum pipe_format formats[] = {
      PIPE_FORMAT_RGBA8888_UNORM,
      PIPE_FORMAT_BGRA8888_UNORM,
      PIPE_FORMAT_NONE
   };
   enum pipe_format format;
   struct pipe_resource resource_temp;
   struct pipe_surface surface_temp;
   int i;

   memset(info, 0, sizeof(*info));

   /* It's hard to say whether window or screen should be created
    * first.  Different environments would prefer one or the other.
    *
    * Also, no easy way of querying supported formats if the screen
    * cannot be created first.
    */
   for (i = 0; info->window == NULL && formats[i] != PIPE_FORMAT_NONE; i++) {
      info->screen = graw_create_window_and_screen(0, 0, width, height,
                                                   formats[i],
                                                   &info->window);
      format = formats[i];
   }
   if (!info->screen || !info->window) {
      debug_printf("graw: Failed to create screen/window\n");
      return FALSE;
   }
   
   info->ctx = info->screen->context_create(info->screen, NULL, 0);
   if (info->ctx == NULL) {
      debug_printf("graw: Failed to create context\n");
      return FALSE;
   }

   for (i = 0; i < num_cbufs; i++) {
      /* create color texture */
      resource_temp.target = PIPE_TEXTURE_2D;
      resource_temp.format = format;
      resource_temp.width0 = width;
      resource_temp.height0 = height;
      resource_temp.depth0 = 1;
      resource_temp.array_size = 1;
      resource_temp.last_level = 0;
      resource_temp.nr_samples = 1;
      resource_temp.bind = (PIPE_BIND_RENDER_TARGET |
                            PIPE_BIND_DISPLAY_TARGET);
      info->color_buf[i] = info->screen->resource_create(info->screen,
                                                         &resource_temp);
      if (info->color_buf[i] == NULL) {
         debug_printf("graw: Failed to create color texture\n");
         return FALSE;
      }

      /* create color surface */
      surface_temp.format = resource_temp.format;
      surface_temp.u.tex.level = 0;
      surface_temp.u.tex.first_layer = 0;
      surface_temp.u.tex.last_layer = 0;
      info->color_surf[i] = info->ctx->create_surface(info->ctx,
                                                      info->color_buf[i],
                                                      &surface_temp);
      if (info->color_surf[i] == NULL) {
         debug_printf("graw: Failed to get color surface\n");
         return FALSE;
      }
   }

   /* create Z texture (XXX try other Z/S formats if needed) */
   resource_temp.target = PIPE_TEXTURE_2D;
   resource_temp.format = PIPE_FORMAT_S8_UINT_Z24_UNORM;
   resource_temp.width0 = width;
   resource_temp.height0 = height;
   resource_temp.depth0 = 1;
   resource_temp.array_size = 1;
   resource_temp.last_level = 0;
   resource_temp.nr_samples = 1;
   resource_temp.bind = PIPE_BIND_DEPTH_STENCIL;
   info->zs_buf = info->screen->resource_create(info->screen, &resource_temp);
   if (!info->zs_buf) {
      debug_printf("graw: Failed to create Z texture\n");
      return FALSE;
   }

   /* create z surface */
   surface_temp.format = resource_temp.format;
   surface_temp.u.tex.level = 0;
   surface_temp.u.tex.first_layer = 0;
   surface_temp.u.tex.last_layer = 0;
   info->zs_surf = info->ctx->create_surface(info->ctx,
                                             info->zs_buf,
                                             &surface_temp);
   if (info->zs_surf == NULL) {
      debug_printf("graw: Failed to get Z surface\n");
      return FALSE;
   }

   {
      struct pipe_framebuffer_state fb;
      memset(&fb, 0, sizeof fb);
      fb.nr_cbufs = num_cbufs;
      fb.width = width;
      fb.height = height;
      for (i = 0; i < num_cbufs; i++)
         fb.cbufs[i] = info->color_surf[i];
      fb.zsbuf = info->zs_surf;
      info->ctx->set_framebuffer_state(info->ctx, &fb);
   }

   return TRUE;
}


static inline void
graw_util_default_state(struct graw_info *info, boolean depth_test)
{
   {
      struct pipe_blend_state blend;
      void *handle;
      memset(&blend, 0, sizeof blend);
      blend.rt[0].colormask = PIPE_MASK_RGBA;
      handle = info->ctx->create_blend_state(info->ctx, &blend);
      info->ctx->bind_blend_state(info->ctx, handle);
   }

   {
      struct pipe_depth_stencil_alpha_state depthStencilAlpha;
      void *handle;
      memset(&depthStencilAlpha, 0, sizeof depthStencilAlpha);
      depthStencilAlpha.depth.enabled = depth_test;
      depthStencilAlpha.depth.writemask = 1;
      depthStencilAlpha.depth.func = PIPE_FUNC_LESS;
      handle = info->ctx->create_depth_stencil_alpha_state(info->ctx,
                                                           &depthStencilAlpha);
      info->ctx->bind_depth_stencil_alpha_state(info->ctx, handle);
   }

   {
      struct pipe_rasterizer_state rasterizer;
      void *handle;
      memset(&rasterizer, 0, sizeof rasterizer);
      rasterizer.cull_face = PIPE_FACE_NONE;
      rasterizer.half_pixel_center = 1;
      rasterizer.bottom_edge_rule = 1;
      handle = info->ctx->create_rasterizer_state(info->ctx, &rasterizer);
      info->ctx->bind_rasterizer_state(info->ctx, handle);
   }
}


static inline void
graw_util_viewport(struct graw_info *info,
                   float x, float y,
                   float width, float height,
                   float zNear, float zFar)
{
   float z = zNear;
   float half_width = width / 2.0f;
   float half_height = height / 2.0f;
   float half_depth = (zFar - zNear) / 2.0f;
   struct pipe_viewport_state vp;

   vp.scale[0] = half_width;
   vp.scale[1] = half_height;
   vp.scale[2] = half_depth;

   vp.translate[0] = half_width + x;
   vp.translate[1] = half_height + y;
   vp.translate[2] = half_depth + z;

   info->ctx->set_viewport_states(info->ctx, 0, 1, &vp);
}


static inline void
graw_util_flush_front(const struct graw_info *info)
{
   info->screen->flush_frontbuffer(info->screen, info->color_buf[0],
                                   0, 0, info->window, NULL);
}


static inline struct pipe_resource *
graw_util_create_tex2d(const struct graw_info *info,
                       int width, int height, enum pipe_format format,
                       const void *data)
{ 
   const int row_stride = width * util_format_get_blocksize(format);
   const int image_bytes = row_stride * height;
   struct pipe_resource temp, *tex;
   struct pipe_box box;

   temp.target = PIPE_TEXTURE_2D;
   temp.format = format;
   temp.width0 = width;
   temp.height0 = height;
   temp.depth0 = 1;
   temp.last_level = 0;
   temp.array_size = 1;
   temp.nr_samples = 1;
   temp.bind = PIPE_BIND_SAMPLER_VIEW;
   
   tex = info->screen->resource_create(info->screen, &temp);
   if (!tex) {
      debug_printf("graw: failed to create texture\n");
      return NULL;
   }

   u_box_2d(0, 0, width, height, &box);

   info->ctx->transfer_inline_write(info->ctx,
                                    tex,
                                    0,
                                    PIPE_TRANSFER_WRITE,
                                    &box,
                                    data,
                                    row_stride,
                                    image_bytes);

   /* Possibly read back & compare against original data:
    */
#if 0
   {
      struct pipe_transfer *t;
      uint32_t *ptr;
      t = pipe_transfer_map(info->ctx, samptex,
                            0, 0, /* level, layer */
                            PIPE_TRANSFER_READ,
                            0, 0, SIZE, SIZE); /* x, y, width, height */

      ptr = info->ctx->transfer_map(info->ctx, t);

      if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
         assert(0);
         exit(9);
      }

      info->ctx->transfer_unmap(info->ctx, t);

      info->ctx->transfer_destroy(info->ctx, t);
   }
#endif

   return tex;
}


static inline void *
graw_util_create_simple_sampler(const struct graw_info *info,
                                unsigned wrap_mode,
                                unsigned img_filter)
{
   struct pipe_sampler_state sampler_desc;
   void *sampler;

   memset(&sampler_desc, 0, sizeof sampler_desc);
   sampler_desc.wrap_s =
   sampler_desc.wrap_t =
   sampler_desc.wrap_r = wrap_mode;
   sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
   sampler_desc.min_img_filter =
   sampler_desc.mag_img_filter = img_filter;
   sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
   sampler_desc.compare_func = 0;
   sampler_desc.normalized_coords = 1;
   sampler_desc.max_anisotropy = 0;
   
   sampler = info->ctx->create_sampler_state(info->ctx, &sampler_desc);

   return sampler;
}


static inline struct pipe_sampler_view *
graw_util_create_simple_sampler_view(const struct graw_info *info,
                                     struct pipe_resource *texture)
{
   struct pipe_sampler_view sv_temp;
   struct pipe_sampler_view *sv;

   memset(&sv_temp, 0, sizeof(sv_temp));
   sv_temp.format = texture->format;
   sv_temp.texture = texture;
   sv_temp.swizzle_r = PIPE_SWIZZLE_X;
   sv_temp.swizzle_g = PIPE_SWIZZLE_Y;
   sv_temp.swizzle_b = PIPE_SWIZZLE_Z;
   sv_temp.swizzle_a = PIPE_SWIZZLE_W;

   sv = info->ctx->create_sampler_view(info->ctx, texture, &sv_temp);

   return sv;
}