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