1 /* Display a cleared blue window. This demo has no dependencies on
2 * any utility code, just the graw interface and gallium.
7 #include "state_tracker/graw.h"
8 #include "pipe/p_screen.h"
9 #include "pipe/p_context.h"
10 #include "pipe/p_state.h"
11 #include "pipe/p_defines.h"
13 #include "util/u_memory.h" /* Offset() */
14 #include "util/u_draw_quad.h"
16 enum pipe_format formats[] = {
17 PIPE_FORMAT_R8G8B8A8_UNORM,
18 PIPE_FORMAT_B8G8R8A8_UNORM,
22 static const int WIDTH = 300;
23 static const int HEIGHT = 300;
25 static struct pipe_screen *screen = NULL;
26 static struct pipe_context *ctx = NULL;
27 static struct pipe_surface *surf = NULL;
28 static struct pipe_resource *tex = NULL;
29 static void *window = NULL;
36 static struct vertex vertices[3] =
39 { 0.0f, -0.9f, 0.0f, 1.0f },
40 { 1.0f, 0.0f, 0.0f, 1.0f }
43 { -0.9f, 0.9f, 0.0f, 1.0f },
44 { 0.0f, 1.0f, 0.0f, 1.0f }
47 { 0.9f, 0.9f, 0.0f, 1.0f },
48 { 0.0f, 0.0f, 1.0f, 1.0f }
55 static void set_viewport( float x, float y,
56 float width, float height,
57 float near, float far)
60 float half_width = (float)width / 2.0f;
61 float half_height = (float)height / 2.0f;
62 float half_depth = ((float)far - (float)near) / 2.0f;
63 struct pipe_viewport_state vp;
65 vp.scale[0] = half_width;
66 vp.scale[1] = half_height;
67 vp.scale[2] = half_depth;
70 vp.translate[0] = half_width + x;
71 vp.translate[1] = half_height + y;
72 vp.translate[2] = half_depth + z;
73 vp.translate[3] = 0.0f;
75 ctx->set_viewport_state( ctx, &vp );
78 static void set_vertices( void )
80 struct pipe_vertex_element ve[2];
81 struct pipe_vertex_buffer vbuf;
84 memset(ve, 0, sizeof ve);
86 ve[0].src_offset = Offset(struct vertex, position);
87 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
88 ve[1].src_offset = Offset(struct vertex, color);
89 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
91 handle = ctx->create_vertex_elements_state(ctx, 2, ve);
92 ctx->bind_vertex_elements_state(ctx, handle);
95 vbuf.stride = sizeof( struct vertex );
96 vbuf.buffer_offset = 0;
97 vbuf.buffer = screen->user_buffer_create(screen,
100 PIPE_BIND_VERTEX_BUFFER);
102 ctx->set_vertex_buffers(ctx, 1, &vbuf);
105 static void set_vertex_shader( void )
112 "DCL OUT[0], POSITION\n"
113 "DCL OUT[1], COLOR\n"
114 " 0: MOV OUT[1], IN[1]\n"
115 " 1: MOV OUT[0], IN[0]\n"
118 handle = graw_parse_vertex_shader(ctx, text);
119 ctx->bind_vs_state(ctx, handle);
122 static void set_fragment_shader( void )
127 "DCL IN[0], COLOR, LINEAR\n"
128 "DCL OUT[0], COLOR\n"
129 " 0: MOV OUT[0], IN[0]\n"
132 handle = graw_parse_fragment_shader(ctx, text);
133 ctx->bind_fs_state(ctx, handle);
137 static void draw( void )
139 float clear_color[4] = {1,0,1,1};
141 ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
142 util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
143 ctx->flush(ctx, NULL);
145 graw_save_surface_to_file(ctx, surf, NULL);
147 screen->flush_frontbuffer(screen, tex, 0, 0, window);
151 static void init( void )
153 struct pipe_framebuffer_state fb;
154 struct pipe_resource templat;
155 struct pipe_surface surf_tmpl;
158 /* It's hard to say whether window or screen should be created
159 * first. Different environments would prefer one or the other.
161 * Also, no easy way of querying supported formats if the screen
162 * cannot be created first.
164 for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
165 screen = graw_create_window_and_screen(0, 0, 300, 300,
168 if (window && screen)
171 if (!screen || !window) {
172 fprintf(stderr, "Unable to create window\n");
176 ctx = screen->context_create(screen, NULL);
178 fprintf(stderr, "Unable to create context!\n");
182 templat.target = PIPE_TEXTURE_2D;
183 templat.format = formats[i];
184 templat.width0 = WIDTH;
185 templat.height0 = HEIGHT;
187 templat.array_size = 1;
188 templat.last_level = 0;
189 templat.nr_samples = 1;
190 templat.bind = (PIPE_BIND_RENDER_TARGET |
191 PIPE_BIND_DISPLAY_TARGET);
193 tex = screen->resource_create(screen,
196 fprintf(stderr, "Unable to create screen texture!\n");
200 surf_tmpl.format = templat.format;
201 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
202 surf_tmpl.u.tex.level = 0;
203 surf_tmpl.u.tex.first_layer = 0;
204 surf_tmpl.u.tex.last_layer = 0;
205 surf = ctx->create_surface(ctx, tex, &surf_tmpl);
207 fprintf(stderr, "Unable to create tex surface!\n");
211 memset(&fb, 0, sizeof fb);
217 ctx->set_framebuffer_state(ctx, &fb);
220 struct pipe_blend_state blend;
222 memset(&blend, 0, sizeof blend);
223 blend.rt[0].colormask = PIPE_MASK_RGBA;
224 handle = ctx->create_blend_state(ctx, &blend);
225 ctx->bind_blend_state(ctx, handle);
229 struct pipe_depth_stencil_alpha_state depthstencil;
231 memset(&depthstencil, 0, sizeof depthstencil);
232 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
233 ctx->bind_depth_stencil_alpha_state(ctx, handle);
237 struct pipe_rasterizer_state rasterizer;
239 memset(&rasterizer, 0, sizeof rasterizer);
240 rasterizer.cull_face = PIPE_FACE_NONE;
241 rasterizer.gl_rasterization_rules = 1;
242 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
243 ctx->bind_rasterizer_state(ctx, handle);
246 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
249 set_fragment_shader();
252 static void args(int argc, char *argv[])
256 for (i = 1; i < argc;) {
257 if (graw_parse_args(&i, argc, argv)) {
264 int main( int argc, char *argv[] )
269 graw_set_display_func( draw );