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