Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4358 Serge 1
 
2
3
 
4
#include "pipe/p_defines.h"
5
#include "pipe/p_screen.h"
6
#include "pipe/p_shader_tokens.h"
7
#include "pipe/p_state.h"
8
9
 
10
#include "util/u_debug.h"
11
#include "util/u_draw_quad.h"
12
#include "util/u_format.h"
13
#include "util/u_inlines.h"
14
#include "util/u_memory.h"
15
16
 
17
 
18
{
19
   struct pipe_screen *screen;
20
   struct pipe_context *ctx;
21
   struct pipe_resource *color_buf[PIPE_MAX_COLOR_BUFS], *zs_buf;
22
   struct pipe_surface *color_surf[PIPE_MAX_COLOR_BUFS], *zs_surf;
23
   void *window;
24
};
25
26
 
27
 
28
 
29
graw_util_create_window(struct graw_info *info,
30
                        int width, int height,
31
                        int num_cbufs, bool zstencil_buf)
32
{
33
   static const enum pipe_format formats[] = {
34
      PIPE_FORMAT_RGBA8888_UNORM,
35
      PIPE_FORMAT_BGRA8888_UNORM,
36
      PIPE_FORMAT_NONE
37
   };
38
   enum pipe_format format;
39
   struct pipe_resource resource_temp;
40
   struct pipe_surface surface_temp;
41
   int i;
42
43
 
44
45
 
46
    * first.  Different environments would prefer one or the other.
47
    *
48
    * Also, no easy way of querying supported formats if the screen
49
    * cannot be created first.
50
    */
51
   for (i = 0; info->window == NULL && formats[i] != PIPE_FORMAT_NONE; i++) {
52
      info->screen = graw_create_window_and_screen(0, 0, width, height,
53
                                                   formats[i],
54
                                                   &info->window);
55
      format = formats[i];
56
   }
57
   if (!info->screen || !info->window) {
58
      debug_printf("graw: Failed to create screen/window\n");
59
      return FALSE;
60
   }
61
62
 
63
   if (info->ctx == NULL) {
64
      debug_printf("graw: Failed to create context\n");
65
      return FALSE;
66
   }
67
68
 
69
      /* create color texture */
70
      resource_temp.target = PIPE_TEXTURE_2D;
71
      resource_temp.format = format;
72
      resource_temp.width0 = width;
73
      resource_temp.height0 = height;
74
      resource_temp.depth0 = 1;
75
      resource_temp.array_size = 1;
76
      resource_temp.last_level = 0;
77
      resource_temp.nr_samples = 1;
78
      resource_temp.bind = (PIPE_BIND_RENDER_TARGET |
79
                            PIPE_BIND_DISPLAY_TARGET);
80
      info->color_buf[i] = info->screen->resource_create(info->screen,
81
                                                         &resource_temp);
82
      if (info->color_buf[i] == NULL) {
83
         debug_printf("graw: Failed to create color texture\n");
84
         return FALSE;
85
      }
86
87
 
88
      surface_temp.format = resource_temp.format;
89
      surface_temp.u.tex.level = 0;
90
      surface_temp.u.tex.first_layer = 0;
91
      surface_temp.u.tex.last_layer = 0;
92
      info->color_surf[i] = info->ctx->create_surface(info->ctx,
93
                                                      info->color_buf[i],
94
                                                      &surface_temp);
95
      if (info->color_surf[i] == NULL) {
96
         debug_printf("graw: Failed to get color surface\n");
97
         return FALSE;
98
      }
99
   }
100
101
 
102
   resource_temp.target = PIPE_TEXTURE_2D;
103
   resource_temp.format = PIPE_FORMAT_S8_UINT_Z24_UNORM;
104
   resource_temp.width0 = width;
105
   resource_temp.height0 = height;
106
   resource_temp.depth0 = 1;
107
   resource_temp.array_size = 1;
108
   resource_temp.last_level = 0;
109
   resource_temp.nr_samples = 1;
110
   resource_temp.bind = PIPE_BIND_DEPTH_STENCIL;
111
   info->zs_buf = info->screen->resource_create(info->screen, &resource_temp);
112
   if (!info->zs_buf) {
113
      debug_printf("graw: Failed to create Z texture\n");
114
      return FALSE;
115
   }
116
117
 
118
   surface_temp.format = resource_temp.format;
119
   surface_temp.u.tex.level = 0;
120
   surface_temp.u.tex.first_layer = 0;
