1 /* Display a cleared blue window. This demo has no dependencies on
2 * any utility code, just the graw interface and gallium.
6 #include "state_tracker/graw.h"
7 #include "pipe/p_screen.h"
8 #include "pipe/p_context.h"
9 #include "pipe/p_state.h"
10 #include "pipe/p_defines.h"
12 #include "util/u_memory.h" /* Offset() */
13 #include "util/u_draw_quad.h"
15 enum pipe_format formats[] = {
16 PIPE_FORMAT_R8G8B8A8_UNORM,
17 PIPE_FORMAT_B8G8R8A8_UNORM,
21 static const int WIDTH = 300;
22 static const int HEIGHT = 300;
24 static struct pipe_screen *screen = NULL;
25 static struct pipe_context *ctx = NULL;
26 static struct pipe_surface *surf = NULL;
27 static struct pipe_resource *tex = NULL;
28 static void *window = NULL;
35 static struct vertex vertices[4] =
37 { { 0.0f, -0.9f, 0.0f, 1.0f },
38 { 1.0f, 0.0f, 0.0f, 1.0f }
40 { { -0.9f, 0.9f, 0.0f, 1.0f },
41 { 0.0f, 1.0f, 0.0f, 1.0f }
43 { { 0.9f, 0.9f, 0.0f, 1.0f },
44 { 0.0f, 0.0f, 1.0f, 1.0f }
51 static void set_viewport( float x, float y,
52 float width, float height,
53 float near, float far)
56 float half_width = (float)width / 2.0f;
57 float half_height = (float)height / 2.0f;
58 float half_depth = ((float)far - (float)near) / 2.0f;
59 struct pipe_viewport_state vp;
61 vp.scale[0] = half_width;
62 vp.scale[1] = half_height;
63 vp.scale[2] = half_depth;
66 vp.translate[0] = half_width + x;
67 vp.translate[1] = half_height + y;
68 vp.translate[2] = half_depth + z;
69 vp.translate[3] = 0.0f;
71 ctx->set_viewport_state( ctx, &vp );
74 static void set_vertices( void )
76 struct pipe_vertex_element ve[2];
77 struct pipe_vertex_buffer vbuf;
80 memset(ve, 0, sizeof ve);
82 ve[0].src_offset = Offset(struct vertex, position);
83 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
84 ve[1].src_offset = Offset(struct vertex, color);
85 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
87 handle = ctx->create_vertex_elements_state(ctx, 2, ve);
88 ctx->bind_vertex_elements_state(ctx, handle);
91 vbuf.stride = sizeof( struct vertex );
92 vbuf.buffer_offset = 0;
93 vbuf.buffer = screen->user_buffer_create(screen,
96 PIPE_BIND_VERTEX_BUFFER);
98 ctx->set_vertex_buffers(ctx, 1, &vbuf);
101 static void set_vertex_shader( void )
108 "DCL OUT[0], POSITION\n"
109 "DCL OUT[1], COLOR\n"
110 " 0: MOV OUT[1], IN[1]\n"
111 " 1: MOV OUT[0], IN[0]\n"
114 handle = graw_parse_vertex_shader(ctx, text);
115 ctx->bind_vs_state(ctx, handle);
118 static void set_fragment_shader( void )
123 "DCL IN[0], COLOR, LINEAR\n"
124 "DCL OUT[0], COLOR\n"
125 " 0: MOV OUT[0], IN[0]\n"
128 handle = graw_parse_fragment_shader(ctx, text);
129 ctx->bind_fs_state(ctx, handle);
133 static void set_geometry_shader( void )
138 "PROPERTY GS_INPUT_PRIMITIVE TRIANGLES\n"
139 "PROPERTY GS_OUTPUT_PRIMITIVE TRIANGLE_STRIP\n"
140 "DCL IN[][0], POSITION, CONSTANT\n"
141 "DCL IN[][1], COLOR, CONSTANT\n"
142 "DCL OUT[0], POSITION, CONSTANT\n"
143 "DCL OUT[1], COLOR, CONSTANT\n"
144 "0:MOV OUT[0], IN[0][0]\n"
145 "1:MOV OUT[1], IN[0][1]\n"
147 "3:MOV OUT[0], IN[1][0]\n"
148 "4:MOV OUT[1], IN[0][1]\n" /* copy color from input vertex 0 */
150 "6:MOV OUT[0], IN[2][0]\n"
151 "7:MOV OUT[1], IN[2][1]\n"
156 handle = graw_parse_geometry_shader(ctx, text);
157 ctx->bind_gs_state(ctx, handle);
160 static void draw( void )
162 float clear_color[4] = {1,0,1,1};
164 ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
165 util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
166 ctx->flush(ctx, NULL);
168 screen->flush_frontbuffer(screen, tex, 0, 0, window);
172 static void init( void )
174 struct pipe_framebuffer_state fb;
175 struct pipe_resource templat;
176 struct pipe_surface surf_tmpl;
179 /* It's hard to say whether window or screen should be created
180 * first. Different environments would prefer one or the other.
182 * Also, no easy way of querying supported formats if the screen
183 * cannot be created first.
185 for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
186 screen = graw_create_window_and_screen(0, 0, 300, 300,
189 if (window && screen)
192 if (!screen || !window) {
193 fprintf(stderr, "Unable to create window\n");
197 ctx = screen->context_create(screen, NULL);
201 templat.target = PIPE_TEXTURE_2D;
202 templat.format = formats[i];
203 templat.width0 = WIDTH;
204 templat.height0 = HEIGHT;
206 templat.array_size = 1;
207 templat.last_level = 0;
208 templat.nr_samples = 1;
209 templat.bind = (PIPE_BIND_RENDER_TARGET |
210 PIPE_BIND_DISPLAY_TARGET);
212 tex = screen->resource_create(screen,
217 surf_tmpl.format = templat.format;
218 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
219 surf_tmpl.u.tex.level = 0;
220 surf_tmpl.u.tex.first_layer = 0;
221 surf_tmpl.u.tex.last_layer = 0;
222 surf = ctx->create_surface(ctx, tex, &surf_tmpl);
226 memset(&fb, 0, sizeof fb);
232 ctx->set_framebuffer_state(ctx, &fb);
235 struct pipe_blend_state blend;
237 memset(&blend, 0, sizeof blend);
238 blend.rt[0].colormask = PIPE_MASK_RGBA;
239 handle = ctx->create_blend_state(ctx, &blend);
240 ctx->bind_blend_state(ctx, handle);
244 struct pipe_depth_stencil_alpha_state depthstencil;
246 memset(&depthstencil, 0, sizeof depthstencil);
247 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
248 ctx->bind_depth_stencil_alpha_state(ctx, handle);
252 struct pipe_rasterizer_state rasterizer;
254 memset(&rasterizer, 0, sizeof rasterizer);
255 rasterizer.cull_face = PIPE_FACE_NONE;
256 rasterizer.gl_rasterization_rules = 1;
257 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
258 ctx->bind_rasterizer_state(ctx, handle);
261 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
264 set_fragment_shader();
265 set_geometry_shader();
269 int main( int argc, char *argv[] )
273 graw_set_display_func( draw );