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 void *window
= NULL
;
35 static struct vertex vertices
[3] =
38 { 0.0f
, -0.9f
, 0.0f
, 1.0f
},
39 { 1.0f
, 0.0f
, 0.0f
, 1.0f
}
42 { -0.9f
, 0.9f
, 0.0f
, 1.0f
},
43 { 0.0f
, 1.0f
, 0.0f
, 1.0f
}
46 { 0.9f
, 0.9f
, 0.0f
, 1.0f
},
47 { 0.0f
, 0.0f
, 1.0f
, 1.0f
}
54 static void set_viewport( float x
, float y
,
55 float width
, float height
,
56 float near
, float far
)
59 float half_width
= (float)width
/ 2.0f
;
60 float half_height
= (float)height
/ 2.0f
;
61 float half_depth
= ((float)far
- (float)near
) / 2.0f
;
62 struct pipe_viewport_state vp
;
64 vp
.scale
[0] = half_width
;
65 vp
.scale
[1] = half_height
;
66 vp
.scale
[2] = half_depth
;
69 vp
.translate
[0] = half_width
+ x
;
70 vp
.translate
[1] = half_height
+ y
;
71 vp
.translate
[2] = half_depth
+ z
;
72 vp
.translate
[3] = 0.0f
;
74 ctx
->set_viewport_state( ctx
, &vp
);
77 static void set_vertices( void )
79 struct pipe_vertex_element ve
[2];
80 struct pipe_vertex_buffer vbuf
;
83 memset(ve
, 0, sizeof ve
);
85 ve
[0].src_offset
= Offset(struct vertex
, position
);
86 ve
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
87 ve
[1].src_offset
= Offset(struct vertex
, color
);
88 ve
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
90 handle
= ctx
->create_vertex_elements_state(ctx
, 2, ve
);
91 ctx
->bind_vertex_elements_state(ctx
, handle
);
94 vbuf
.stride
= sizeof( struct vertex
);
95 vbuf
.max_index
= sizeof(vertices
) / vbuf
.stride
;
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
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
145 graw_save_surface_to_file(ctx
, surf
, NULL
);
147 screen
->flush_frontbuffer(screen
, surf
, window
);
151 static void init( void )
153 struct pipe_framebuffer_state fb
;
154 struct pipe_resource
*tex
, templat
;
157 /* It's hard to say whether window or screen should be created
158 * first. Different environments would prefer one or the other.
160 * Also, no easy way of querying supported formats if the screen
161 * cannot be created first.
164 window
== NULL
&& formats
[i
] != PIPE_FORMAT_NONE
;
167 screen
= graw_create_window_and_screen(0,0,300,300,
172 ctx
= screen
->context_create(screen
, NULL
);
174 fprintf(stderr
, "Unable to create context!\n");
178 templat
.target
= PIPE_TEXTURE_2D
;
179 templat
.format
= formats
[i
];
180 templat
.width0
= WIDTH
;
181 templat
.height0
= HEIGHT
;
183 templat
.last_level
= 0;
184 templat
.nr_samples
= 1;
185 templat
.bind
= (PIPE_BIND_RENDER_TARGET
|
186 PIPE_BIND_DISPLAY_TARGET
);
188 tex
= screen
->resource_create(screen
,
191 fprintf(stderr
, "Unable to create screen texture!\n");
195 surf
= screen
->get_tex_surface(screen
, tex
, 0, 0, 0,
196 PIPE_BIND_RENDER_TARGET
|
197 PIPE_BIND_DISPLAY_TARGET
);
199 fprintf(stderr
, "Unable to get tex surface!\n");
203 memset(&fb
, 0, sizeof fb
);
209 ctx
->set_framebuffer_state(ctx
, &fb
);
212 struct pipe_blend_state blend
;
214 memset(&blend
, 0, sizeof blend
);
215 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
216 handle
= ctx
->create_blend_state(ctx
, &blend
);
217 ctx
->bind_blend_state(ctx
, handle
);
221 struct pipe_depth_stencil_alpha_state depthstencil
;
223 memset(&depthstencil
, 0, sizeof depthstencil
);
224 handle
= ctx
->create_depth_stencil_alpha_state(ctx
, &depthstencil
);
225 ctx
->bind_depth_stencil_alpha_state(ctx
, handle
);
229 struct pipe_rasterizer_state rasterizer
;
231 memset(&rasterizer
, 0, sizeof rasterizer
);
232 rasterizer
.cull_face
= PIPE_FACE_NONE
;
233 rasterizer
.gl_rasterization_rules
= 1;
234 handle
= ctx
->create_rasterizer_state(ctx
, &rasterizer
);
235 ctx
->bind_rasterizer_state(ctx
, handle
);
238 set_viewport(0, 0, WIDTH
, HEIGHT
, 30, 1000);
241 set_fragment_shader();
244 static void args(int argc
, char *argv
[])
248 for (i
= 1; i
< argc
;) {
249 if (graw_parse_args(&i
, argc
, argv
)) {
256 int main( int argc
, char *argv
[] )
261 graw_set_display_func( draw
);