121
   surface_temp.u.tex.last_layer = 0;
122
   info->zs_surf = info->ctx->create_surface(info->ctx,
123
                                             info->zs_buf,
124
                                             &surface_temp);
125
   if (info->zs_surf == NULL) {
126
      debug_printf("graw: Failed to get Z surface\n");
127
      return FALSE;
128
   }
129
130
 
131
      struct pipe_framebuffer_state fb;
132
      memset(&fb, 0, sizeof fb);
133
      fb.nr_cbufs = num_cbufs;
134
      fb.width = width;
135
      fb.height = height;
136
      for (i = 0; i < num_cbufs; i++)
137
         fb.cbufs[i] = info->color_surf[i];
138
      fb.zsbuf = info->zs_surf;
139
      info->ctx->set_framebuffer_state(info->ctx, &fb);
140
   }
141
142
 
143
}
144
145
 
146
 
147
graw_util_default_state(struct graw_info *info, boolean depth_test)
148
{
149
   {
150
      struct pipe_blend_state blend;
151
      void *handle;
152
      memset(&blend, 0, sizeof blend);
153
      blend.rt[0].colormask = PIPE_MASK_RGBA;
154
      handle = info->ctx->create_blend_state(info->ctx, &blend);
155
      info->ctx->bind_blend_state(info->ctx, handle);
156
   }
157
158
 
159
      struct pipe_depth_stencil_alpha_state depthStencilAlpha;
160
      void *handle;
161
      memset(&depthStencilAlpha, 0, sizeof depthStencilAlpha);
162
      depthStencilAlpha.depth.enabled = depth_test;
163
      depthStencilAlpha.depth.writemask = 1;
164
      depthStencilAlpha.depth.func = PIPE_FUNC_LESS;
165
      handle = info->ctx->create_depth_stencil_alpha_state(info->ctx,
166
                                                           &depthStencilAlpha);
167
      info->ctx->bind_depth_stencil_alpha_state(info->ctx, handle);
168
   }
169
170
 
171
      struct pipe_rasterizer_state rasterizer;
172
      void *handle;
173
      memset(&rasterizer, 0, sizeof rasterizer);
174
      rasterizer.cull_face = PIPE_FACE_NONE;
175
      rasterizer.half_pixel_center = 1;
176
      rasterizer.bottom_edge_rule = 1;
177
      handle = info->ctx->create_rasterizer_state(info->ctx, &rasterizer);
178
      info->ctx->bind_rasterizer_state(info->ctx, handle);
179
   }
180
}
181
182
 
183
 
184
graw_util_viewport(struct graw_info *info,
185
                   float x, float y,
186
                   float width, float height,
187
                   float near, float far)
188
{
189
   float z = near;
190
   float half_width = width / 2.0f;
191
   float half_height = height / 2.0f;
192
   float half_depth = (far - near) / 2.0f;
193
   struct pipe_viewport_state vp;
194
195
 
196
   vp.scale[1] = half_height;
197
   vp.scale[2] = half_depth;
198
   vp.scale[3] = 1.0f;
199
200
 
201
   vp.translate[1] = half_height + y;
202
   vp.translate[2] = half_depth + z;
203
   vp.translate[3] = 0.0f;
204
205
 
206
}
207
208
 
209
 
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);
214
}
215
216
 
217
 
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
 
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
 
238
   if (!tex) {
239
      debug_printf("graw: failed to create texture\n");
240
      return NULL;
241
   }
242
243
 
244
245
 
246
                                    tex,
247
                                    0,
248
                                    PIPE_TRANSFER_WRITE,
249
                                    &box,
250
                                    data,
251
                                    row_stride,
252
                                    image_bytes);
253
254
 
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
 
266
267
 
268
         assert(0);
269
         exit(9);
270
      }
271
272
 
273
274
 
275
   }
276
#endif
277
278
 
279
}
280
281
 
282
 
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
 
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
 
303
304
 
305
}
306
307
 
308
 
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
 
316
   sv_temp.format = texture->format;
317
   sv_temp.texture = texture;
318
   sv_temp.swizzle_r = PIPE_SWIZZLE_RED;
319
   sv_temp.swizzle_g = PIPE_SWIZZLE_GREEN;
320
   sv_temp.swizzle_b = PIPE_SWIZZLE_BLUE;
321
   sv_temp.swizzle_a = PIPE_SWIZZLE_ALPHA;
322
323
 
324
325
 
326
}
327