1bf215546Sopenharmony_ci/* Test texture swizzles */ 2bf215546Sopenharmony_ci 3bf215546Sopenharmony_ci#include <stdio.h> 4bf215546Sopenharmony_ci 5bf215546Sopenharmony_ci#include "graw_util.h" 6bf215546Sopenharmony_ci 7bf215546Sopenharmony_ci 8bf215546Sopenharmony_cistatic struct graw_info info; 9bf215546Sopenharmony_ci 10bf215546Sopenharmony_cistatic struct pipe_resource *texture = NULL; 11bf215546Sopenharmony_cistatic struct pipe_sampler_view *sv = NULL; 12bf215546Sopenharmony_cistatic void *sampler = NULL; 13bf215546Sopenharmony_ci 14bf215546Sopenharmony_cistatic const int WIDTH = 300; 15bf215546Sopenharmony_cistatic const int HEIGHT = 300; 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_cistatic void set_vertices(void) 39bf215546Sopenharmony_ci{ 40bf215546Sopenharmony_ci struct pipe_vertex_element ve[2]; 41bf215546Sopenharmony_ci struct pipe_vertex_buffer vbuf; 42bf215546Sopenharmony_ci void *handle; 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci memset(ve, 0, sizeof ve); 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci ve[0].src_offset = Offset(struct vertex, position); 47bf215546Sopenharmony_ci ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 48bf215546Sopenharmony_ci ve[1].src_offset = Offset(struct vertex, color); 49bf215546Sopenharmony_ci ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_ci handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve); 52bf215546Sopenharmony_ci info.ctx->bind_vertex_elements_state(info.ctx, handle); 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci memset(&vbuf, 0, sizeof vbuf); 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci vbuf.stride = sizeof(struct vertex); 57bf215546Sopenharmony_ci vbuf.buffer_offset = 0; 58bf215546Sopenharmony_ci vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx, 59bf215546Sopenharmony_ci PIPE_BIND_VERTEX_BUFFER, 60bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 61bf215546Sopenharmony_ci sizeof(vertices), 62bf215546Sopenharmony_ci vertices); 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf); 65bf215546Sopenharmony_ci} 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_cistatic void set_vertex_shader(void) 68bf215546Sopenharmony_ci{ 69bf215546Sopenharmony_ci void *handle; 70bf215546Sopenharmony_ci const char *text = 71bf215546Sopenharmony_ci "VERT\n" 72bf215546Sopenharmony_ci "DCL IN[0]\n" 73bf215546Sopenharmony_ci "DCL IN[1]\n" 74bf215546Sopenharmony_ci "DCL OUT[0], POSITION\n" 75bf215546Sopenharmony_ci "DCL OUT[1], GENERIC[0]\n" 76bf215546Sopenharmony_ci " 0: MOV OUT[1], IN[1]\n" 77bf215546Sopenharmony_ci " 1: MOV OUT[0], IN[0]\n" 78bf215546Sopenharmony_ci " 2: END\n"; 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci handle = graw_parse_vertex_shader(info.ctx, text); 81bf215546Sopenharmony_ci info.ctx->bind_vs_state(info.ctx, handle); 82bf215546Sopenharmony_ci} 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_cistatic void set_fragment_shader(void) 85bf215546Sopenharmony_ci{ 86bf215546Sopenharmony_ci void *handle; 87bf215546Sopenharmony_ci const char *text = 88bf215546Sopenharmony_ci "FRAG\n" 89bf215546Sopenharmony_ci "DCL IN[0], GENERIC[0], PERSPECTIVE\n" 90bf215546Sopenharmony_ci "DCL OUT[0], COLOR\n" 91bf215546Sopenharmony_ci "DCL SAMP[0]\n" 92bf215546Sopenharmony_ci "DCL SVIEW[0], 2D, FLOAT\n" 93bf215546Sopenharmony_ci " 0: TXP OUT[0], IN[0], SAMP[0], 2D\n" 94bf215546Sopenharmony_ci " 2: END\n"; 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ci handle = graw_parse_fragment_shader(info.ctx, text); 97bf215546Sopenharmony_ci info.ctx->bind_fs_state(info.ctx, handle); 98bf215546Sopenharmony_ci} 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_cistatic void draw(void) 102bf215546Sopenharmony_ci{ 103bf215546Sopenharmony_ci union pipe_color_union clear_color; 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci clear_color.f[0] = 0.5; 106bf215546Sopenharmony_ci clear_color.f[1] = 0.5; 107bf215546Sopenharmony_ci clear_color.f[2] = 0.5; 108bf215546Sopenharmony_ci clear_color.f[3] = 1.0; 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_ci info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0); 111bf215546Sopenharmony_ci util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, 4); 112bf215546Sopenharmony_ci info.ctx->flush(info.ctx, NULL, 0); 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci graw_util_flush_front(&info); 115bf215546Sopenharmony_ci} 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_cistatic void 120bf215546Sopenharmony_ciinit_tex(const unsigned swizzle[4]) 121bf215546Sopenharmony_ci{ 122bf215546Sopenharmony_ci#define SIZE 256 123bf215546Sopenharmony_ci struct pipe_sampler_view sv_template; 124bf215546Sopenharmony_ci ubyte tex2d[SIZE][SIZE][4]; 125bf215546Sopenharmony_ci int s, t; 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci for (s = 0; s < SIZE; s++) { 128bf215546Sopenharmony_ci for (t = 0; t < SIZE; t++) { 129bf215546Sopenharmony_ci tex2d[t][s][0] = 0; /*B*/ 130bf215546Sopenharmony_ci tex2d[t][s][1] = t; /*G*/ 131bf215546Sopenharmony_ci tex2d[t][s][2] = s; /*R*/ 132bf215546Sopenharmony_ci tex2d[t][s][3] = 1; /*A*/ 133bf215546Sopenharmony_ci } 134bf215546Sopenharmony_ci } 135bf215546Sopenharmony_ci 136bf215546Sopenharmony_ci texture = graw_util_create_tex2d(&info, SIZE, SIZE, 137bf215546Sopenharmony_ci PIPE_FORMAT_B8G8R8A8_UNORM, tex2d); 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_ci memset(&sv_template, 0, sizeof sv_template); 140bf215546Sopenharmony_ci sv_template.format = texture->format; 141bf215546Sopenharmony_ci sv_template.texture = texture; 142bf215546Sopenharmony_ci sv_template.swizzle_r = swizzle[0]; 143bf215546Sopenharmony_ci sv_template.swizzle_g = swizzle[1]; 144bf215546Sopenharmony_ci sv_template.swizzle_b = swizzle[2]; 145bf215546Sopenharmony_ci sv_template.swizzle_a = swizzle[3]; 146bf215546Sopenharmony_ci sv = info.ctx->create_sampler_view(info.ctx, texture, &sv_template); 147bf215546Sopenharmony_ci if (sv == NULL) 148bf215546Sopenharmony_ci exit(5); 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ci info.ctx->set_sampler_views(info.ctx, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &sv); 151bf215546Sopenharmony_ci 152bf215546Sopenharmony_ci sampler = graw_util_create_simple_sampler(&info, 153bf215546Sopenharmony_ci PIPE_TEX_WRAP_REPEAT, 154bf215546Sopenharmony_ci PIPE_TEX_FILTER_NEAREST); 155bf215546Sopenharmony_ci 156bf215546Sopenharmony_ci info.ctx->bind_sampler_states(info.ctx, PIPE_SHADER_FRAGMENT, 157bf215546Sopenharmony_ci 0, 1, &sampler); 158bf215546Sopenharmony_ci#undef SIZE 159bf215546Sopenharmony_ci} 160bf215546Sopenharmony_ci 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_cistatic void 163bf215546Sopenharmony_ciinit(const unsigned swizzle[4]) 164bf215546Sopenharmony_ci{ 165bf215546Sopenharmony_ci if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE)) 166bf215546Sopenharmony_ci exit(1); 167bf215546Sopenharmony_ci 168bf215546Sopenharmony_ci graw_util_default_state(&info, FALSE); 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 10000); 171bf215546Sopenharmony_ci 172bf215546Sopenharmony_ci init_tex(swizzle); 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci set_vertices(); 175bf215546Sopenharmony_ci set_vertex_shader(); 176bf215546Sopenharmony_ci set_fragment_shader(); 177bf215546Sopenharmony_ci} 178bf215546Sopenharmony_ci 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_cistatic unsigned 181bf215546Sopenharmony_cichar_to_swizzle(char c) 182bf215546Sopenharmony_ci{ 183bf215546Sopenharmony_ci switch (c) { 184bf215546Sopenharmony_ci case 'r': 185bf215546Sopenharmony_ci return PIPE_SWIZZLE_X; 186bf215546Sopenharmony_ci case 'g': 187bf215546Sopenharmony_ci return PIPE_SWIZZLE_Y; 188bf215546Sopenharmony_ci case 'b': 189bf215546Sopenharmony_ci return PIPE_SWIZZLE_Z; 190bf215546Sopenharmony_ci case 'a': 191bf215546Sopenharmony_ci return PIPE_SWIZZLE_W; 192bf215546Sopenharmony_ci case '0': 193bf215546Sopenharmony_ci return PIPE_SWIZZLE_0; 194bf215546Sopenharmony_ci case '1': 195bf215546Sopenharmony_ci return PIPE_SWIZZLE_1; 196bf215546Sopenharmony_ci default: 197bf215546Sopenharmony_ci return PIPE_SWIZZLE_X; 198bf215546Sopenharmony_ci } 199bf215546Sopenharmony_ci} 200bf215546Sopenharmony_ci 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_ciint main(int argc, char *argv[]) 203bf215546Sopenharmony_ci{ 204bf215546Sopenharmony_ci const char swizzle_names[] = "rgba01"; 205bf215546Sopenharmony_ci uint swizzle[4]; 206bf215546Sopenharmony_ci int i; 207bf215546Sopenharmony_ci 208bf215546Sopenharmony_ci swizzle[0] = PIPE_SWIZZLE_X; 209bf215546Sopenharmony_ci swizzle[1] = PIPE_SWIZZLE_Y; 210bf215546Sopenharmony_ci swizzle[2] = PIPE_SWIZZLE_Z; 211bf215546Sopenharmony_ci swizzle[3] = PIPE_SWIZZLE_W; 212bf215546Sopenharmony_ci 213bf215546Sopenharmony_ci for (i = 1; i < argc; i++) { 214bf215546Sopenharmony_ci swizzle[i-1] = char_to_swizzle(argv[i][0]); 215bf215546Sopenharmony_ci } 216bf215546Sopenharmony_ci 217bf215546Sopenharmony_ci printf("Example:\n"); 218bf215546Sopenharmony_ci printf(" tex-swizzle r 0 g 1\n"); 219bf215546Sopenharmony_ci printf("Current swizzle = "); 220bf215546Sopenharmony_ci for (i = 0; i < 4; i++) { 221bf215546Sopenharmony_ci printf("%c", swizzle_names[swizzle[i]]); 222bf215546Sopenharmony_ci } 223bf215546Sopenharmony_ci printf("\n"); 224bf215546Sopenharmony_ci 225bf215546Sopenharmony_ci init(swizzle); 226bf215546Sopenharmony_ci 227bf215546Sopenharmony_ci graw_set_display_func(draw); 228bf215546Sopenharmony_ci graw_main_loop(); 229bf215546Sopenharmony_ci return 0; 230bf215546Sopenharmony_ci} 231