quad-sample.c revision b8e80941
1/* Display a cleared blue window.  This demo has no dependencies on
2 * any utility code, just the graw interface and gallium.
3 */
4
5#include "state_tracker/graw.h"
6#include "pipe/p_screen.h"
7#include "pipe/p_context.h"
8#include "pipe/p_shader_tokens.h"
9#include "pipe/p_state.h"
10#include "pipe/p_defines.h"
11
12#include "util/u_debug.h"       /* debug_dump_surface_bmp() */
13#include "util/u_inlines.h"
14#include "util/u_memory.h"      /* Offset() */
15#include "util/u_draw_quad.h"
16#include "util/u_box.h"
17
18#include <stdio.h>
19
20enum pipe_format formats[] = {
21   PIPE_FORMAT_RGBA8888_UNORM,
22   PIPE_FORMAT_BGRA8888_UNORM,
23   PIPE_FORMAT_NONE
24};
25
26static const int WIDTH = 300;
27static const int HEIGHT = 300;
28
29static struct pipe_screen *screen = NULL;
30static struct pipe_context *ctx = NULL;
31static struct pipe_resource *rttex = NULL;
32static struct pipe_resource *samptex = NULL;
33static struct pipe_surface *surf = NULL;
34static struct pipe_sampler_view *sv = NULL;
35static void *sampler = NULL;
36static void *window = NULL;
37
38struct vertex {
39   float position[4];
40   float color[4];
41};
42
43static struct vertex vertices[] =
44{
45   { { 0.9, -0.9, 0.0, 1.0 },
46     { 1, 0, 0, 1 } },
47
48   { { 0.9,  0.9, 0.0, 1.0 },
49     { 1, 1, 0, 1 } },
50
51   { {-0.9,  0.9, 0.0, 1.0 },
52     { 0, 1, 0, 1 } },
53
54   { {-0.9,  -0.9, 0.0, 1.0 },
55     { 0, 0, 0, 1 } },
56};
57
58
59
60
61static void set_viewport( float x, float y,
62                          float width, float height,
63                          float zNear, float zFar)
64{
65   float z = zFar;
66   float half_width = (float)width / 2.0f;
67   float half_height = (float)height / 2.0f;
68   float half_depth = ((float)zFar - (float)zNear) / 2.0f;
69   struct pipe_viewport_state vp;
70
71   vp.scale[0] = half_width;
72   vp.scale[1] = half_height;
73   vp.scale[2] = half_depth;
74
75   vp.translate[0] = half_width + x;
76   vp.translate[1] = half_height + y;
77   vp.translate[2] = half_depth + z;
78
79   ctx->set_viewport_states( ctx, 0, 1, &vp );
80}
81
82static void set_vertices( void )
83{
84   struct pipe_vertex_element ve[2];
85   struct pipe_vertex_buffer vbuf;
86   void *handle;
87
88   memset(ve, 0, sizeof ve);
89
90   ve[0].src_offset = Offset(struct vertex, position);
91   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
92   ve[1].src_offset = Offset(struct vertex, color);
93   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
94
95   handle = ctx->create_vertex_elements_state(ctx, 2, ve);
96   ctx->bind_vertex_elements_state(ctx, handle);
97
98   memset(&vbuf, 0, sizeof vbuf);
99
100   vbuf.stride = sizeof( struct vertex );
101   vbuf.buffer_offset = 0;
102   vbuf.buffer.resource = pipe_buffer_create_with_data(ctx,
103                                              PIPE_BIND_VERTEX_BUFFER,
104                                              PIPE_USAGE_DEFAULT,
105                                              sizeof(vertices),
106                                              vertices);
107
108   ctx->set_vertex_buffers(ctx, 0, 1, &vbuf);
109}
110
111static void set_vertex_shader( void )
112{
113   void *handle;
114   const char *text =
115      "VERT\n"
116      "DCL IN[0]\n"
117      "DCL IN[1]\n"
118      "DCL OUT[0], POSITION\n"
119      "DCL OUT[1], GENERIC[0]\n"
120      "  0: MOV OUT[1], IN[1]\n"
121      "  1: MOV OUT[0], IN[0]\n"
122      "  2: END\n";
123
124   handle = graw_parse_vertex_shader(ctx, text);
125   ctx->bind_vs_state(ctx, handle);
126}
127
128static void set_fragment_shader( void )
129{
130   void *handle;
131   const char *text =
132      "FRAG\n"
133      "DCL IN[0], GENERIC[0], PERSPECTIVE\n"
134      "DCL OUT[0], COLOR\n"
135      "DCL TEMP[0]\n"
136      "DCL SAMP[0]\n"
137      "DCL RES[0], 2D, FLOAT\n"
138      "  0: SAMPLE TEMP[0], IN[0], RES[0], SAMP[0]\n"
139      "  1: MOV OUT[0], TEMP[0]\n"
140      "  2: END\n";
141
142   handle = graw_parse_fragment_shader(ctx, text);
143   ctx->bind_fs_state(ctx, handle);
144}
145
146
147static void draw( void )
148{
149   union pipe_color_union clear_color = { {.5,.5,.5,1} };
150
151   ctx->clear(ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0);
152   util_draw_arrays(ctx, PIPE_PRIM_QUADS, 0, 4);
153   ctx->flush(ctx, NULL, 0);
154
155   graw_save_surface_to_file(ctx, surf, NULL);
156
157   screen->flush_frontbuffer(screen, rttex, 0, 0, window, NULL);
158}
159
160#define SIZE 16
161
162static void init_tex( void )
163{
164   struct pipe_sampler_view sv_template;
165   struct pipe_sampler_state sampler_desc;
166   struct pipe_resource templat;
167   struct pipe_box box;
168   ubyte tex2d[SIZE][SIZE][4];
169   int s, t;
170
171#if (SIZE != 2)
172   for (s = 0; s < SIZE; s++) {
173      for (t = 0; t < SIZE; t++) {
174         if (0) {
175            int x = (s ^ t) & 1;
176	    tex2d[t][s][0] = (x) ? 0 : 63;
177	    tex2d[t][s][1] = (x) ? 0 : 128;
178	    tex2d[t][s][2] = 0;
179	    tex2d[t][s][3] = 0xff;
180         }
181         else {
182            int x = ((s ^ t) >> 2) & 1;
183	    tex2d[t][s][0] = s*255/(SIZE-1);
184	    tex2d[t][s][1] = t*255/(SIZE-1);
185	    tex2d[t][s][2] = (x) ? 0 : 128;
186	    tex2d[t][s][3] = 0xff;
187         }
188      }
189   }
190#else
191   tex2d[0][0][0] = 0;
192   tex2d[0][0][1] = 255;
193   tex2d[0][0][2] = 255;
194   tex2d[0][0][3] = 0;
195
196   tex2d[0][1][0] = 0;
197   tex2d[0][1][1] = 0;
198   tex2d[0][1][2] = 255;
199   tex2d[0][1][3] = 255;
200
201   tex2d[1][0][0] = 255;
202   tex2d[1][0][1] = 255;
203   tex2d[1][0][2] = 0;
204   tex2d[1][0][3] = 255;
205
206   tex2d[1][1][0] = 255;
207   tex2d[1][1][1] = 0;
208   tex2d[1][1][2] = 0;
209   tex2d[1][1][3] = 255;
210#endif
211
212   memset(&templat, 0, sizeof(templat));
213   templat.target = PIPE_TEXTURE_2D;
214   templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
215   templat.width0 = SIZE;
216   templat.height0 = SIZE;
217   templat.depth0 = 1;
218   templat.last_level = 0;
219   templat.bind = PIPE_BIND_SAMPLER_VIEW;
220
221
222   samptex = screen->resource_create(screen,
223                                 &templat);
224   if (samptex == NULL)
225      exit(4);
226
227   u_box_2d(0,0,SIZE,SIZE, &box);
228
229   ctx->texture_subdata(ctx,
230                        samptex,
231                        0,
232                        PIPE_TRANSFER_WRITE,
233                        &box,
234                        tex2d,
235                        sizeof tex2d[0],
236                        sizeof tex2d);
237
238   /* Possibly read back & compare against original data:
239    */
240   if (0)
241   {
242      struct pipe_transfer *t;
243      uint32_t *ptr;
244      ptr = pipe_transfer_map(ctx, samptex,
245                              0, 0, /* level, layer */
246                              PIPE_TRANSFER_READ,
247                              0, 0, SIZE, SIZE, &t); /* x, y, width, height */
248
249      if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
250         assert(0);
251         exit(9);
252      }
253
254      ctx->transfer_unmap(ctx, t);
255   }
256
257   memset(&sv_template, 0, sizeof sv_template);
258   sv_template.format = samptex->format;
259   sv_template.texture = samptex;
260   sv_template.swizzle_r = 0;
261   sv_template.swizzle_g = 1;
262   sv_template.swizzle_b = 2;
263   sv_template.swizzle_a = 3;
264   sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
265   if (sv == NULL)
266      exit(5);
267
268   ctx->set_sampler_views(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sv);
269
270
271   memset(&sampler_desc, 0, sizeof sampler_desc);
272   sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
273   sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
274   sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
275   sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
276   sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
277   sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
278   sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
279   sampler_desc.compare_func = 0;
280   sampler_desc.normalized_coords = 1;
281   sampler_desc.max_anisotropy = 0;
282
283   sampler = ctx->create_sampler_state(ctx, &sampler_desc);
284   if (sampler == NULL)
285      exit(6);
286
287   ctx->bind_sampler_states(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sampler);
288
289}
290
291static void init( void )
292{
293   struct pipe_framebuffer_state fb;
294   struct pipe_resource templat;
295   struct pipe_surface surf_tmpl;
296   int i;
297
298   /* It's hard to say whether window or screen should be created
299    * first.  Different environments would prefer one or the other.
300    *
301    * Also, no easy way of querying supported formats if the screen
302    * cannot be created first.
303    */
304   for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
305      screen = graw_create_window_and_screen(0, 0, 300, 300,
306                                             formats[i],
307                                             &window);
308      if (window && screen)
309         break;
310   }
311   if (!screen || !window) {
312      fprintf(stderr, "Unable to create window\n");
313      exit(1);
314   }
315
316   ctx = screen->context_create(screen, NULL, 0);
317   if (ctx == NULL)
318      exit(3);
319
320   memset(&templat, 0, sizeof(templat));
321   templat.target = PIPE_TEXTURE_2D;
322   templat.format = formats[i];
323   templat.width0 = WIDTH;
324   templat.height0 = HEIGHT;
325   templat.depth0 = 1;
326   templat.array_size = 1;
327   templat.last_level = 0;
328   templat.bind = (PIPE_BIND_RENDER_TARGET |
329                   PIPE_BIND_DISPLAY_TARGET);
330
331   rttex = screen->resource_create(screen,
332                                 &templat);
333   if (rttex == NULL)
334      exit(4);
335
336   surf_tmpl.format = templat.format;
337   surf_tmpl.u.tex.level = 0;
338   surf_tmpl.u.tex.first_layer = 0;
339   surf_tmpl.u.tex.last_layer = 0;
340   surf = ctx->create_surface(ctx, rttex, &surf_tmpl);
341   if (surf == NULL)
342      exit(5);
343
344   memset(&fb, 0, sizeof fb);
345   fb.nr_cbufs = 1;
346   fb.width = WIDTH;
347   fb.height = HEIGHT;
348   fb.cbufs[0] = surf;
349
350   ctx->set_framebuffer_state(ctx, &fb);
351
352   {
353      struct pipe_blend_state blend;
354      void *handle;
355      memset(&blend, 0, sizeof blend);
356      blend.rt[0].colormask = PIPE_MASK_RGBA;
357      handle = ctx->create_blend_state(ctx, &blend);
358      ctx->bind_blend_state(ctx, handle);
359   }
360
361   {
362      struct pipe_depth_stencil_alpha_state depthstencil;
363      void *handle;
364      memset(&depthstencil, 0, sizeof depthstencil);
365      handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
366      ctx->bind_depth_stencil_alpha_state(ctx, handle);
367   }
368
369   {
370      struct pipe_rasterizer_state rasterizer;
371      void *handle;
372      memset(&rasterizer, 0, sizeof rasterizer);
373      rasterizer.cull_face = PIPE_FACE_NONE;
374      rasterizer.half_pixel_center = 1;
375      rasterizer.bottom_edge_rule = 1;
376      rasterizer.depth_clip_near = 1;
377      rasterizer.depth_clip_far = 1;
378      handle = ctx->create_rasterizer_state(ctx, &rasterizer);
379      ctx->bind_rasterizer_state(ctx, handle);
380   }
381
382   set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
383
384   init_tex();
385
386   set_vertices();
387   set_vertex_shader();
388   set_fragment_shader();
389}
390
391static void args(int argc, char *argv[])
392{
393   int i;
394
395   for (i = 1; i < argc;) {
396      if (graw_parse_args(&i, argc, argv)) {
397         continue;
398      }
399      exit(1);
400   }
401}
402
403
404int main( int argc, char *argv[] )
405{
406   args(argc, argv);
407   init();
408
409   graw_set_display_func( draw );
410   graw_main_loop();
411   return 0;
412}
413