1 /* Display a cleared blue window. This demo has no dependencies on
2 * any utility code, just the graw interface and gallium.
5 #include "state_tracker/graw.h"
6 #include "pipe/p_screen.h"
7 #include "pipe/p_context.h"
8 #include "pipe/p_shader_tokens.h"
9 #include "pipe/p_state.h"
10 #include "pipe/p_defines.h"
12 #include "util/u_debug.h" /* debug_dump_surface_bmp() */
13 #include "util/u_inlines.h"
14 #include "util/u_memory.h" /* Offset() */
15 #include "util/u_draw_quad.h"
16 #include "util/u_box.h"
20 enum pipe_format formats
[] = {
21 PIPE_FORMAT_RGBA8888_UNORM
,
22 PIPE_FORMAT_BGRA8888_UNORM
,
26 static const int WIDTH
= 300;
27 static const int HEIGHT
= 300;
29 static struct pipe_screen
*screen
= NULL
;
30 static struct pipe_context
*ctx
= NULL
;
31 static struct pipe_resource
*rttex
= NULL
;
32 static struct pipe_resource
*samptex
= NULL
;
33 static struct pipe_surface
*surf
= NULL
;
34 static struct pipe_sampler_view
*sv
= NULL
;
35 static void *sampler
= NULL
;
36 static void *window
= NULL
;
43 static struct vertex vertices
[] =
45 { { 0.9, -0.9, 0.0, 1.0 },
48 { { 0.9, 0.9, 0.0, 1.0 },
51 { {-0.9, 0.9, 0.0, 1.0 },
54 { {-0.9, -0.9, 0.0, 1.0 },
61 static void set_viewport( float x
, float y
,
62 float width
, float height
,
63 float near
, float far
)
66 float half_width
= (float)width
/ 2.0f
;
67 float half_height
= (float)height
/ 2.0f
;
68 float half_depth
= ((float)far
- (float)near
) / 2.0f
;
69 struct pipe_viewport_state vp
;
71 vp
.scale
[0] = half_width
;
72 vp
.scale
[1] = half_height
;
73 vp
.scale
[2] = half_depth
;
76 vp
.translate
[0] = half_width
+ x
;
77 vp
.translate
[1] = half_height
+ y
;
78 vp
.translate
[2] = half_depth
+ z
;
79 vp
.translate
[3] = 0.0f
;
81 ctx
->set_viewport_states( ctx
, 0, 1, &vp
);
84 static void set_vertices( void )
86 struct pipe_vertex_element ve
[2];
87 struct pipe_vertex_buffer vbuf
;
90 memset(ve
, 0, sizeof ve
);
92 ve
[0].src_offset
= Offset(struct vertex
, position
);
93 ve
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
94 ve
[1].src_offset
= Offset(struct vertex
, color
);
95 ve
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
97 handle
= ctx
->create_vertex_elements_state(ctx
, 2, ve
);
98 ctx
->bind_vertex_elements_state(ctx
, handle
);
100 memset(&vbuf
, 0, sizeof vbuf
);
102 vbuf
.stride
= sizeof( struct vertex
);
103 vbuf
.buffer_offset
= 0;
104 vbuf
.buffer
= pipe_buffer_create_with_data(ctx
,
105 PIPE_BIND_VERTEX_BUFFER
,
110 ctx
->set_vertex_buffers(ctx
, 0, 1, &vbuf
);
113 static void set_vertex_shader( void )
120 "DCL OUT[0], POSITION\n"
121 "DCL OUT[1], GENERIC[0]\n"
122 " 0: MOV OUT[1], IN[1]\n"
123 " 1: MOV OUT[0], IN[0]\n"
126 handle
= graw_parse_vertex_shader(ctx
, text
);
127 ctx
->bind_vs_state(ctx
, handle
);
130 static void set_fragment_shader( void )
135 "DCL IN[0], GENERIC[0], PERSPECTIVE\n"
136 "DCL OUT[0], COLOR\n"
139 "DCL RES[0], 2D, FLOAT\n"
140 " 0: SAMPLE TEMP[0], IN[0], RES[0], SAMP[0]\n"
141 " 1: MOV OUT[0], TEMP[0]\n"
144 handle
= graw_parse_fragment_shader(ctx
, text
);
145 ctx
->bind_fs_state(ctx
, handle
);
149 static void draw( void )
151 union pipe_color_union clear_color
= { {.5,.5,.5,1} };
153 ctx
->clear(ctx
, PIPE_CLEAR_COLOR
, &clear_color
, 0, 0);
154 util_draw_arrays(ctx
, PIPE_PRIM_QUADS
, 0, 4);
155 ctx
->flush(ctx
, NULL
, 0);
157 graw_save_surface_to_file(ctx
, surf
, NULL
);
159 screen
->flush_frontbuffer(screen
, rttex
, 0, 0, window
, NULL
);
164 static void init_tex( void )
166 struct pipe_sampler_view sv_template
;
167 struct pipe_sampler_state sampler_desc
;
168 struct pipe_resource templat
;
170 ubyte tex2d
[SIZE
][SIZE
][4];
174 for (s
= 0; s
< SIZE
; s
++) {
175 for (t
= 0; t
< SIZE
; t
++) {
178 tex2d
[t
][s
][0] = (x
) ? 0 : 63;
179 tex2d
[t
][s
][1] = (x
) ? 0 : 128;
181 tex2d
[t
][s
][3] = 0xff;
184 int x
= ((s
^ t
) >> 2) & 1;
185 tex2d
[t
][s
][0] = s
*255/(SIZE
-1);
186 tex2d
[t
][s
][1] = t
*255/(SIZE
-1);
187 tex2d
[t
][s
][2] = (x
) ? 0 : 128;
188 tex2d
[t
][s
][3] = 0xff;
194 tex2d
[0][0][1] = 255;
195 tex2d
[0][0][2] = 255;
200 tex2d
[0][1][2] = 255;
201 tex2d
[0][1][3] = 255;
203 tex2d
[1][0][0] = 255;
204 tex2d
[1][0][1] = 255;
206 tex2d
[1][0][3] = 255;
208 tex2d
[1][1][0] = 255;
211 tex2d
[1][1][3] = 255;
214 templat
.target
= PIPE_TEXTURE_2D
;
215 templat
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
216 templat
.width0
= SIZE
;
217 templat
.height0
= SIZE
;
219 templat
.last_level
= 0;
220 templat
.nr_samples
= 1;
221 templat
.bind
= PIPE_BIND_SAMPLER_VIEW
;
224 samptex
= screen
->resource_create(screen
,
229 u_box_2d(0,0,SIZE
,SIZE
, &box
);
231 ctx
->transfer_inline_write(ctx
,
240 /* Possibly read back & compare against original data:
244 struct pipe_transfer
*t
;
246 ptr
= pipe_transfer_map(ctx
, samptex
,
247 0, 0, /* level, layer */
249 0, 0, SIZE
, SIZE
, &t
); /* x, y, width, height */
251 if (memcmp(ptr
, tex2d
, sizeof tex2d
) != 0) {
256 ctx
->transfer_unmap(ctx
, t
);
259 memset(&sv_template
, 0, sizeof sv_template
);
260 sv_template
.format
= samptex
->format
;
261 sv_template
.texture
= samptex
;
262 sv_template
.swizzle_r
= 0;
263 sv_template
.swizzle_g
= 1;
264 sv_template
.swizzle_b
= 2;
265 sv_template
.swizzle_a
= 3;
266 sv
= ctx
->create_sampler_view(ctx
, samptex
, &sv_template
);
270 ctx
->set_sampler_views(ctx
, PIPE_SHADER_FRAGMENT
, 0, 1, &sv
);
273 memset(&sampler_desc
, 0, sizeof sampler_desc
);
274 sampler_desc
.wrap_s
= PIPE_TEX_WRAP_REPEAT
;
275 sampler_desc
.wrap_t
= PIPE_TEX_WRAP_REPEAT
;
276 sampler_desc
.wrap_r
= PIPE_TEX_WRAP_REPEAT
;
277 sampler_desc
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
278 sampler_desc
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
279 sampler_desc
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
280 sampler_desc
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
281 sampler_desc
.compare_func
= 0;
282 sampler_desc
.normalized_coords
= 1;
283 sampler_desc
.max_anisotropy
= 0;
285 sampler
= ctx
->create_sampler_state(ctx
, &sampler_desc
);
289 ctx
->bind_sampler_states(ctx
, PIPE_SHADER_FRAGMENT
, 0, 1, &sampler
);
293 static void init( void )
295 struct pipe_framebuffer_state fb
;
296 struct pipe_resource templat
;
297 struct pipe_surface surf_tmpl
;
300 /* It's hard to say whether window or screen should be created
301 * first. Different environments would prefer one or the other.
303 * Also, no easy way of querying supported formats if the screen
304 * cannot be created first.
306 for (i
= 0; formats
[i
] != PIPE_FORMAT_NONE
; i
++) {
307 screen
= graw_create_window_and_screen(0, 0, 300, 300,
310 if (window
&& screen
)
313 if (!screen
|| !window
) {
314 fprintf(stderr
, "Unable to create window\n");
318 ctx
= screen
->context_create(screen
, NULL
);
322 templat
.target
= PIPE_TEXTURE_2D
;
323 templat
.format
= formats
[i
];
324 templat
.width0
= WIDTH
;
325 templat
.height0
= HEIGHT
;
327 templat
.array_size
= 1;
328 templat
.last_level
= 0;
329 templat
.nr_samples
= 1;
330 templat
.bind
= (PIPE_BIND_RENDER_TARGET
|
331 PIPE_BIND_DISPLAY_TARGET
);
333 rttex
= screen
->resource_create(screen
,
338 surf_tmpl
.format
= templat
.format
;
339 surf_tmpl
.u
.tex
.level
= 0;
340 surf_tmpl
.u
.tex
.first_layer
= 0;
341 surf_tmpl
.u
.tex
.last_layer
= 0;
342 surf
= ctx
->create_surface(ctx
, rttex
, &surf_tmpl
);
346 memset(&fb
, 0, sizeof fb
);
352 ctx
->set_framebuffer_state(ctx
, &fb
);
355 struct pipe_blend_state blend
;
357 memset(&blend
, 0, sizeof blend
);
358 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
359 handle
= ctx
->create_blend_state(ctx
, &blend
);
360 ctx
->bind_blend_state(ctx
, handle
);
364 struct pipe_depth_stencil_alpha_state depthstencil
;
366 memset(&depthstencil
, 0, sizeof depthstencil
);
367 handle
= ctx
->create_depth_stencil_alpha_state(ctx
, &depthstencil
);
368 ctx
->bind_depth_stencil_alpha_state(ctx
, handle
);
372 struct pipe_rasterizer_state rasterizer
;
374 memset(&rasterizer
, 0, sizeof rasterizer
);
375 rasterizer
.cull_face
= PIPE_FACE_NONE
;
376 rasterizer
.half_pixel_center
= 1;
377 rasterizer
.bottom_edge_rule
= 1;
378 rasterizer
.depth_clip
= 1;
379 handle
= ctx
->create_rasterizer_state(ctx
, &rasterizer
);
380 ctx
->bind_rasterizer_state(ctx
, handle
);
383 set_viewport(0, 0, WIDTH
, HEIGHT
, 30, 1000);
389 set_fragment_shader();
392 static void args(int argc
, char *argv
[])
396 for (i
= 1; i
< argc
;) {
397 if (graw_parse_args(&i
, argc
, argv
)) {
405 int main( int argc
, char *argv
[] )
410 graw_set_display_func( draw
);