1848b8605Smrg/* Display a cleared blue window. This demo has no dependencies on 2848b8605Smrg * any utility code, just the graw interface and gallium. 3848b8605Smrg */ 4848b8605Smrg 5848b8605Smrg#include "state_tracker/graw.h" 6848b8605Smrg#include "pipe/p_screen.h" 7848b8605Smrg#include "pipe/p_context.h" 8848b8605Smrg#include "pipe/p_shader_tokens.h" 9848b8605Smrg#include "pipe/p_state.h" 10848b8605Smrg#include "pipe/p_defines.h" 11848b8605Smrg 12848b8605Smrg#include "util/u_debug.h" /* debug_dump_surface_bmp() */ 13848b8605Smrg#include "util/u_inlines.h" 14848b8605Smrg#include "util/u_memory.h" /* Offset() */ 15848b8605Smrg#include "util/u_draw_quad.h" 16848b8605Smrg#include "util/u_box.h" 17848b8605Smrg 18848b8605Smrg#include <stdio.h> 19848b8605Smrg 20848b8605Smrgenum pipe_format formats[] = { 21848b8605Smrg PIPE_FORMAT_RGBA8888_UNORM, 22848b8605Smrg PIPE_FORMAT_BGRA8888_UNORM, 23848b8605Smrg PIPE_FORMAT_NONE 24848b8605Smrg}; 25848b8605Smrg 26848b8605Smrgstatic const int WIDTH = 300; 27848b8605Smrgstatic const int HEIGHT = 300; 28848b8605Smrg 29848b8605Smrgstatic struct pipe_screen *screen = NULL; 30848b8605Smrgstatic struct pipe_context *ctx = NULL; 31848b8605Smrgstatic struct pipe_resource *rttex = NULL; 32848b8605Smrgstatic struct pipe_resource *samptex = NULL; 33848b8605Smrgstatic struct pipe_surface *surf = NULL; 34848b8605Smrgstatic struct pipe_sampler_view *sv = NULL; 35848b8605Smrgstatic void *sampler = NULL; 36848b8605Smrgstatic void *window = NULL; 37848b8605Smrg 38848b8605Smrgstruct vertex { 39848b8605Smrg float position[4]; 40848b8605Smrg float color[4]; 41848b8605Smrg}; 42848b8605Smrg 43848b8605Smrgstatic struct vertex vertices[] = 44848b8605Smrg{ 45848b8605Smrg { { 0.9, -0.9, 0.0, 1.0 }, 46848b8605Smrg { 1, 0, 0, 1 } }, 47848b8605Smrg 48848b8605Smrg { { 0.9, 0.9, 0.0, 1.0 }, 49848b8605Smrg { 1, 1, 0, 1 } }, 50848b8605Smrg 51848b8605Smrg { {-0.9, 0.9, 0.0, 1.0 }, 52848b8605Smrg { 0, 1, 0, 1 } }, 53848b8605Smrg 54848b8605Smrg { {-0.9, -0.9, 0.0, 1.0 }, 55848b8605Smrg { 0, 0, 0, 1 } }, 56848b8605Smrg}; 57848b8605Smrg 58848b8605Smrg 59848b8605Smrg 60848b8605Smrg 61848b8605Smrgstatic void set_viewport( float x, float y, 62848b8605Smrg float width, float height, 63b8e80941Smrg float zNear, float zFar) 64848b8605Smrg{ 65b8e80941Smrg float z = zFar; 66848b8605Smrg float half_width = (float)width / 2.0f; 67848b8605Smrg float half_height = (float)height / 2.0f; 68b8e80941Smrg float half_depth = ((float)zFar - (float)zNear) / 2.0f; 69848b8605Smrg struct pipe_viewport_state vp; 70848b8605Smrg 71848b8605Smrg vp.scale[0] = half_width; 72848b8605Smrg vp.scale[1] = half_height; 73848b8605Smrg vp.scale[2] = half_depth; 74848b8605Smrg 75848b8605Smrg vp.translate[0] = half_width + x; 76848b8605Smrg vp.translate[1] = half_height + y; 77848b8605Smrg vp.translate[2] = half_depth + z; 78848b8605Smrg 79848b8605Smrg ctx->set_viewport_states( ctx, 0, 1, &vp ); 80848b8605Smrg} 81848b8605Smrg 82848b8605Smrgstatic void set_vertices( void ) 83848b8605Smrg{ 84848b8605Smrg struct pipe_vertex_element ve[2]; 85848b8605Smrg struct pipe_vertex_buffer vbuf; 86848b8605Smrg void *handle; 87848b8605Smrg 88848b8605Smrg memset(ve, 0, sizeof ve); 89848b8605Smrg 90848b8605Smrg ve[0].src_offset = Offset(struct vertex, position); 91848b8605Smrg ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 92848b8605Smrg ve[1].src_offset = Offset(struct vertex, color); 93848b8605Smrg ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 94848b8605Smrg 95848b8605Smrg handle = ctx->create_vertex_elements_state(ctx, 2, ve); 96848b8605Smrg ctx->bind_vertex_elements_state(ctx, handle); 97848b8605Smrg 98848b8605Smrg memset(&vbuf, 0, sizeof vbuf); 99848b8605Smrg 100848b8605Smrg vbuf.stride = sizeof( struct vertex ); 101848b8605Smrg vbuf.buffer_offset = 0; 102b8e80941Smrg vbuf.buffer.resource = pipe_buffer_create_with_data(ctx, 103848b8605Smrg PIPE_BIND_VERTEX_BUFFER, 104848b8605Smrg PIPE_USAGE_DEFAULT, 105848b8605Smrg sizeof(vertices), 106848b8605Smrg vertices); 107848b8605Smrg 108848b8605Smrg ctx->set_vertex_buffers(ctx, 0, 1, &vbuf); 109848b8605Smrg} 110848b8605Smrg 111848b8605Smrgstatic void set_vertex_shader( void ) 112848b8605Smrg{ 113848b8605Smrg void *handle; 114848b8605Smrg const char *text = 115848b8605Smrg "VERT\n" 116848b8605Smrg "DCL IN[0]\n" 117848b8605Smrg "DCL IN[1]\n" 118848b8605Smrg "DCL OUT[0], POSITION\n" 119848b8605Smrg "DCL OUT[1], GENERIC[0]\n" 120848b8605Smrg " 0: MOV OUT[1], IN[1]\n" 121848b8605Smrg " 1: MOV OUT[0], IN[0]\n" 122848b8605Smrg " 2: END\n"; 123848b8605Smrg 124848b8605Smrg handle = graw_parse_vertex_shader(ctx, text); 125848b8605Smrg ctx->bind_vs_state(ctx, handle); 126848b8605Smrg} 127848b8605Smrg 128848b8605Smrgstatic void set_fragment_shader( void ) 129848b8605Smrg{ 130848b8605Smrg void *handle; 131848b8605Smrg const char *text = 132848b8605Smrg "FRAG\n" 133848b8605Smrg "DCL IN[0], GENERIC[0], PERSPECTIVE\n" 134848b8605Smrg "DCL OUT[0], COLOR\n" 135848b8605Smrg "DCL TEMP[0]\n" 136848b8605Smrg "DCL SAMP[0]\n" 137848b8605Smrg "DCL RES[0], 2D, FLOAT\n" 138848b8605Smrg " 0: SAMPLE TEMP[0], IN[0], RES[0], SAMP[0]\n" 139848b8605Smrg " 1: MOV OUT[0], TEMP[0]\n" 140848b8605Smrg " 2: END\n"; 141848b8605Smrg 142848b8605Smrg handle = graw_parse_fragment_shader(ctx, text); 143848b8605Smrg ctx->bind_fs_state(ctx, handle); 144848b8605Smrg} 145848b8605Smrg 146848b8605Smrg 147848b8605Smrgstatic void draw( void ) 148848b8605Smrg{ 149848b8605Smrg union pipe_color_union clear_color = { {.5,.5,.5,1} }; 150848b8605Smrg 151848b8605Smrg ctx->clear(ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0); 152848b8605Smrg util_draw_arrays(ctx, PIPE_PRIM_QUADS, 0, 4); 153848b8605Smrg ctx->flush(ctx, NULL, 0); 154848b8605Smrg 155848b8605Smrg graw_save_surface_to_file(ctx, surf, NULL); 156848b8605Smrg 157848b8605Smrg screen->flush_frontbuffer(screen, rttex, 0, 0, window, NULL); 158848b8605Smrg} 159848b8605Smrg 160848b8605Smrg#define SIZE 16 161848b8605Smrg 162848b8605Smrgstatic void init_tex( void ) 163848b8605Smrg{ 164848b8605Smrg struct pipe_sampler_view sv_template; 165848b8605Smrg struct pipe_sampler_state sampler_desc; 166848b8605Smrg struct pipe_resource templat; 167848b8605Smrg struct pipe_box box; 168848b8605Smrg ubyte tex2d[SIZE][SIZE][4]; 169848b8605Smrg int s, t; 170848b8605Smrg 171848b8605Smrg#if (SIZE != 2) 172848b8605Smrg for (s = 0; s < SIZE; s++) { 173848b8605Smrg for (t = 0; t < SIZE; t++) { 174848b8605Smrg if (0) { 175848b8605Smrg int x = (s ^ t) & 1; 176848b8605Smrg tex2d[t][s][0] = (x) ? 0 : 63; 177848b8605Smrg tex2d[t][s][1] = (x) ? 0 : 128; 178848b8605Smrg tex2d[t][s][2] = 0; 179848b8605Smrg tex2d[t][s][3] = 0xff; 180848b8605Smrg } 181848b8605Smrg else { 182848b8605Smrg int x = ((s ^ t) >> 2) & 1; 183848b8605Smrg tex2d[t][s][0] = s*255/(SIZE-1); 184848b8605Smrg tex2d[t][s][1] = t*255/(SIZE-1); 185848b8605Smrg tex2d[t][s][2] = (x) ? 0 : 128; 186848b8605Smrg tex2d[t][s][3] = 0xff; 187848b8605Smrg } 188848b8605Smrg } 189848b8605Smrg } 190848b8605Smrg#else 191848b8605Smrg tex2d[0][0][0] = 0; 192848b8605Smrg tex2d[0][0][1] = 255; 193848b8605Smrg tex2d[0][0][2] = 255; 194848b8605Smrg tex2d[0][0][3] = 0; 195848b8605Smrg 196848b8605Smrg tex2d[0][1][0] = 0; 197848b8605Smrg tex2d[0][1][1] = 0; 198848b8605Smrg tex2d[0][1][2] = 255; 199848b8605Smrg tex2d[0][1][3] = 255; 200848b8605Smrg 201848b8605Smrg tex2d[1][0][0] = 255; 202848b8605Smrg tex2d[1][0][1] = 255; 203848b8605Smrg tex2d[1][0][2] = 0; 204848b8605Smrg tex2d[1][0][3] = 255; 205848b8605Smrg 206848b8605Smrg tex2d[1][1][0] = 255; 207848b8605Smrg tex2d[1][1][1] = 0; 208848b8605Smrg tex2d[1][1][2] = 0; 209848b8605Smrg tex2d[1][1][3] = 255; 210848b8605Smrg#endif 211848b8605Smrg 212b8e80941Smrg memset(&templat, 0, sizeof(templat)); 213848b8605Smrg templat.target = PIPE_TEXTURE_2D; 214848b8605Smrg templat.format = PIPE_FORMAT_B8G8R8A8_UNORM; 215848b8605Smrg templat.width0 = SIZE; 216848b8605Smrg templat.height0 = SIZE; 217848b8605Smrg templat.depth0 = 1; 218848b8605Smrg templat.last_level = 0; 219848b8605Smrg templat.bind = PIPE_BIND_SAMPLER_VIEW; 220848b8605Smrg 221848b8605Smrg 222848b8605Smrg samptex = screen->resource_create(screen, 223848b8605Smrg &templat); 224848b8605Smrg if (samptex == NULL) 225848b8605Smrg exit(4); 226848b8605Smrg 227848b8605Smrg u_box_2d(0,0,SIZE,SIZE, &box); 228848b8605Smrg 229b8e80941Smrg ctx->texture_subdata(ctx, 230b8e80941Smrg samptex, 231b8e80941Smrg 0, 232b8e80941Smrg PIPE_TRANSFER_WRITE, 233b8e80941Smrg &box, 234b8e80941Smrg tex2d, 235b8e80941Smrg sizeof tex2d[0], 236b8e80941Smrg sizeof tex2d); 237848b8605Smrg 238848b8605Smrg /* Possibly read back & compare against original data: 239848b8605Smrg */ 240848b8605Smrg if (0) 241848b8605Smrg { 242848b8605Smrg struct pipe_transfer *t; 243848b8605Smrg uint32_t *ptr; 244848b8605Smrg ptr = pipe_transfer_map(ctx, samptex, 245848b8605Smrg 0, 0, /* level, layer */ 246848b8605Smrg PIPE_TRANSFER_READ, 247848b8605Smrg 0, 0, SIZE, SIZE, &t); /* x, y, width, height */ 248848b8605Smrg 249848b8605Smrg if (memcmp(ptr, tex2d, sizeof tex2d) != 0) { 250848b8605Smrg assert(0); 251848b8605Smrg exit(9); 252848b8605Smrg } 253848b8605Smrg 254848b8605Smrg ctx->transfer_unmap(ctx, t); 255848b8605Smrg } 256848b8605Smrg 257848b8605Smrg memset(&sv_template, 0, sizeof sv_template); 258848b8605Smrg sv_template.format = samptex->format; 259848b8605Smrg sv_template.texture = samptex; 260848b8605Smrg sv_template.swizzle_r = 0; 261848b8605Smrg sv_template.swizzle_g = 1; 262848b8605Smrg sv_template.swizzle_b = 2; 263848b8605Smrg sv_template.swizzle_a = 3; 264848b8605Smrg sv = ctx->create_sampler_view(ctx, samptex, &sv_template); 265848b8605Smrg if (sv == NULL) 266848b8605Smrg exit(5); 267848b8605Smrg 268848b8605Smrg ctx->set_sampler_views(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sv); 269848b8605Smrg 270848b8605Smrg 271848b8605Smrg memset(&sampler_desc, 0, sizeof sampler_desc); 272848b8605Smrg sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT; 273848b8605Smrg sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT; 274848b8605Smrg sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT; 275848b8605Smrg sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST; 276848b8605Smrg sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 277848b8605Smrg sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 278848b8605Smrg sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE; 279848b8605Smrg sampler_desc.compare_func = 0; 280848b8605Smrg sampler_desc.normalized_coords = 1; 281848b8605Smrg sampler_desc.max_anisotropy = 0; 282848b8605Smrg 283848b8605Smrg sampler = ctx->create_sampler_state(ctx, &sampler_desc); 284848b8605Smrg if (sampler == NULL) 285848b8605Smrg exit(6); 286848b8605Smrg 287848b8605Smrg ctx->bind_sampler_states(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sampler); 288848b8605Smrg 289848b8605Smrg} 290848b8605Smrg 291848b8605Smrgstatic void init( void ) 292848b8605Smrg{ 293848b8605Smrg struct pipe_framebuffer_state fb; 294848b8605Smrg struct pipe_resource templat; 295848b8605Smrg struct pipe_surface surf_tmpl; 296848b8605Smrg int i; 297848b8605Smrg 298848b8605Smrg /* It's hard to say whether window or screen should be created 299848b8605Smrg * first. Different environments would prefer one or the other. 300848b8605Smrg * 301848b8605Smrg * Also, no easy way of querying supported formats if the screen 302848b8605Smrg * cannot be created first. 303848b8605Smrg */ 304848b8605Smrg for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) { 305848b8605Smrg screen = graw_create_window_and_screen(0, 0, 300, 300, 306848b8605Smrg formats[i], 307848b8605Smrg &window); 308848b8605Smrg if (window && screen) 309848b8605Smrg break; 310848b8605Smrg } 311848b8605Smrg if (!screen || !window) { 312848b8605Smrg fprintf(stderr, "Unable to create window\n"); 313848b8605Smrg exit(1); 314848b8605Smrg } 315848b8605Smrg 316b8e80941Smrg ctx = screen->context_create(screen, NULL, 0); 317848b8605Smrg if (ctx == NULL) 318848b8605Smrg exit(3); 319848b8605Smrg 320b8e80941Smrg memset(&templat, 0, sizeof(templat)); 321848b8605Smrg templat.target = PIPE_TEXTURE_2D; 322848b8605Smrg templat.format = formats[i]; 323848b8605Smrg templat.width0 = WIDTH; 324848b8605Smrg templat.height0 = HEIGHT; 325848b8605Smrg templat.depth0 = 1; 326848b8605Smrg templat.array_size = 1; 327848b8605Smrg templat.last_level = 0; 328848b8605Smrg templat.bind = (PIPE_BIND_RENDER_TARGET | 329848b8605Smrg PIPE_BIND_DISPLAY_TARGET); 330848b8605Smrg 331848b8605Smrg rttex = screen->resource_create(screen, 332848b8605Smrg &templat); 333848b8605Smrg if (rttex == NULL) 334848b8605Smrg exit(4); 335848b8605Smrg 336848b8605Smrg surf_tmpl.format = templat.format; 337848b8605Smrg surf_tmpl.u.tex.level = 0; 338848b8605Smrg surf_tmpl.u.tex.first_layer = 0; 339848b8605Smrg surf_tmpl.u.tex.last_layer = 0; 340848b8605Smrg surf = ctx->create_surface(ctx, rttex, &surf_tmpl); 341848b8605Smrg if (surf == NULL) 342848b8605Smrg exit(5); 343848b8605Smrg 344848b8605Smrg memset(&fb, 0, sizeof fb); 345848b8605Smrg fb.nr_cbufs = 1; 346848b8605Smrg fb.width = WIDTH; 347848b8605Smrg fb.height = HEIGHT; 348848b8605Smrg fb.cbufs[0] = surf; 349848b8605Smrg 350848b8605Smrg ctx->set_framebuffer_state(ctx, &fb); 351848b8605Smrg 352848b8605Smrg { 353848b8605Smrg struct pipe_blend_state blend; 354848b8605Smrg void *handle; 355848b8605Smrg memset(&blend, 0, sizeof blend); 356848b8605Smrg blend.rt[0].colormask = PIPE_MASK_RGBA; 357848b8605Smrg handle = ctx->create_blend_state(ctx, &blend); 358848b8605Smrg ctx->bind_blend_state(ctx, handle); 359848b8605Smrg } 360848b8605Smrg 361848b8605Smrg { 362848b8605Smrg struct pipe_depth_stencil_alpha_state depthstencil; 363848b8605Smrg void *handle; 364848b8605Smrg memset(&depthstencil, 0, sizeof depthstencil); 365848b8605Smrg handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil); 366848b8605Smrg ctx->bind_depth_stencil_alpha_state(ctx, handle); 367848b8605Smrg } 368848b8605Smrg 369848b8605Smrg { 370848b8605Smrg struct pipe_rasterizer_state rasterizer; 371848b8605Smrg void *handle; 372848b8605Smrg memset(&rasterizer, 0, sizeof rasterizer); 373848b8605Smrg rasterizer.cull_face = PIPE_FACE_NONE; 374848b8605Smrg rasterizer.half_pixel_center = 1; 375848b8605Smrg rasterizer.bottom_edge_rule = 1; 376b8e80941Smrg rasterizer.depth_clip_near = 1; 377b8e80941Smrg rasterizer.depth_clip_far = 1; 378848b8605Smrg handle = ctx->create_rasterizer_state(ctx, &rasterizer); 379848b8605Smrg ctx->bind_rasterizer_state(ctx, handle); 380848b8605Smrg } 381848b8605Smrg 382848b8605Smrg set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000); 383848b8605Smrg 384848b8605Smrg init_tex(); 385848b8605Smrg 386848b8605Smrg set_vertices(); 387848b8605Smrg set_vertex_shader(); 388848b8605Smrg set_fragment_shader(); 389848b8605Smrg} 390848b8605Smrg 391848b8605Smrgstatic void args(int argc, char *argv[]) 392848b8605Smrg{ 393848b8605Smrg int i; 394848b8605Smrg 395848b8605Smrg for (i = 1; i < argc;) { 396848b8605Smrg if (graw_parse_args(&i, argc, argv)) { 397848b8605Smrg continue; 398848b8605Smrg } 399848b8605Smrg exit(1); 400848b8605Smrg } 401848b8605Smrg} 402848b8605Smrg 403848b8605Smrg 404848b8605Smrgint main( int argc, char *argv[] ) 405848b8605Smrg{ 406848b8605Smrg args(argc, argv); 407848b8605Smrg init(); 408848b8605Smrg 409848b8605Smrg graw_set_display_func( draw ); 410848b8605Smrg graw_main_loop(); 411848b8605Smrg return 0; 412848b8605Smrg} 413