1af69d88dSmrg/* Display a huge triangle on a 8192x8192 canvas. 2af69d88dSmrg * This demo has no dependencies on any utility code, 3af69d88dSmrg * just the graw interface and gallium. 4af69d88dSmrg */ 5af69d88dSmrg 6af69d88dSmrg#include "graw_util.h" 7af69d88dSmrg#include "util/u_debug.h" 8af69d88dSmrg 9af69d88dSmrg#include <stdio.h> 10af69d88dSmrg 11af69d88dSmrgstatic struct graw_info info; 12af69d88dSmrg 13af69d88dSmrgstatic const int WIDTH = 4*2048; 14af69d88dSmrgstatic const int HEIGHT = 4*2048; 15af69d88dSmrg 16af69d88dSmrg 17af69d88dSmrgstruct vertex { 18af69d88dSmrg float position[4]; 19af69d88dSmrg float color[4]; 20af69d88dSmrg}; 21af69d88dSmrg 22af69d88dSmrgstatic boolean FlatShade = FALSE; 23af69d88dSmrg 24af69d88dSmrg 25af69d88dSmrgstatic struct vertex vertices[3] = 26af69d88dSmrg{ 27af69d88dSmrg { 28af69d88dSmrg { -1.0f, -1.0f, 0.0f, 1.0f }, 29af69d88dSmrg { 1.0f, 0.0f, 0.0f, 1.0f } 30af69d88dSmrg }, 31af69d88dSmrg { 32af69d88dSmrg { -1.0f, 1.0f, 0.0f, 1.0f }, 33af69d88dSmrg { 0.0f, 1.0f, 0.0f, 1.0f } 34af69d88dSmrg }, 35af69d88dSmrg { 36af69d88dSmrg { 1.0f, 1.0f, 0.0f, 1.0f }, 37af69d88dSmrg { 0.0f, 0.0f, 1.0f, 1.0f } 38af69d88dSmrg } 39af69d88dSmrg}; 40af69d88dSmrg 41af69d88dSmrg 42af69d88dSmrgstatic void set_vertices( void ) 43af69d88dSmrg{ 44af69d88dSmrg struct pipe_vertex_element ve[2]; 45af69d88dSmrg struct pipe_vertex_buffer vbuf; 46af69d88dSmrg void *handle; 47af69d88dSmrg 48af69d88dSmrg memset(ve, 0, sizeof ve); 49af69d88dSmrg 50af69d88dSmrg ve[0].src_offset = Offset(struct vertex, position); 51af69d88dSmrg ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 52af69d88dSmrg ve[1].src_offset = Offset(struct vertex, color); 53af69d88dSmrg ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 54af69d88dSmrg 55af69d88dSmrg handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve); 56af69d88dSmrg info.ctx->bind_vertex_elements_state(info.ctx, handle); 57af69d88dSmrg 58af69d88dSmrg memset(&vbuf, 0, sizeof vbuf); 59af69d88dSmrg 60af69d88dSmrg vbuf.stride = sizeof( struct vertex ); 61af69d88dSmrg vbuf.buffer_offset = 0; 6201e04c3fSmrg vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx, 63af69d88dSmrg PIPE_BIND_VERTEX_BUFFER, 64af69d88dSmrg PIPE_USAGE_DEFAULT, 65af69d88dSmrg sizeof(vertices), 66af69d88dSmrg vertices); 67af69d88dSmrg 687ec681f3Smrg info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf); 69af69d88dSmrg} 70af69d88dSmrg 71af69d88dSmrg 72af69d88dSmrgstatic void set_vertex_shader( void ) 73af69d88dSmrg{ 74af69d88dSmrg void *handle; 75af69d88dSmrg const char *text = 76af69d88dSmrg "VERT\n" 77af69d88dSmrg "DCL IN[0]\n" 78af69d88dSmrg "DCL IN[1]\n" 79af69d88dSmrg "DCL OUT[0], POSITION\n" 80af69d88dSmrg "DCL OUT[1], COLOR\n" 81af69d88dSmrg " 0: MOV OUT[1], IN[1]\n" 82af69d88dSmrg " 1: MOV OUT[0], IN[0]\n" 83af69d88dSmrg " 2: END\n"; 84af69d88dSmrg 85af69d88dSmrg handle = graw_parse_vertex_shader(info.ctx, text); 86af69d88dSmrg info.ctx->bind_vs_state(info.ctx, handle); 87af69d88dSmrg} 88af69d88dSmrg 89af69d88dSmrg 90af69d88dSmrgstatic void set_fragment_shader( void ) 91af69d88dSmrg{ 92af69d88dSmrg void *handle; 93af69d88dSmrg const char *text = 94af69d88dSmrg "FRAG\n" 95af69d88dSmrg "DCL IN[0], COLOR, LINEAR\n" 96af69d88dSmrg "DCL OUT[0], COLOR\n" 97af69d88dSmrg " 0: MOV OUT[0], IN[0]\n" 98af69d88dSmrg " 1: END\n"; 99af69d88dSmrg 100af69d88dSmrg handle = graw_parse_fragment_shader(info.ctx, text); 101af69d88dSmrg info.ctx->bind_fs_state(info.ctx, handle); 102af69d88dSmrg} 103af69d88dSmrg 104af69d88dSmrg 105af69d88dSmrgstatic void draw( void ) 106af69d88dSmrg{ 107af69d88dSmrg union pipe_color_union clear_color = { {1,0,1,1} }; 108af69d88dSmrg 1097ec681f3Smrg info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0); 110af69d88dSmrg util_draw_arrays(info.ctx, PIPE_PRIM_TRIANGLES, 0, 3); 111af69d88dSmrg info.ctx->flush(info.ctx, NULL, 0); 112af69d88dSmrg 113af69d88dSmrg graw_save_surface_to_file(info.ctx, info.color_surf[0], NULL); 114af69d88dSmrg 115af69d88dSmrg graw_util_flush_front(&info); 116af69d88dSmrg} 117af69d88dSmrg 118af69d88dSmrg 119af69d88dSmrgstatic void init( void ) 120af69d88dSmrg{ 121af69d88dSmrg if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE)) 122af69d88dSmrg exit(1); 123af69d88dSmrg 124af69d88dSmrg graw_util_default_state(&info, FALSE); 125af69d88dSmrg 126af69d88dSmrg { 127af69d88dSmrg struct pipe_rasterizer_state rasterizer; 128af69d88dSmrg void *handle; 129af69d88dSmrg memset(&rasterizer, 0, sizeof rasterizer); 130af69d88dSmrg rasterizer.cull_face = PIPE_FACE_NONE; 131af69d88dSmrg rasterizer.half_pixel_center = 1; 132af69d88dSmrg rasterizer.bottom_edge_rule = 1; 133af69d88dSmrg rasterizer.flatshade = FlatShade; 13401e04c3fSmrg rasterizer.depth_clip_near = 1; 13501e04c3fSmrg rasterizer.depth_clip_far = 1; 136af69d88dSmrg handle = info.ctx->create_rasterizer_state(info.ctx, &rasterizer); 137af69d88dSmrg info.ctx->bind_rasterizer_state(info.ctx, handle); 138af69d88dSmrg } 139af69d88dSmrg 140af69d88dSmrg 141af69d88dSmrg graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 1000); 142af69d88dSmrg 143af69d88dSmrg set_vertices(); 144af69d88dSmrg set_vertex_shader(); 145af69d88dSmrg set_fragment_shader(); 146af69d88dSmrg} 147af69d88dSmrg 148af69d88dSmrgstatic void args(int argc, char *argv[]) 149af69d88dSmrg{ 150af69d88dSmrg int i; 151af69d88dSmrg 152af69d88dSmrg for (i = 1; i < argc; ) { 153af69d88dSmrg if (graw_parse_args(&i, argc, argv)) { 154af69d88dSmrg /* ok */ 155af69d88dSmrg } 156af69d88dSmrg else if (strcmp(argv[i], "-f") == 0) { 157af69d88dSmrg FlatShade = TRUE; 158af69d88dSmrg i++; 159af69d88dSmrg } 160af69d88dSmrg else { 161af69d88dSmrg printf("Invalid arg %s\n", argv[i]); 162af69d88dSmrg exit(1); 163af69d88dSmrg } 164af69d88dSmrg } 165af69d88dSmrg} 166af69d88dSmrg 167af69d88dSmrgint main( int argc, char *argv[] ) 168af69d88dSmrg{ 169af69d88dSmrg args(argc, argv); 170af69d88dSmrg init(); 171af69d88dSmrg 172af69d88dSmrg graw_set_display_func( draw ); 173af69d88dSmrg graw_main_loop(); 174af69d88dSmrg return 0; 175af69d88dSmrg} 176