1bf215546Sopenharmony_ci/* Display a cleared blue window. This demo has no dependencies on 2bf215546Sopenharmony_ci * any utility code, just the graw interface and gallium. 3bf215546Sopenharmony_ci */ 4bf215546Sopenharmony_ci 5bf215546Sopenharmony_ci#include "graw_util.h" 6bf215546Sopenharmony_ci 7bf215546Sopenharmony_cistatic const int WIDTH = 300; 8bf215546Sopenharmony_cistatic const int HEIGHT = 300; 9bf215546Sopenharmony_ci 10bf215546Sopenharmony_cistatic struct graw_info info; 11bf215546Sopenharmony_ci 12bf215546Sopenharmony_ci 13bf215546Sopenharmony_cistatic struct pipe_resource *texture = NULL; 14bf215546Sopenharmony_cistatic struct pipe_sampler_view *sv = NULL; 15bf215546Sopenharmony_cistatic void *sampler = NULL; 16bf215546Sopenharmony_ci 17bf215546Sopenharmony_cistruct vertex { 18bf215546Sopenharmony_ci float position[4]; 19bf215546Sopenharmony_ci float color[4]; 20bf215546Sopenharmony_ci}; 21bf215546Sopenharmony_ci 22bf215546Sopenharmony_cistatic struct vertex vertices[] = 23bf215546Sopenharmony_ci{ 24bf215546Sopenharmony_ci { { 0.9, -0.9, 0.0, 1.0 }, 25bf215546Sopenharmony_ci { 1, 0, 0, 1 } }, 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci { { 0.9, 0.9, 0.0, 1.0 }, 28bf215546Sopenharmony_ci { 1, 1, 0, 1 } }, 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci { {-0.9, 0.9, 0.0, 1.0 }, 31bf215546Sopenharmony_ci { 0, 1, 0, 1 } }, 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci { {-0.9, -0.9, 0.0, 1.0 }, 34bf215546Sopenharmony_ci { 0, 0, 0, 1 } }, 35bf215546Sopenharmony_ci}; 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_cistatic void set_vertices( void ) 41bf215546Sopenharmony_ci{ 42bf215546Sopenharmony_ci struct pipe_vertex_element ve[2]; 43bf215546Sopenharmony_ci struct pipe_vertex_buffer vbuf; 44bf215546Sopenharmony_ci void *handle; 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci memset(ve, 0, sizeof ve); 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ci ve[0].src_offset = Offset(struct vertex, position); 49bf215546Sopenharmony_ci ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 50bf215546Sopenharmony_ci ve[1].src_offset = Offset(struct vertex, color); 51bf215546Sopenharmony_ci ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve); 54bf215546Sopenharmony_ci info.ctx->bind_vertex_elements_state(info.ctx, handle); 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci memset(&vbuf, 0, sizeof vbuf); 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci vbuf.stride = sizeof( struct vertex ); 59bf215546Sopenharmony_ci vbuf.buffer_offset = 0; 60bf215546Sopenharmony_ci vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx, 61bf215546Sopenharmony_ci PIPE_BIND_VERTEX_BUFFER, 62bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 63bf215546Sopenharmony_ci sizeof(vertices), 64bf215546Sopenharmony_ci vertices); 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf); 67bf215546Sopenharmony_ci} 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_cistatic void set_vertex_shader( void ) 70bf215546Sopenharmony_ci{ 71bf215546Sopenharmony_ci void *handle; 72bf215546Sopenharmony_ci const char *text = 73bf215546Sopenharmony_ci "VERT\n" 74bf215546Sopenharmony_ci "DCL IN[0]\n" 75bf215546Sopenharmony_ci "DCL IN[1]\n" 76bf215546Sopenharmony_ci "DCL OUT[0], POSITION\n" 77bf215546Sopenharmony_ci "DCL OUT[1], GENERIC[0]\n" 78bf215546Sopenharmony_ci " 0: MOV OUT[1], IN[1]\n" 79bf215546Sopenharmony_ci " 1: MOV OUT[0], IN[0]\n" 80bf215546Sopenharmony_ci " 2: END\n"; 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_ci handle = graw_parse_vertex_shader(info.ctx, text); 83bf215546Sopenharmony_ci info.ctx->bind_vs_state(info.ctx, handle); 84bf215546Sopenharmony_ci} 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_cistatic void set_fragment_shader( void ) 87bf215546Sopenharmony_ci{ 88bf215546Sopenharmony_ci void *handle; 89bf215546Sopenharmony_ci const char *text = 90bf215546Sopenharmony_ci "FRAG\n" 91bf215546Sopenharmony_ci "DCL IN[0], GENERIC[0], PERSPECTIVE\n" 92bf215546Sopenharmony_ci "DCL OUT[0], COLOR\n" 93bf215546Sopenharmony_ci "DCL TEMP[0]\n" 94bf215546Sopenharmony_ci "DCL SAMP[0]\n" 95bf215546Sopenharmony_ci "DCL SVIEW[0], 2D, FLOAT\n" 96bf215546Sopenharmony_ci " 0: TXP TEMP[0], IN[0], SAMP[0], 2D\n" 97bf215546Sopenharmony_ci " 1: MOV OUT[0], TEMP[0]\n" 98bf215546Sopenharmony_ci " 2: END\n"; 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci handle = graw_parse_fragment_shader(info.ctx, text); 101bf215546Sopenharmony_ci info.ctx->bind_fs_state(info.ctx, handle); 102bf215546Sopenharmony_ci} 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_cistatic void draw( void ) 106bf215546Sopenharmony_ci{ 107bf215546Sopenharmony_ci union pipe_color_union clear_color = { {.5,.5,.5,1} }; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0); 110bf215546Sopenharmony_ci util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, 4); 111bf215546Sopenharmony_ci info.ctx->flush(info.ctx, NULL, 0); 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ci graw_save_surface_to_file(info.ctx, info.color_surf[0], NULL); 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci graw_util_flush_front(&info); 116bf215546Sopenharmony_ci} 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci#define SIZE 16 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_cistatic void init_tex( void ) 122bf215546Sopenharmony_ci{ 123bf215546Sopenharmony_ci ubyte tex2d[SIZE][SIZE][4]; 124bf215546Sopenharmony_ci int s, t; 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ci#if (SIZE != 2) 127bf215546Sopenharmony_ci for (s = 0; s < SIZE; s++) { 128bf215546Sopenharmony_ci for (t = 0; t < SIZE; t++) { 129bf215546Sopenharmony_ci if (0) { 130bf215546Sopenharmony_ci int x = (s ^ t) & 1; 131bf215546Sopenharmony_ci tex2d[t][s][0] = (x) ? 0 : 63; 132bf215546Sopenharmony_ci tex2d[t][s][1] = (x) ? 0 : 128; 133bf215546Sopenharmony_ci tex2d[t][s][2] = 0; 134bf215546Sopenharmony_ci tex2d[t][s][3] = 0xff; 135bf215546Sopenharmony_ci } 136bf215546Sopenharmony_ci else { 137bf215546Sopenharmony_ci int x = ((s ^ t) >> 2) & 1; 138bf215546Sopenharmony_ci tex2d[t][s][0] = s*255/(SIZE-1); 139bf215546Sopenharmony_ci tex2d[t][s][1] = t*255/(SIZE-1); 140bf215546Sopenharmony_ci tex2d[t][s][2] = (x) ? 0 : 128; 141bf215546Sopenharmony_ci tex2d[t][s][3] = 0xff; 142bf215546Sopenharmony_ci } 143bf215546Sopenharmony_ci } 144bf215546Sopenharmony_ci } 145bf215546Sopenharmony_ci#else 146bf215546Sopenharmony_ci tex2d[0][0][0] = 0; 147bf215546Sopenharmony_ci tex2d[0][0][1] = 255; 148bf215546Sopenharmony_ci tex2d[0][0][2] = 255; 149bf215546Sopenharmony_ci tex2d[0][0][3] = 0; 150bf215546Sopenharmony_ci 151bf215546Sopenharmony_ci tex2d[0][1][0] = 0; 152bf215546Sopenharmony_ci tex2d[0][1][1] = 0; 153bf215546Sopenharmony_ci tex2d[0][1][2] = 255; 154bf215546Sopenharmony_ci tex2d[0][1][3] = 255; 155bf215546Sopenharmony_ci 156bf215546Sopenharmony_ci tex2d[1][0][0] = 255; 157bf215546Sopenharmony_ci tex2d[1][0][1] = 255; 158bf215546Sopenharmony_ci tex2d[1][0][2] = 0; 159bf215546Sopenharmony_ci tex2d[1][0][3] = 255; 160bf215546Sopenharmony_ci 161bf215546Sopenharmony_ci tex2d[1][1][0] = 255; 162bf215546Sopenharmony_ci tex2d[1][1][1] = 0; 163bf215546Sopenharmony_ci tex2d[1][1][2] = 0; 164bf215546Sopenharmony_ci tex2d[1][1][3] = 255; 165bf215546Sopenharmony_ci#endif 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci texture = graw_util_create_tex2d(&info, SIZE, SIZE, 168bf215546Sopenharmony_ci PIPE_FORMAT_B8G8R8A8_UNORM, tex2d); 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci sv = graw_util_create_simple_sampler_view(&info, texture); 171bf215546Sopenharmony_ci info.ctx->set_sampler_views(info.ctx, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &sv); 172bf215546Sopenharmony_ci 173bf215546Sopenharmony_ci sampler = graw_util_create_simple_sampler(&info, 174bf215546Sopenharmony_ci PIPE_TEX_WRAP_REPEAT, 175bf215546Sopenharmony_ci PIPE_TEX_FILTER_NEAREST); 176bf215546Sopenharmony_ci info.ctx->bind_sampler_states(info.ctx, PIPE_SHADER_FRAGMENT, 177bf215546Sopenharmony_ci 0, 1, &sampler); 178bf215546Sopenharmony_ci} 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_cistatic void init( void ) 182bf215546Sopenharmony_ci{ 183bf215546Sopenharmony_ci if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE)) 184bf215546Sopenharmony_ci exit(1); 185bf215546Sopenharmony_ci 186bf215546Sopenharmony_ci graw_util_default_state(&info, FALSE); 187bf215546Sopenharmony_ci 188bf215546Sopenharmony_ci { 189bf215546Sopenharmony_ci struct pipe_rasterizer_state rasterizer; 190bf215546Sopenharmony_ci void *handle; 191bf215546Sopenharmony_ci memset(&rasterizer, 0, sizeof rasterizer); 192bf215546Sopenharmony_ci rasterizer.cull_face = PIPE_FACE_NONE; 193bf215546Sopenharmony_ci rasterizer.half_pixel_center = 1; 194bf215546Sopenharmony_ci rasterizer.bottom_edge_rule = 1; 195bf215546Sopenharmony_ci rasterizer.depth_clip_near = 1; 196bf215546Sopenharmony_ci rasterizer.depth_clip_far = 1; 197bf215546Sopenharmony_ci handle = info.ctx->create_rasterizer_state(info.ctx, &rasterizer); 198bf215546Sopenharmony_ci info.ctx->bind_rasterizer_state(info.ctx, handle); 199bf215546Sopenharmony_ci } 200bf215546Sopenharmony_ci 201bf215546Sopenharmony_ci graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 1000); 202bf215546Sopenharmony_ci 203bf215546Sopenharmony_ci init_tex(); 204bf215546Sopenharmony_ci 205bf215546Sopenharmony_ci set_vertices(); 206bf215546Sopenharmony_ci set_vertex_shader(); 207bf215546Sopenharmony_ci set_fragment_shader(); 208bf215546Sopenharmony_ci} 209bf215546Sopenharmony_ci 210bf215546Sopenharmony_ci 211bf215546Sopenharmony_cistatic void args(int argc, char *argv[]) 212bf215546Sopenharmony_ci{ 213bf215546Sopenharmony_ci int i; 214bf215546Sopenharmony_ci 215bf215546Sopenharmony_ci for (i = 1; i < argc;) { 216bf215546Sopenharmony_ci if (graw_parse_args(&i, argc, argv)) { 217bf215546Sopenharmony_ci continue; 218bf215546Sopenharmony_ci } 219bf215546Sopenharmony_ci exit(1); 220bf215546Sopenharmony_ci } 221bf215546Sopenharmony_ci} 222bf215546Sopenharmony_ci 223bf215546Sopenharmony_ciint main( int argc, char *argv[] ) 224bf215546Sopenharmony_ci{ 225bf215546Sopenharmony_ci args(argc, argv); 226bf215546Sopenharmony_ci init(); 227bf215546Sopenharmony_ci 228bf215546Sopenharmony_ci graw_set_display_func( draw ); 229bf215546Sopenharmony_ci graw_main_loop(); 230bf215546Sopenharmony_ci return 0; 231bf215546Sopenharmony_ci} 232