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