1bf215546Sopenharmony_ci/* Test the writing Z in fragment shader. 2bf215546Sopenharmony_ci * The red quad should be entirely in front of the blue quad even 3bf215546Sopenharmony_ci * though the overlap and intersect in Z. 4bf215546Sopenharmony_ci */ 5bf215546Sopenharmony_ci 6bf215546Sopenharmony_ci#include <stdio.h> 7bf215546Sopenharmony_ci 8bf215546Sopenharmony_ci#include "graw_util.h" 9bf215546Sopenharmony_ci 10bf215546Sopenharmony_ci#include "util/macros.h" 11bf215546Sopenharmony_ci 12bf215546Sopenharmony_ci 13bf215546Sopenharmony_cistatic int width = 300; 14bf215546Sopenharmony_cistatic int height = 300; 15bf215546Sopenharmony_ci 16bf215546Sopenharmony_cistatic struct graw_info info; 17bf215546Sopenharmony_ci 18bf215546Sopenharmony_ci 19bf215546Sopenharmony_cistruct vertex { 20bf215546Sopenharmony_ci float position[4]; 21bf215546Sopenharmony_ci float color[4]; 22bf215546Sopenharmony_ci}; 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#define z0 0.2 25bf215546Sopenharmony_ci#define z01 0.5 26bf215546Sopenharmony_ci#define z1 0.4 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_cistatic struct vertex vertices[] = 30bf215546Sopenharmony_ci{ 31bf215546Sopenharmony_ci /* left quad: clock-wise, front-facing, red */ 32bf215546Sopenharmony_ci { 33bf215546Sopenharmony_ci {-0.8, -0.9, z0, 1.0 }, 34bf215546Sopenharmony_ci { 1, 0, 0, 1 } 35bf215546Sopenharmony_ci }, 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci { 38bf215546Sopenharmony_ci { -0.2, -0.9, z0, 1.0 }, 39bf215546Sopenharmony_ci { 1, 0, 0, 1 } 40bf215546Sopenharmony_ci }, 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_ci { 43bf215546Sopenharmony_ci { 0.2, 0.9, z01, 1.0 }, 44bf215546Sopenharmony_ci { 1, 0, 0, 1 } 45bf215546Sopenharmony_ci }, 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ci { 48bf215546Sopenharmony_ci {-0.9, 0.9, z01, 1.0 }, 49bf215546Sopenharmony_ci { 1, 0, 0, 1 } 50bf215546Sopenharmony_ci }, 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci /* right quad : counter-clock-wise, back-facing, green */ 53bf215546Sopenharmony_ci { 54bf215546Sopenharmony_ci { 0.2, -0.9, z1, 1.0 }, 55bf215546Sopenharmony_ci { 0, 0, 1, -1 } 56bf215546Sopenharmony_ci }, 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci { 59bf215546Sopenharmony_ci { -0.2, 0.8, z1, 1.0 }, 60bf215546Sopenharmony_ci { 0, 0, 1, -1 } 61bf215546Sopenharmony_ci }, 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci { 64bf215546Sopenharmony_ci { 0.9, 0.8, z1, 1.0 }, 65bf215546Sopenharmony_ci { 0, 0, 1, -1 } 66bf215546Sopenharmony_ci }, 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci { 69bf215546Sopenharmony_ci { 0.8, -0.9, z1, 1.0 }, 70bf215546Sopenharmony_ci { 0, 0, 1, -1 } 71bf215546Sopenharmony_ci }, 72bf215546Sopenharmony_ci}; 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci#define NUM_VERTS ARRAY_SIZE(vertices) 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_cistatic void 79bf215546Sopenharmony_ciset_vertices(void) 80bf215546Sopenharmony_ci{ 81bf215546Sopenharmony_ci struct pipe_vertex_element ve[2]; 82bf215546Sopenharmony_ci struct pipe_vertex_buffer vbuf; 83bf215546Sopenharmony_ci void *handle; 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci memset(ve, 0, sizeof ve); 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ci ve[0].src_offset = Offset(struct vertex, position); 88bf215546Sopenharmony_ci ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 89bf215546Sopenharmony_ci ve[1].src_offset = Offset(struct vertex, color); 90bf215546Sopenharmony_ci ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve); 93bf215546Sopenharmony_ci info.ctx->bind_vertex_elements_state(info.ctx, handle); 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci memset(&vbuf, 0, sizeof vbuf); 96bf215546Sopenharmony_ci 97bf215546Sopenharmony_ci vbuf.stride = sizeof(struct vertex); 98bf215546Sopenharmony_ci vbuf.buffer_offset = 0; 99bf215546Sopenharmony_ci vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx, 100bf215546Sopenharmony_ci PIPE_BIND_VERTEX_BUFFER, 101bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 102bf215546Sopenharmony_ci sizeof(vertices), 103bf215546Sopenharmony_ci vertices); 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf); 106bf215546Sopenharmony_ci} 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_cistatic void 110bf215546Sopenharmony_ciset_vertex_shader(void) 111bf215546Sopenharmony_ci{ 112bf215546Sopenharmony_ci void *handle; 113bf215546Sopenharmony_ci const char *text = 114bf215546Sopenharmony_ci "VERT\n" 115bf215546Sopenharmony_ci "DCL IN[0]\n" 116bf215546Sopenharmony_ci "DCL IN[1]\n" 117bf215546Sopenharmony_ci "DCL OUT[0], POSITION\n" 118bf215546Sopenharmony_ci "DCL OUT[1], GENERIC[0]\n" 119bf215546Sopenharmony_ci " 0: MOV OUT[0], IN[0]\n" 120bf215546Sopenharmony_ci " 1: MOV OUT[1], IN[1]\n" 121bf215546Sopenharmony_ci " 2: END\n"; 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_ci handle = graw_parse_vertex_shader(info.ctx, text); 124bf215546Sopenharmony_ci info.ctx->bind_vs_state(info.ctx, handle); 125bf215546Sopenharmony_ci} 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci 128bf215546Sopenharmony_cistatic void 129bf215546Sopenharmony_ciset_fragment_shader(void) 130bf215546Sopenharmony_ci{ 131bf215546Sopenharmony_ci void *handle; 132bf215546Sopenharmony_ci const char *text = 133bf215546Sopenharmony_ci "FRAG\n" 134bf215546Sopenharmony_ci "DCL IN[0], GENERIC, CONSTANT\n" 135bf215546Sopenharmony_ci "DCL OUT[0], COLOR\n" 136bf215546Sopenharmony_ci "DCL OUT[1], POSITION\n" 137bf215546Sopenharmony_ci "DCL TEMP[0]\n" 138bf215546Sopenharmony_ci "IMM FLT32 { 1.0, 0.0, 0.0, 0.0 }\n" 139bf215546Sopenharmony_ci "IMM FLT32 { 0.0, 1.0, 0.0, 0.0 }\n" 140bf215546Sopenharmony_ci "IMM FLT32 { 0.5, 0.4, 0.0, 0.0 }\n" 141bf215546Sopenharmony_ci " 0: MOV OUT[0], IN[0]\n" /* front-facing: red */ 142bf215546Sopenharmony_ci " 1: IF IN[0].xxxx :3\n" 143bf215546Sopenharmony_ci " 2: MOV OUT[1].z, IMM[2].yyyy\n" /* red: Z = 0.4 */ 144bf215546Sopenharmony_ci " 3: ELSE :5\n" 145bf215546Sopenharmony_ci " 4: MOV OUT[1].z, IMM[2].xxxx\n" /* blue: Z = 0.5 */ 146bf215546Sopenharmony_ci " 5: ENDIF\n" 147bf215546Sopenharmony_ci " 6: END\n"; 148bf215546Sopenharmony_ci 149bf215546Sopenharmony_ci handle = graw_parse_fragment_shader(info.ctx, text); 150bf215546Sopenharmony_ci info.ctx->bind_fs_state(info.ctx, handle); 151bf215546Sopenharmony_ci} 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_cistatic void 156bf215546Sopenharmony_cidraw(void) 157bf215546Sopenharmony_ci{ 158bf215546Sopenharmony_ci union pipe_color_union clear_color; 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci clear_color.f[0] = 0.25; 161bf215546Sopenharmony_ci clear_color.f[1] = 0.25; 162bf215546Sopenharmony_ci clear_color.f[2] = 0.25; 163bf215546Sopenharmony_ci clear_color.f[3] = 1.00; 164bf215546Sopenharmony_ci 165bf215546Sopenharmony_ci info.ctx->clear(info.ctx, 166bf215546Sopenharmony_ci PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL, 167bf215546Sopenharmony_ci NULL, 168bf215546Sopenharmony_ci &clear_color, 1.0, 0); 169bf215546Sopenharmony_ci util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS); 170bf215546Sopenharmony_ci info.ctx->flush(info.ctx, NULL, 0); 171bf215546Sopenharmony_ci 172bf215546Sopenharmony_ci#if 0 173bf215546Sopenharmony_ci /* At the moment, libgraw leaks out/makes available some of the 174bf215546Sopenharmony_ci * symbols from gallium/auxiliary, including these debug helpers. 175bf215546Sopenharmony_ci * Will eventually want to bless some of these paths, and lock the 176bf215546Sopenharmony_ci * others down so they aren't accessible from test programs. 177bf215546Sopenharmony_ci * 178bf215546Sopenharmony_ci * This currently just happens to work on debug builds - a release 179bf215546Sopenharmony_ci * build will probably fail to link here: 180bf215546Sopenharmony_ci */ 181bf215546Sopenharmony_ci debug_dump_surface_bmp(info.ctx, "result.bmp", surf); 182bf215546Sopenharmony_ci#endif 183bf215546Sopenharmony_ci 184bf215546Sopenharmony_ci graw_util_flush_front(&info); 185bf215546Sopenharmony_ci} 186bf215546Sopenharmony_ci 187bf215546Sopenharmony_ci 188bf215546Sopenharmony_ci#if 0 189bf215546Sopenharmony_cistatic void 190bf215546Sopenharmony_ciresize(int w, int h) 191bf215546Sopenharmony_ci{ 192bf215546Sopenharmony_ci width = w; 193bf215546Sopenharmony_ci height = h; 194bf215546Sopenharmony_ci 195bf215546Sopenharmony_ci graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0); 196bf215546Sopenharmony_ci} 197bf215546Sopenharmony_ci#endif 198bf215546Sopenharmony_ci 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_cistatic void 201bf215546Sopenharmony_ciinit(void) 202bf215546Sopenharmony_ci{ 203bf215546Sopenharmony_ci if (!graw_util_create_window(&info, width, height, 1, TRUE)) 204bf215546Sopenharmony_ci exit(1); 205bf215546Sopenharmony_ci 206bf215546Sopenharmony_ci graw_util_default_state(&info, TRUE); 207bf215546Sopenharmony_ci 208bf215546Sopenharmony_ci graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0); 209bf215546Sopenharmony_ci 210bf215546Sopenharmony_ci set_vertices(); 211bf215546Sopenharmony_ci set_vertex_shader(); 212bf215546Sopenharmony_ci set_fragment_shader(); 213bf215546Sopenharmony_ci} 214bf215546Sopenharmony_ci 215bf215546Sopenharmony_ci 216bf215546Sopenharmony_ciint 217bf215546Sopenharmony_cimain(int argc, char *argv[]) 218bf215546Sopenharmony_ci{ 219bf215546Sopenharmony_ci init(); 220bf215546Sopenharmony_ci 221bf215546Sopenharmony_ci printf("The red quad should be entirely in front of the blue quad.\n"); 222bf215546Sopenharmony_ci 223bf215546Sopenharmony_ci graw_set_display_func(draw); 224bf215546Sopenharmony_ci /*graw_set_reshape_func(resize);*/ 225bf215546Sopenharmony_ci graw_main_loop(); 226bf215546Sopenharmony_ci return 0; 227bf215546Sopenharmony_ci} 228