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 "frontend/graw.h" 6bf215546Sopenharmony_ci#include "pipe/p_screen.h" 7bf215546Sopenharmony_ci#include "pipe/p_context.h" 8bf215546Sopenharmony_ci#include "pipe/p_shader_tokens.h" 9bf215546Sopenharmony_ci#include "pipe/p_state.h" 10bf215546Sopenharmony_ci#include "pipe/p_defines.h" 11bf215546Sopenharmony_ci 12bf215546Sopenharmony_ci#include <stdio.h> /* for fread(), etc */ 13bf215546Sopenharmony_ci 14bf215546Sopenharmony_ci#include "util/u_inlines.h" 15bf215546Sopenharmony_ci#include "util/u_memory.h" /* Offset() */ 16bf215546Sopenharmony_ci#include "util/u_draw_quad.h" 17bf215546Sopenharmony_ci#include "util/u_box.h" 18bf215546Sopenharmony_ci 19bf215546Sopenharmony_cistatic const char *filename = NULL; 20bf215546Sopenharmony_ciunsigned show_fps = 0; 21bf215546Sopenharmony_ci 22bf215546Sopenharmony_ci 23bf215546Sopenharmony_cistatic void usage(char *name) 24bf215546Sopenharmony_ci{ 25bf215546Sopenharmony_ci fprintf(stderr, "usage: %s [ options ] shader_filename\n", name); 26bf215546Sopenharmony_ci#ifndef _WIN32 27bf215546Sopenharmony_ci fprintf(stderr, "\n" ); 28bf215546Sopenharmony_ci fprintf(stderr, "options:\n"); 29bf215546Sopenharmony_ci fprintf(stderr, " -fps show frames per second\n"); 30bf215546Sopenharmony_ci#endif 31bf215546Sopenharmony_ci} 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_cienum pipe_format formats[] = { 35bf215546Sopenharmony_ci PIPE_FORMAT_RGBA8888_UNORM, 36bf215546Sopenharmony_ci PIPE_FORMAT_BGRA8888_UNORM, 37bf215546Sopenharmony_ci PIPE_FORMAT_NONE 38bf215546Sopenharmony_ci}; 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_cistatic const int WIDTH = 250; 41bf215546Sopenharmony_cistatic const int HEIGHT = 250; 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_cistatic struct pipe_screen *screen = NULL; 44bf215546Sopenharmony_cistatic struct pipe_context *ctx = NULL; 45bf215546Sopenharmony_cistatic struct pipe_resource *rttex = NULL; 46bf215546Sopenharmony_cistatic struct pipe_resource *constbuf = NULL; 47bf215546Sopenharmony_cistatic struct pipe_surface *surf = NULL; 48bf215546Sopenharmony_cistatic struct pipe_sampler_view *sv = NULL; 49bf215546Sopenharmony_cistatic void *sampler = NULL; 50bf215546Sopenharmony_cistatic void *window = NULL; 51bf215546Sopenharmony_cistatic struct pipe_resource *samptex = NULL; 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_cistruct vertex { 54bf215546Sopenharmony_ci float position[4]; 55bf215546Sopenharmony_ci float color[3]; 56bf215546Sopenharmony_ci}; 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci/* Draw a regular mesh 59bf215546Sopenharmony_ci */ 60bf215546Sopenharmony_ci#define MESH_SZ 16 61bf215546Sopenharmony_cistatic struct vertex vertices[MESH_SZ * MESH_SZ]; 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_cistatic float constants[] = 64bf215546Sopenharmony_ci{ 0.4, 0, 0, 1, 65bf215546Sopenharmony_ci 1, 1, 1, 1, 66bf215546Sopenharmony_ci 2, 2, 2, 2, 67bf215546Sopenharmony_ci 4, 8, 16, 32, 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_ci 3, 0, 0, 0, 70bf215546Sopenharmony_ci 0, .5, 0, 0, 71bf215546Sopenharmony_ci 0, 0, 1, 0, 72bf215546Sopenharmony_ci 0, 0, 0, 1, 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci 1, 0, 0, 0.5, 75bf215546Sopenharmony_ci 0, 1, 0, 0.5, 76bf215546Sopenharmony_ci 0, 0, 1, 0, 77bf215546Sopenharmony_ci 0, 0, 0, 1, 78bf215546Sopenharmony_ci}; 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_cistatic void init_fs_constbuf( void ) 81bf215546Sopenharmony_ci{ 82bf215546Sopenharmony_ci struct pipe_resource templat; 83bf215546Sopenharmony_ci struct pipe_box box; 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci memset(&templat, 0, sizeof(templat)); 86bf215546Sopenharmony_ci templat.target = PIPE_BUFFER; 87bf215546Sopenharmony_ci templat.format = PIPE_FORMAT_R8_UNORM; 88bf215546Sopenharmony_ci templat.width0 = sizeof(constants); 89bf215546Sopenharmony_ci templat.height0 = 1; 90bf215546Sopenharmony_ci templat.depth0 = 1; 91bf215546Sopenharmony_ci templat.array_size = 1; 92bf215546Sopenharmony_ci templat.last_level = 0; 93bf215546Sopenharmony_ci templat.bind = PIPE_BIND_CONSTANT_BUFFER; 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci constbuf = screen->resource_create(screen, 96bf215546Sopenharmony_ci &templat); 97bf215546Sopenharmony_ci if (constbuf == NULL) 98bf215546Sopenharmony_ci exit(4); 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci u_box_2d(0,0,sizeof(constants),1, &box); 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci ctx->buffer_subdata(ctx, constbuf, 104bf215546Sopenharmony_ci PIPE_MAP_WRITE, 105bf215546Sopenharmony_ci 0, sizeof(constants), constants); 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci pipe_set_constant_buffer(ctx, 108bf215546Sopenharmony_ci PIPE_SHADER_VERTEX, 0, 109bf215546Sopenharmony_ci constbuf); 110bf215546Sopenharmony_ci} 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_cistatic void set_viewport( float x, float y, 114bf215546Sopenharmony_ci float width, float height, 115bf215546Sopenharmony_ci float zNear, float zFar) 116bf215546Sopenharmony_ci{ 117bf215546Sopenharmony_ci float z = zFar; 118bf215546Sopenharmony_ci float half_width = (float)width / 2.0f; 119bf215546Sopenharmony_ci float half_height = (float)height / 2.0f; 120bf215546Sopenharmony_ci float half_depth = ((float)zFar - (float)zNear) / 2.0f; 121bf215546Sopenharmony_ci struct pipe_viewport_state vp; 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_ci vp.scale[0] = half_width; 124bf215546Sopenharmony_ci vp.scale[1] = half_height; 125bf215546Sopenharmony_ci vp.scale[2] = half_depth; 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci vp.translate[0] = half_width + x; 128bf215546Sopenharmony_ci vp.translate[1] = half_height + y; 129bf215546Sopenharmony_ci vp.translate[2] = half_depth + z; 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_ci vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X; 132bf215546Sopenharmony_ci vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y; 133bf215546Sopenharmony_ci vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z; 134bf215546Sopenharmony_ci vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W; 135bf215546Sopenharmony_ci 136bf215546Sopenharmony_ci ctx->set_viewport_states( ctx, 0, 1, &vp ); 137bf215546Sopenharmony_ci} 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_cistatic void set_vertices( void ) 140bf215546Sopenharmony_ci{ 141bf215546Sopenharmony_ci struct pipe_vertex_element ve[2]; 142bf215546Sopenharmony_ci struct pipe_vertex_buffer vbuf; 143bf215546Sopenharmony_ci void *handle; 144bf215546Sopenharmony_ci int x,y; 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci memset(ve, 0, sizeof ve); 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci ve[0].src_offset = Offset(struct vertex, position); 149bf215546Sopenharmony_ci ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 150bf215546Sopenharmony_ci ve[1].src_offset = Offset(struct vertex, color); 151bf215546Sopenharmony_ci ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci handle = ctx->create_vertex_elements_state(ctx, 2, ve); 154bf215546Sopenharmony_ci ctx->bind_vertex_elements_state(ctx, handle); 155bf215546Sopenharmony_ci 156bf215546Sopenharmony_ci for (x = 0; x < MESH_SZ; x++) { 157bf215546Sopenharmony_ci for (y = 0; y < MESH_SZ; y++) { 158bf215546Sopenharmony_ci int i = y * MESH_SZ + x; 159bf215546Sopenharmony_ci vertices[i].position[0] = ((float)x)/MESH_SZ * 2.0 - 1.0; 160bf215546Sopenharmony_ci vertices[i].position[1] = ((float)y)/MESH_SZ * 2.0 - 1.0; 161bf215546Sopenharmony_ci vertices[i].position[2] = 0; 162bf215546Sopenharmony_ci vertices[i].position[3] = 1.0; 163bf215546Sopenharmony_ci 164bf215546Sopenharmony_ci vertices[i].color[0] = .5; 165bf215546Sopenharmony_ci vertices[i].color[1] = (float)x / (float)MESH_SZ; 166bf215546Sopenharmony_ci vertices[i].color[2] = (float)y / (float)MESH_SZ; 167bf215546Sopenharmony_ci } 168bf215546Sopenharmony_ci } 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci memset(&vbuf, 0, sizeof vbuf); 171bf215546Sopenharmony_ci 172bf215546Sopenharmony_ci vbuf.stride = sizeof( struct vertex ); 173bf215546Sopenharmony_ci vbuf.buffer_offset = 0; 174bf215546Sopenharmony_ci vbuf.buffer.resource = pipe_buffer_create_with_data(ctx, 175bf215546Sopenharmony_ci PIPE_BIND_VERTEX_BUFFER, 176bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 177bf215546Sopenharmony_ci sizeof(vertices), 178bf215546Sopenharmony_ci vertices); 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_ci ctx->set_vertex_buffers(ctx, 0, 1, 0, false, &vbuf); 181bf215546Sopenharmony_ci} 182bf215546Sopenharmony_ci 183bf215546Sopenharmony_cistatic void set_vertex_shader( void ) 184bf215546Sopenharmony_ci{ 185bf215546Sopenharmony_ci FILE *f; 186bf215546Sopenharmony_ci char buf[50000]; 187bf215546Sopenharmony_ci void *handle; 188bf215546Sopenharmony_ci int sz; 189bf215546Sopenharmony_ci 190bf215546Sopenharmony_ci if ((f = fopen(filename, "r")) == NULL) { 191bf215546Sopenharmony_ci fprintf(stderr, "Couldn't open %s\n", filename); 192bf215546Sopenharmony_ci exit(1); 193bf215546Sopenharmony_ci } 194bf215546Sopenharmony_ci 195bf215546Sopenharmony_ci sz = fread(buf, 1, sizeof(buf), f); 196bf215546Sopenharmony_ci if (!feof(f)) { 197bf215546Sopenharmony_ci printf("file too long\n"); 198bf215546Sopenharmony_ci exit(1); 199bf215546Sopenharmony_ci } 200bf215546Sopenharmony_ci printf("%.*s\n", sz, buf); 201bf215546Sopenharmony_ci buf[sz] = 0; 202bf215546Sopenharmony_ci 203bf215546Sopenharmony_ci handle = graw_parse_vertex_shader(ctx, buf); 204bf215546Sopenharmony_ci ctx->bind_vs_state(ctx, handle); 205bf215546Sopenharmony_ci fclose(f); 206bf215546Sopenharmony_ci} 207bf215546Sopenharmony_ci 208bf215546Sopenharmony_cistatic void set_fragment_shader( void ) 209bf215546Sopenharmony_ci{ 210bf215546Sopenharmony_ci void *handle; 211bf215546Sopenharmony_ci const char *text = 212bf215546Sopenharmony_ci "FRAG\n" 213bf215546Sopenharmony_ci "DCL IN[0], COLOR, LINEAR\n" 214bf215546Sopenharmony_ci "DCL OUT[0], COLOR\n" 215bf215546Sopenharmony_ci " 0: MOV OUT[0], IN[0]\n" 216bf215546Sopenharmony_ci " 1: END\n"; 217bf215546Sopenharmony_ci 218bf215546Sopenharmony_ci handle = graw_parse_fragment_shader(ctx, text); 219bf215546Sopenharmony_ci ctx->bind_fs_state(ctx, handle); 220bf215546Sopenharmony_ci} 221bf215546Sopenharmony_ci 222bf215546Sopenharmony_ci 223bf215546Sopenharmony_ci 224bf215546Sopenharmony_cistatic void draw( void ) 225bf215546Sopenharmony_ci{ 226bf215546Sopenharmony_ci union pipe_color_union clear_color = { {.1,.3,.5,0} }; 227bf215546Sopenharmony_ci 228bf215546Sopenharmony_ci ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0); 229bf215546Sopenharmony_ci util_draw_arrays(ctx, PIPE_PRIM_POINTS, 0, ARRAY_SIZE(vertices)); 230bf215546Sopenharmony_ci ctx->flush(ctx, NULL, 0); 231bf215546Sopenharmony_ci 232bf215546Sopenharmony_ci graw_save_surface_to_file(ctx, surf, NULL); 233bf215546Sopenharmony_ci 234bf215546Sopenharmony_ci screen->flush_frontbuffer(screen, ctx, rttex, 0, 0, window, NULL); 235bf215546Sopenharmony_ci} 236bf215546Sopenharmony_ci 237bf215546Sopenharmony_ci#define SIZE 16 238bf215546Sopenharmony_ci 239bf215546Sopenharmony_cistatic void init_tex( void ) 240bf215546Sopenharmony_ci{ 241bf215546Sopenharmony_ci struct pipe_sampler_view sv_template; 242bf215546Sopenharmony_ci struct pipe_sampler_state sampler_desc; 243bf215546Sopenharmony_ci struct pipe_resource templat; 244bf215546Sopenharmony_ci struct pipe_box box; 245bf215546Sopenharmony_ci ubyte tex2d[SIZE][SIZE][4]; 246bf215546Sopenharmony_ci int s, t; 247bf215546Sopenharmony_ci 248bf215546Sopenharmony_ci#if (SIZE != 2) 249bf215546Sopenharmony_ci for (s = 0; s < SIZE; s++) { 250bf215546Sopenharmony_ci for (t = 0; t < SIZE; t++) { 251bf215546Sopenharmony_ci if (0) { 252bf215546Sopenharmony_ci int x = (s ^ t) & 1; 253bf215546Sopenharmony_ci tex2d[t][s][0] = (x) ? 0 : 63; 254bf215546Sopenharmony_ci tex2d[t][s][1] = (x) ? 0 : 128; 255bf215546Sopenharmony_ci tex2d[t][s][2] = 0; 256bf215546Sopenharmony_ci tex2d[t][s][3] = 0xff; 257bf215546Sopenharmony_ci } 258bf215546Sopenharmony_ci else { 259bf215546Sopenharmony_ci int x = ((s ^ t) >> 2) & 1; 260bf215546Sopenharmony_ci tex2d[t][s][0] = s*255/(SIZE-1); 261bf215546Sopenharmony_ci tex2d[t][s][1] = t*255/(SIZE-1); 262bf215546Sopenharmony_ci tex2d[t][s][2] = (x) ? 0 : 128; 263bf215546Sopenharmony_ci tex2d[t][s][3] = 0xff; 264bf215546Sopenharmony_ci } 265bf215546Sopenharmony_ci } 266bf215546Sopenharmony_ci } 267bf215546Sopenharmony_ci#else 268bf215546Sopenharmony_ci tex2d[0][0][0] = 0; 269bf215546Sopenharmony_ci tex2d[0][0][1] = 255; 270bf215546Sopenharmony_ci tex2d[0][0][2] = 255; 271bf215546Sopenharmony_ci tex2d[0][0][3] = 0; 272bf215546Sopenharmony_ci 273bf215546Sopenharmony_ci tex2d[0][1][0] = 0; 274bf215546Sopenharmony_ci tex2d[0][1][1] = 0; 275bf215546Sopenharmony_ci tex2d[0][1][2] = 255; 276bf215546Sopenharmony_ci tex2d[0][1][3] = 255; 277bf215546Sopenharmony_ci 278bf215546Sopenharmony_ci tex2d[1][0][0] = 255; 279bf215546Sopenharmony_ci tex2d[1][0][1] = 255; 280bf215546Sopenharmony_ci tex2d[1][0][2] = 0; 281bf215546Sopenharmony_ci tex2d[1][0][3] = 255; 282bf215546Sopenharmony_ci 283bf215546Sopenharmony_ci tex2d[1][1][0] = 255; 284bf215546Sopenharmony_ci tex2d[1][1][1] = 0; 285bf215546Sopenharmony_ci tex2d[1][1][2] = 0; 286bf215546Sopenharmony_ci tex2d[1][1][3] = 255; 287bf215546Sopenharmony_ci#endif 288bf215546Sopenharmony_ci 289bf215546Sopenharmony_ci memset(&templat, 0, sizeof(templat)); 290bf215546Sopenharmony_ci templat.target = PIPE_TEXTURE_2D; 291bf215546Sopenharmony_ci templat.format = PIPE_FORMAT_B8G8R8A8_UNORM; 292bf215546Sopenharmony_ci templat.width0 = SIZE; 293bf215546Sopenharmony_ci templat.height0 = SIZE; 294bf215546Sopenharmony_ci templat.depth0 = 1; 295bf215546Sopenharmony_ci templat.array_size = 1; 296bf215546Sopenharmony_ci templat.last_level = 0; 297bf215546Sopenharmony_ci templat.bind = PIPE_BIND_SAMPLER_VIEW; 298bf215546Sopenharmony_ci 299bf215546Sopenharmony_ci 300bf215546Sopenharmony_ci samptex = screen->resource_create(screen, 301bf215546Sopenharmony_ci &templat); 302bf215546Sopenharmony_ci if (samptex == NULL) 303bf215546Sopenharmony_ci exit(4); 304bf215546Sopenharmony_ci 305bf215546Sopenharmony_ci u_box_2d(0,0,SIZE,SIZE, &box); 306bf215546Sopenharmony_ci 307bf215546Sopenharmony_ci ctx->texture_subdata(ctx, 308bf215546Sopenharmony_ci samptex, 309bf215546Sopenharmony_ci 0, 310bf215546Sopenharmony_ci PIPE_MAP_WRITE, 311bf215546Sopenharmony_ci &box, 312bf215546Sopenharmony_ci tex2d, 313bf215546Sopenharmony_ci sizeof tex2d[0], 314bf215546Sopenharmony_ci sizeof tex2d); 315bf215546Sopenharmony_ci 316bf215546Sopenharmony_ci /* Possibly read back & compare against original data: 317bf215546Sopenharmony_ci */ 318bf215546Sopenharmony_ci if (0) 319bf215546Sopenharmony_ci { 320bf215546Sopenharmony_ci struct pipe_transfer *t; 321bf215546Sopenharmony_ci uint32_t *ptr; 322bf215546Sopenharmony_ci ptr = pipe_texture_map(ctx, samptex, 323bf215546Sopenharmony_ci 0, 0, /* level, layer */ 324bf215546Sopenharmony_ci PIPE_MAP_READ, 325bf215546Sopenharmony_ci 0, 0, SIZE, SIZE, &t); /* x, y, width, height */ 326bf215546Sopenharmony_ci 327bf215546Sopenharmony_ci if (memcmp(ptr, tex2d, sizeof tex2d) != 0) { 328bf215546Sopenharmony_ci assert(0); 329bf215546Sopenharmony_ci exit(9); 330bf215546Sopenharmony_ci } 331bf215546Sopenharmony_ci 332bf215546Sopenharmony_ci ctx->texture_unmap(ctx, t); 333bf215546Sopenharmony_ci } 334bf215546Sopenharmony_ci 335bf215546Sopenharmony_ci memset(&sv_template, 0, sizeof sv_template); 336bf215546Sopenharmony_ci sv_template.format = samptex->format; 337bf215546Sopenharmony_ci sv_template.texture = samptex; 338bf215546Sopenharmony_ci sv_template.swizzle_r = 0; 339bf215546Sopenharmony_ci sv_template.swizzle_g = 1; 340bf215546Sopenharmony_ci sv_template.swizzle_b = 2; 341bf215546Sopenharmony_ci sv_template.swizzle_a = 3; 342bf215546Sopenharmony_ci sv = ctx->create_sampler_view(ctx, samptex, &sv_template); 343bf215546Sopenharmony_ci if (sv == NULL) 344bf215546Sopenharmony_ci exit(5); 345bf215546Sopenharmony_ci 346bf215546Sopenharmony_ci ctx->set_sampler_views(ctx, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &sv); 347bf215546Sopenharmony_ci 348bf215546Sopenharmony_ci 349bf215546Sopenharmony_ci memset(&sampler_desc, 0, sizeof sampler_desc); 350bf215546Sopenharmony_ci sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT; 351bf215546Sopenharmony_ci sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT; 352bf215546Sopenharmony_ci sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT; 353bf215546Sopenharmony_ci sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST; 354bf215546Sopenharmony_ci sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 355bf215546Sopenharmony_ci sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 356bf215546Sopenharmony_ci sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE; 357bf215546Sopenharmony_ci sampler_desc.compare_func = 0; 358bf215546Sopenharmony_ci sampler_desc.normalized_coords = 1; 359bf215546Sopenharmony_ci sampler_desc.max_anisotropy = 0; 360bf215546Sopenharmony_ci 361bf215546Sopenharmony_ci sampler = ctx->create_sampler_state(ctx, &sampler_desc); 362bf215546Sopenharmony_ci if (sampler == NULL) 363bf215546Sopenharmony_ci exit(6); 364bf215546Sopenharmony_ci 365bf215546Sopenharmony_ci ctx->bind_sampler_states(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sampler); 366bf215546Sopenharmony_ci 367bf215546Sopenharmony_ci} 368bf215546Sopenharmony_ci 369bf215546Sopenharmony_cistatic void init( void ) 370bf215546Sopenharmony_ci{ 371bf215546Sopenharmony_ci struct pipe_framebuffer_state fb; 372bf215546Sopenharmony_ci struct pipe_resource templat; 373bf215546Sopenharmony_ci struct pipe_surface surf_tmpl; 374bf215546Sopenharmony_ci int i; 375bf215546Sopenharmony_ci 376bf215546Sopenharmony_ci /* It's hard to say whether window or screen should be created 377bf215546Sopenharmony_ci * first. Different environments would prefer one or the other. 378bf215546Sopenharmony_ci * 379bf215546Sopenharmony_ci * Also, no easy way of querying supported formats if the screen 380bf215546Sopenharmony_ci * cannot be created first. 381bf215546Sopenharmony_ci */ 382bf215546Sopenharmony_ci for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) { 383bf215546Sopenharmony_ci screen = graw_create_window_and_screen(0, 0, 300, 300, 384bf215546Sopenharmony_ci formats[i], 385bf215546Sopenharmony_ci &window); 386bf215546Sopenharmony_ci if (window && screen) 387bf215546Sopenharmony_ci break; 388bf215546Sopenharmony_ci } 389bf215546Sopenharmony_ci if (!screen || !window) { 390bf215546Sopenharmony_ci fprintf(stderr, "Unable to create window\n"); 391bf215546Sopenharmony_ci exit(1); 392bf215546Sopenharmony_ci } 393bf215546Sopenharmony_ci 394bf215546Sopenharmony_ci ctx = screen->context_create(screen, NULL, 0); 395bf215546Sopenharmony_ci if (ctx == NULL) 396bf215546Sopenharmony_ci exit(3); 397bf215546Sopenharmony_ci 398bf215546Sopenharmony_ci memset(&templat, 0, sizeof(templat)); 399bf215546Sopenharmony_ci templat.target = PIPE_TEXTURE_2D; 400bf215546Sopenharmony_ci templat.format = formats[i]; 401bf215546Sopenharmony_ci templat.width0 = WIDTH; 402bf215546Sopenharmony_ci templat.height0 = HEIGHT; 403bf215546Sopenharmony_ci templat.depth0 = 1; 404bf215546Sopenharmony_ci templat.array_size = 1; 405bf215546Sopenharmony_ci templat.last_level = 0; 406bf215546Sopenharmony_ci templat.bind = (PIPE_BIND_RENDER_TARGET | 407bf215546Sopenharmony_ci PIPE_BIND_DISPLAY_TARGET); 408bf215546Sopenharmony_ci 409bf215546Sopenharmony_ci rttex = screen->resource_create(screen, 410bf215546Sopenharmony_ci &templat); 411bf215546Sopenharmony_ci if (rttex == NULL) 412bf215546Sopenharmony_ci exit(4); 413bf215546Sopenharmony_ci 414bf215546Sopenharmony_ci surf_tmpl.format = templat.format; 415bf215546Sopenharmony_ci surf_tmpl.u.tex.level = 0; 416bf215546Sopenharmony_ci surf_tmpl.u.tex.first_layer = 0; 417bf215546Sopenharmony_ci surf_tmpl.u.tex.last_layer = 0; 418bf215546Sopenharmony_ci surf = ctx->create_surface(ctx, rttex, &surf_tmpl); 419bf215546Sopenharmony_ci if (surf == NULL) 420bf215546Sopenharmony_ci exit(5); 421bf215546Sopenharmony_ci 422bf215546Sopenharmony_ci memset(&fb, 0, sizeof fb); 423bf215546Sopenharmony_ci fb.nr_cbufs = 1; 424bf215546Sopenharmony_ci fb.width = WIDTH; 425bf215546Sopenharmony_ci fb.height = HEIGHT; 426bf215546Sopenharmony_ci fb.cbufs[0] = surf; 427bf215546Sopenharmony_ci 428bf215546Sopenharmony_ci ctx->set_framebuffer_state(ctx, &fb); 429bf215546Sopenharmony_ci 430bf215546Sopenharmony_ci { 431bf215546Sopenharmony_ci struct pipe_blend_state blend; 432bf215546Sopenharmony_ci void *handle; 433bf215546Sopenharmony_ci memset(&blend, 0, sizeof blend); 434bf215546Sopenharmony_ci blend.rt[0].colormask = PIPE_MASK_RGBA; 435bf215546Sopenharmony_ci handle = ctx->create_blend_state(ctx, &blend); 436bf215546Sopenharmony_ci ctx->bind_blend_state(ctx, handle); 437bf215546Sopenharmony_ci } 438bf215546Sopenharmony_ci 439bf215546Sopenharmony_ci { 440bf215546Sopenharmony_ci struct pipe_depth_stencil_alpha_state depthstencil; 441bf215546Sopenharmony_ci void *handle; 442bf215546Sopenharmony_ci memset(&depthstencil, 0, sizeof depthstencil); 443bf215546Sopenharmony_ci handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil); 444bf215546Sopenharmony_ci ctx->bind_depth_stencil_alpha_state(ctx, handle); 445bf215546Sopenharmony_ci } 446bf215546Sopenharmony_ci 447bf215546Sopenharmony_ci { 448bf215546Sopenharmony_ci struct pipe_rasterizer_state rasterizer; 449bf215546Sopenharmony_ci void *handle; 450bf215546Sopenharmony_ci memset(&rasterizer, 0, sizeof rasterizer); 451bf215546Sopenharmony_ci rasterizer.cull_face = PIPE_FACE_NONE; 452bf215546Sopenharmony_ci rasterizer.point_size = 8.0; 453bf215546Sopenharmony_ci rasterizer.half_pixel_center = 1; 454bf215546Sopenharmony_ci rasterizer.bottom_edge_rule = 1; 455bf215546Sopenharmony_ci rasterizer.depth_clip_near = 1; 456bf215546Sopenharmony_ci rasterizer.depth_clip_far = 1; 457bf215546Sopenharmony_ci handle = ctx->create_rasterizer_state(ctx, &rasterizer); 458bf215546Sopenharmony_ci ctx->bind_rasterizer_state(ctx, handle); 459bf215546Sopenharmony_ci } 460bf215546Sopenharmony_ci 461bf215546Sopenharmony_ci set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000); 462bf215546Sopenharmony_ci 463bf215546Sopenharmony_ci init_tex(); 464bf215546Sopenharmony_ci init_fs_constbuf(); 465bf215546Sopenharmony_ci 466bf215546Sopenharmony_ci set_vertices(); 467bf215546Sopenharmony_ci set_vertex_shader(); 468bf215546Sopenharmony_ci set_fragment_shader(); 469bf215546Sopenharmony_ci} 470bf215546Sopenharmony_ci 471bf215546Sopenharmony_cistatic void args(int argc, char *argv[]) 472bf215546Sopenharmony_ci{ 473bf215546Sopenharmony_ci int i; 474bf215546Sopenharmony_ci 475bf215546Sopenharmony_ci for (i = 1; i < argc;) { 476bf215546Sopenharmony_ci if (graw_parse_args(&i, argc, argv)) { 477bf215546Sopenharmony_ci continue; 478bf215546Sopenharmony_ci } 479bf215546Sopenharmony_ci if (strcmp(argv[i], "-fps") == 0) { 480bf215546Sopenharmony_ci show_fps = 1; 481bf215546Sopenharmony_ci i++; 482bf215546Sopenharmony_ci } 483bf215546Sopenharmony_ci else if (i == argc - 1) { 484bf215546Sopenharmony_ci filename = argv[i]; 485bf215546Sopenharmony_ci i++; 486bf215546Sopenharmony_ci } 487bf215546Sopenharmony_ci else { 488bf215546Sopenharmony_ci usage(argv[0]); 489bf215546Sopenharmony_ci exit(1); 490bf215546Sopenharmony_ci } 491bf215546Sopenharmony_ci } 492bf215546Sopenharmony_ci 493bf215546Sopenharmony_ci if (!filename) { 494bf215546Sopenharmony_ci usage(argv[0]); 495bf215546Sopenharmony_ci exit(1); 496bf215546Sopenharmony_ci } 497bf215546Sopenharmony_ci} 498bf215546Sopenharmony_ci 499bf215546Sopenharmony_ciint main( int argc, char *argv[] ) 500bf215546Sopenharmony_ci{ 501bf215546Sopenharmony_ci args(argc,argv); 502bf215546Sopenharmony_ci init(); 503bf215546Sopenharmony_ci 504bf215546Sopenharmony_ci graw_set_display_func( draw ); 505bf215546Sopenharmony_ci graw_main_loop(); 506bf215546Sopenharmony_ci return 0; 507bf215546Sopenharmony_ci} 508