tri.c revision d851dae919f77180ab70035ad621ea0424162c43
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_state.h"
9#include "pipe/p_defines.h"
10
11#include "util/u_debug.h"       /* debug_dump_surface_bmp() */
12#include "util/u_memory.h"      /* Offset() */
13#include "util/u_draw_quad.h"
14
15enum pipe_format formats[] = {
16   PIPE_FORMAT_R8G8B8A8_UNORM,
17   PIPE_FORMAT_B8G8R8A8_UNORM,
18   PIPE_FORMAT_NONE
19};
20
21static const int WIDTH = 300;
22static const int HEIGHT = 300;
23
24static struct pipe_screen *screen = NULL;
25static struct pipe_context *ctx = NULL;
26static struct pipe_surface *surf = NULL;
27static void *window = NULL;
28
29struct vertex {
30   float position[4];
31   float color[4];
32};
33
34static struct vertex vertices[3] =
35{
36   {
37      { 0.0f, -0.9f, 0.0f, 1.0f },
38      { 1.0f, 0.0f, 0.0f, 1.0f }
39   },
40   {
41      { -0.9f, 0.9f, 0.0f, 1.0f },
42      { 0.0f, 1.0f, 0.0f, 1.0f }
43   },
44   {
45      { 0.9f, 0.9f, 0.0f, 1.0f },
46      { 0.0f, 0.0f, 1.0f, 1.0f }
47   }
48};
49
50
51
52
53static void set_viewport( float x, float y,
54                          float width, float height,
55                          float near, float far)
56{
57   float z = far;
58   float half_width = (float)width / 2.0f;
59   float half_height = (float)height / 2.0f;
60   float half_depth = ((float)far - (float)near) / 2.0f;
61   struct pipe_viewport_state vp;
62
63   vp.scale[0] = half_width;
64   vp.scale[1] = half_height;
65   vp.scale[2] = half_depth;
66   vp.scale[3] = 1.0f;
67
68   vp.translate[0] = half_width + x;
69   vp.translate[1] = half_height + y;
70   vp.translate[2] = half_depth + z;
71   vp.translate[3] = 0.0f;
72
73   ctx->set_viewport_state( ctx, &vp );
74}
75
76static void set_vertices( void )
77{
78   struct pipe_vertex_element ve[2];
79   struct pipe_vertex_buffer vbuf;
80   void *handle;
81
82   memset(ve, 0, sizeof ve);
83
84   ve[0].src_offset = Offset(struct vertex, position);
85   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
86   ve[1].src_offset = Offset(struct vertex, color);
87   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
88
89   handle = ctx->create_vertex_elements_state(ctx, 2, ve);
90   ctx->bind_vertex_elements_state(ctx, handle);
91
92
93   vbuf.stride = sizeof( struct vertex );
94   vbuf.max_index = sizeof(vertices) / vbuf.stride;
95   vbuf.buffer_offset = 0;
96   vbuf.buffer = screen->user_buffer_create(screen,
97                                            vertices,
98                                            sizeof(vertices),
99                                            PIPE_BIND_VERTEX_BUFFER);
100
101   ctx->set_vertex_buffers(ctx, 1, &vbuf);
102}
103
104static void set_vertex_shader( void )
105{
106   void *handle;
107   const char *text =
108      "VERT\n"
109      "DCL IN[0]\n"
110      "DCL IN[1]\n"
111      "DCL OUT[0], POSITION\n"
112      "DCL OUT[1], COLOR\n"
113      "  0: MOV OUT[1], IN[1]\n"
114      "  1: MOV OUT[0], IN[0]\n"
115      "  2: END\n";
116
117   handle = graw_parse_vertex_shader(ctx, text);
118   ctx->bind_vs_state(ctx, handle);
119}
120
121static void set_fragment_shader( void )
122{
123   void *handle;
124   const char *text =
125      "FRAG\n"
126      "DCL IN[0], COLOR, LINEAR\n"
127      "DCL OUT[0], COLOR\n"
128      "  0: MOV OUT[0], IN[0]\n"
129      "  1: END\n";
130
131   handle = graw_parse_fragment_shader(ctx, text);
132   ctx->bind_fs_state(ctx, handle);
133}
134
135
136static void draw( void )
137{
138   float clear_color[4] = {1,0,1,1};
139
140   ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
141   util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
142   ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
143
144#if 0
145   /* At the moment, libgraw leaks out/makes available some of the
146    * symbols from gallium/auxiliary, including these debug helpers.
147    * Will eventually want to bless some of these paths, and lock the
148    * others down so they aren't accessible from test programs.
149    *
150    * This currently just happens to work on debug builds - a release
151    * build will probably fail to link here:
152    */
153   debug_dump_surface_bmp(ctx, "result.bmp", surf);
154#endif
155
156   screen->flush_frontbuffer(screen, surf, window);
157}
158
159
160static void init( void )
161{
162   struct pipe_framebuffer_state fb;
163   struct pipe_resource *tex, templat;
164   int i;
165
166   /* It's hard to say whether window or screen should be created
167    * first.  Different environments would prefer one or the other.
168    *
169    * Also, no easy way of querying supported formats if the screen
170    * cannot be created first.
171    */
172   for (i = 0;
173        window == NULL && formats[i] != PIPE_FORMAT_NONE;
174        i++) {
175
176      screen = graw_create_window_and_screen(0,0,300,300,
177                                             formats[i],
178                                             &window);
179   }
180
181   ctx = screen->context_create(screen, NULL);
182   if (ctx == NULL)
183      exit(3);
184
185   templat.target = PIPE_TEXTURE_2D;
186   templat.format = formats[i];
187   templat.width0 = WIDTH;
188   templat.height0 = HEIGHT;
189   templat.depth0 = 1;
190   templat.last_level = 0;
191   templat.nr_samples = 1;
192   templat.bind = (PIPE_BIND_RENDER_TARGET |
193                   PIPE_BIND_DISPLAY_TARGET);
194
195   tex = screen->resource_create(screen,
196                                 &templat);
197   if (tex == NULL)
198      exit(4);
199
200   surf = screen->get_tex_surface(screen, tex, 0, 0, 0,
201                                  PIPE_BIND_RENDER_TARGET |
202                                  PIPE_BIND_DISPLAY_TARGET);
203   if (surf == NULL)
204      exit(5);
205
206   memset(&fb, 0, sizeof fb);
207   fb.nr_cbufs = 1;
208   fb.width = WIDTH;
209   fb.height = HEIGHT;
210   fb.cbufs[0] = surf;
211
212   ctx->set_framebuffer_state(ctx, &fb);
213
214   {
215      struct pipe_blend_state blend;
216      void *handle;
217      memset(&blend, 0, sizeof blend);
218      blend.rt[0].colormask = PIPE_MASK_RGBA;
219      handle = ctx->create_blend_state(ctx, &blend);
220      ctx->bind_blend_state(ctx, handle);
221   }
222
223   {
224      struct pipe_depth_stencil_alpha_state depthstencil;
225      void *handle;
226      memset(&depthstencil, 0, sizeof depthstencil);
227      handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
228      ctx->bind_depth_stencil_alpha_state(ctx, handle);
229   }
230
231   {
232      struct pipe_rasterizer_state rasterizer;
233      void *handle;
234      memset(&rasterizer, 0, sizeof rasterizer);
235      rasterizer.cull_face = PIPE_FACE_NONE;
236      rasterizer.gl_rasterization_rules = 1;
237      handle = ctx->create_rasterizer_state(ctx, &rasterizer);
238      ctx->bind_rasterizer_state(ctx, handle);
239   }
240
241   set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
242   set_vertices();
243   set_vertex_shader();
244   set_fragment_shader();
245}
246
247
248int main( int argc, char *argv[] )
249{
250   init();
251
252   graw_set_display_func( draw );
253   graw_main_loop();
254   return 0;
255}
256