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