1 /* Display a cleared blue window. This demo has no dependencies on
2 * any utility code, just the graw interface and gallium.
7 static const int WIDTH
= 300;
8 static const int HEIGHT
= 300;
10 static struct graw_info info
;
13 static struct pipe_resource
*texture
= NULL
;
14 static struct pipe_sampler_view
*sv
= NULL
;
15 static void *sampler
= NULL
;
22 static struct vertex vertices
[] =
24 { { 0.9, -0.9, 0.0, 1.0 },
27 { { 0.9, 0.9, 0.0, 1.0 },
30 { {-0.9, 0.9, 0.0, 1.0 },
33 { {-0.9, -0.9, 0.0, 1.0 },
40 static void set_vertices( void )
42 struct pipe_vertex_element ve
[2];
43 struct pipe_vertex_buffer vbuf
;
46 memset(ve
, 0, sizeof ve
);
48 ve
[0].src_offset
= Offset(struct vertex
, position
);
49 ve
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
50 ve
[1].src_offset
= Offset(struct vertex
, color
);
51 ve
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
53 handle
= info
.ctx
->create_vertex_elements_state(info
.ctx
, 2, ve
);
54 info
.ctx
->bind_vertex_elements_state(info
.ctx
, handle
);
56 memset(&vbuf
, 0, sizeof vbuf
);
58 vbuf
.stride
= sizeof( struct vertex
);
59 vbuf
.buffer_offset
= 0;
60 vbuf
.buffer
.resource
= pipe_buffer_create_with_data(info
.ctx
,
61 PIPE_BIND_VERTEX_BUFFER
,
66 info
.ctx
->set_vertex_buffers(info
.ctx
, 0, 1, &vbuf
);
69 static void set_vertex_shader( void )
76 "DCL OUT[0], POSITION\n"
77 "DCL OUT[1], GENERIC[0]\n"
78 " 0: MOV OUT[1], IN[1]\n"
79 " 1: MOV OUT[0], IN[0]\n"
82 handle
= graw_parse_vertex_shader(info
.ctx
, text
);
83 info
.ctx
->bind_vs_state(info
.ctx
, handle
);
86 static void set_fragment_shader( void )
91 "DCL IN[0], GENERIC[0], PERSPECTIVE\n"
95 "DCL SVIEW[0], 2D, FLOAT\n"
96 " 0: TXP TEMP[0], IN[0], SAMP[0], 2D\n"
97 " 1: MOV OUT[0], TEMP[0]\n"
100 handle
= graw_parse_fragment_shader(info
.ctx
, text
);
101 info
.ctx
->bind_fs_state(info
.ctx
, handle
);
105 static void draw( void )
107 union pipe_color_union clear_color
= { {.5,.5,.5,1} };
109 info
.ctx
->clear(info
.ctx
, PIPE_CLEAR_COLOR
, &clear_color
, 0, 0);
110 util_draw_arrays(info
.ctx
, PIPE_PRIM_QUADS
, 0, 4);
111 info
.ctx
->flush(info
.ctx
, NULL
, 0);
113 graw_save_surface_to_file(info
.ctx
, info
.color_surf
[0], NULL
);
115 graw_util_flush_front(&info
);
121 static void init_tex( void )
123 ubyte tex2d
[SIZE
][SIZE
][4];
127 for (s
= 0; s
< SIZE
; s
++) {
128 for (t
= 0; t
< SIZE
; t
++) {
131 tex2d
[t
][s
][0] = (x
) ? 0 : 63;
132 tex2d
[t
][s
][1] = (x
) ? 0 : 128;
134 tex2d
[t
][s
][3] = 0xff;
137 int x
= ((s
^ t
) >> 2) & 1;
138 tex2d
[t
][s
][0] = s
*255/(SIZE
-1);
139 tex2d
[t
][s
][1] = t
*255/(SIZE
-1);
140 tex2d
[t
][s
][2] = (x
) ? 0 : 128;
141 tex2d
[t
][s
][3] = 0xff;
147 tex2d
[0][0][1] = 255;
148 tex2d
[0][0][2] = 255;
153 tex2d
[0][1][2] = 255;
154 tex2d
[0][1][3] = 255;
156 tex2d
[1][0][0] = 255;
157 tex2d
[1][0][1] = 255;
159 tex2d
[1][0][3] = 255;
161 tex2d
[1][1][0] = 255;
164 tex2d
[1][1][3] = 255;
167 texture
= graw_util_create_tex2d(&info
, SIZE
, SIZE
,
168 PIPE_FORMAT_B8G8R8A8_UNORM
, tex2d
);
170 sv
= graw_util_create_simple_sampler_view(&info
, texture
);
171 info
.ctx
->set_sampler_views(info
.ctx
, PIPE_SHADER_FRAGMENT
, 0, 1, &sv
);
173 sampler
= graw_util_create_simple_sampler(&info
,
174 PIPE_TEX_WRAP_REPEAT
,
175 PIPE_TEX_FILTER_NEAREST
);
176 info
.ctx
->bind_sampler_states(info
.ctx
, PIPE_SHADER_FRAGMENT
,
181 static void init( void )
183 if (!graw_util_create_window(&info
, WIDTH
, HEIGHT
, 1, FALSE
))
186 graw_util_default_state(&info
, FALSE
);
189 struct pipe_rasterizer_state rasterizer
;
191 memset(&rasterizer
, 0, sizeof rasterizer
);
192 rasterizer
.cull_face
= PIPE_FACE_NONE
;
193 rasterizer
.half_pixel_center
= 1;
194 rasterizer
.bottom_edge_rule
= 1;
195 rasterizer
.depth_clip_near
= 1;
196 rasterizer
.depth_clip_far
= 1;
197 handle
= info
.ctx
->create_rasterizer_state(info
.ctx
, &rasterizer
);
198 info
.ctx
->bind_rasterizer_state(info
.ctx
, handle
);
201 graw_util_viewport(&info
, 0, 0, WIDTH
, HEIGHT
, 30, 1000);
207 set_fragment_shader();
211 static void args(int argc
, char *argv
[])
215 for (i
= 1; i
< argc
;) {
216 if (graw_parse_args(&i
, argc
, argv
)) {
223 int main( int argc
, char *argv
[] )
228 graw_set_display_func( draw
);