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 "state_tracker/graw.h"
7 #include "pipe/p_screen.h"
8 #include "pipe/p_context.h"
9 #include "pipe/p_state.h"
10 #include "pipe/p_defines.h"
11
12 #include "util/u_memory.h" /* Offset() */
13 #include "util/u_draw_quad.h"
14 #include "util/u_inlines.h"
15
16 enum pipe_format formats[] = {
17 PIPE_FORMAT_R8G8B8A8_UNORM,
18 PIPE_FORMAT_B8G8R8A8_UNORM,
19 PIPE_FORMAT_NONE
20 };
21
22 static const int WIDTH = 300;
23 static const int HEIGHT = 300;
24
25 static struct pipe_screen *screen = NULL;
26 static struct pipe_context *ctx = NULL;
27 static struct pipe_surface *surf = NULL;
28 static struct pipe_resource *tex = NULL;
29 static void *window = NULL;
30
31 struct vertex {
32 float position[4];
33 float color[4];
34 };
35
36 static struct vertex vertices[4] =
37 {
38 { { 0.0f, -0.9f, 0.0f, 1.0f },
39 { 1.0f, 0.0f, 0.0f, 1.0f }
40 },
41 { { -0.9f, 0.9f, 0.0f, 1.0f },
42 { 0.0f, 1.0f, 0.0f, 1.0f }
43 },
44 { { 0.9f, 0.9f, 0.0f, 1.0f },
45 { 0.0f, 0.0f, 1.0f, 1.0f }
46 }
47 };
48
49
50
51
set_viewport(float x,float y,float width,float height,float near,float far)52 static void set_viewport( float x, float y,
53 float width, float height,
54 float near, float far)
55 {
56 float z = far;
57 float half_width = (float)width / 2.0f;
58 float half_height = (float)height / 2.0f;
59 float half_depth = ((float)far - (float)near) / 2.0f;
60 struct pipe_viewport_state vp;
61
62 vp.scale[0] = half_width;
63 vp.scale[1] = half_height;
64 vp.scale[2] = half_depth;
65 vp.scale[3] = 1.0f;
66
67 vp.translate[0] = half_width + x;
68 vp.translate[1] = half_height + y;
69 vp.translate[2] = half_depth + z;
70 vp.translate[3] = 0.0f;
71
72 ctx->set_viewport_state( ctx, &vp );
73 }
74
set_vertices(void)75 static void set_vertices( void )
76 {
77 struct pipe_vertex_element ve[2];
78 struct pipe_vertex_buffer vbuf;
79 void *handle;
80
81 memset(ve, 0, sizeof ve);
82
83 ve[0].src_offset = Offset(struct vertex, position);
84 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
85 ve[1].src_offset = Offset(struct vertex, color);
86 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
87
88 handle = ctx->create_vertex_elements_state(ctx, 2, ve);
89 ctx->bind_vertex_elements_state(ctx, handle);
90
91
92 vbuf.stride = sizeof( struct vertex );
93 vbuf.buffer_offset = 0;
94 vbuf.buffer = pipe_buffer_create_with_data(ctx,
95 PIPE_BIND_VERTEX_BUFFER,
96 PIPE_USAGE_STATIC,
97 sizeof(vertices),
98 vertices);
99
100 ctx->set_vertex_buffers(ctx, 1, &vbuf);
101 }
102
set_vertex_shader(void)103 static void set_vertex_shader( void )
104 {
105 void *handle;
106 const char *text =
107 "VERT\n"
108 "DCL IN[0]\n"
109 "DCL IN[1]\n"
110 "DCL OUT[0], POSITION\n"
111 "DCL OUT[1], COLOR\n"
112 " 0: MOV OUT[1], IN[1]\n"
113 " 1: MOV OUT[0], IN[0]\n"
114 " 2: END\n";
115
116 handle = graw_parse_vertex_shader(ctx, text);
117 ctx->bind_vs_state(ctx, handle);
118 }
119
set_fragment_shader(void)120 static void set_fragment_shader( void )
121 {
122 void *handle;
123 const char *text =
124 "FRAG\n"
125 "DCL IN[0], COLOR, LINEAR\n"
126 "DCL OUT[0], COLOR\n"
127 " 0: MOV OUT[0], IN[0]\n"
128 " 1: END\n";
129
130 handle = graw_parse_fragment_shader(ctx, text);
131 ctx->bind_fs_state(ctx, handle);
132 }
133
134
set_geometry_shader(void)135 static void set_geometry_shader( void )
136 {
137 void *handle;
138 const char *text =
139 "GEOM\n"
140 "PROPERTY GS_INPUT_PRIMITIVE TRIANGLES\n"
141 "PROPERTY GS_OUTPUT_PRIMITIVE TRIANGLE_STRIP\n"
142 "DCL IN[][0], POSITION, CONSTANT\n"
143 "DCL IN[][1], COLOR, CONSTANT\n"
144 "DCL OUT[0], POSITION, CONSTANT\n"
145 "DCL OUT[1], COLOR, CONSTANT\n"
146 "0:MOV OUT[0], IN[0][0]\n"
147 "1:MOV OUT[1], IN[0][1]\n"
148 "2:EMIT\n"
149 "3:MOV OUT[0], IN[1][0]\n"
150 "4:MOV OUT[1], IN[0][1]\n" /* copy color from input vertex 0 */
151 "5:EMIT\n"
152 "6:MOV OUT[0], IN[2][0]\n"
153 "7:MOV OUT[1], IN[2][1]\n"
154 "8:EMIT\n"
155 "9:ENDPRIM\n"
156 "10:END\n";
157
158 handle = graw_parse_geometry_shader(ctx, text);
159 ctx->bind_gs_state(ctx, handle);
160 }
161
draw(void)162 static void draw( void )
163 {
164 union pipe_color_union clear_color = { {1,0,1,1} };
165
166 ctx->clear(ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0);
167 util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
168 ctx->flush(ctx, NULL);
169
170 screen->flush_frontbuffer(screen, tex, 0, 0, window);
171 }
172
173
init(void)174 static void init( void )
175 {
176 struct pipe_framebuffer_state fb;
177 struct pipe_resource templat;
178 struct pipe_surface surf_tmpl;
179 int i;
180
181 /* It's hard to say whether window or screen should be created
182 * first. Different environments would prefer one or the other.
183 *
184 * Also, no easy way of querying supported formats if the screen
185 * cannot be created first.
186 */
187 for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
188 screen = graw_create_window_and_screen(0, 0, 300, 300,
189 formats[i],
190 &window);
191 if (window && screen)
192 break;
193 }
194 if (!screen || !window) {
195 fprintf(stderr, "Unable to create window\n");
196 exit(1);
197 }
198
199 ctx = screen->context_create(screen, NULL);
200 if (ctx == NULL)
201 exit(3);
202
203 templat.target = PIPE_TEXTURE_2D;
204 templat.format = formats[i];
205 templat.width0 = WIDTH;
206 templat.height0 = HEIGHT;
207 templat.depth0 = 1;
208 templat.array_size = 1;
209 templat.last_level = 0;
210 templat.nr_samples = 1;
211 templat.bind = (PIPE_BIND_RENDER_TARGET |
212 PIPE_BIND_DISPLAY_TARGET);
213
214 tex = screen->resource_create(screen,
215 &templat);
216 if (tex == NULL)
217 exit(4);
218
219 surf_tmpl.format = templat.format;
220 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
221 surf_tmpl.u.tex.level = 0;
222 surf_tmpl.u.tex.first_layer = 0;
223 surf_tmpl.u.tex.last_layer = 0;
224 surf = ctx->create_surface(ctx, tex, &surf_tmpl);
225 if (surf == NULL)
226 exit(5);
227
228 memset(&fb, 0, sizeof fb);
229 fb.nr_cbufs = 1;
230 fb.width = WIDTH;
231 fb.height = HEIGHT;
232 fb.cbufs[0] = surf;
233
234 ctx->set_framebuffer_state(ctx, &fb);
235
236 {
237 struct pipe_blend_state blend;
238 void *handle;
239 memset(&blend, 0, sizeof blend);
240 blend.rt[0].colormask = PIPE_MASK_RGBA;
241 handle = ctx->create_blend_state(ctx, &blend);
242 ctx->bind_blend_state(ctx, handle);
243 }
244
245 {
246 struct pipe_depth_stencil_alpha_state depthstencil;
247 void *handle;
248 memset(&depthstencil, 0, sizeof depthstencil);
249 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
250 ctx->bind_depth_stencil_alpha_state(ctx, handle);
251 }
252
253 {
254 struct pipe_rasterizer_state rasterizer;
255 void *handle;
256 memset(&rasterizer, 0, sizeof rasterizer);
257 rasterizer.cull_face = PIPE_FACE_NONE;
258 rasterizer.gl_rasterization_rules = 1;
259 rasterizer.depth_clip = 1;
260 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
261 ctx->bind_rasterizer_state(ctx, handle);
262 }
263
264 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
265 set_vertices();
266 set_vertex_shader();
267 set_fragment_shader();
268 set_geometry_shader();
269 }
270
271
main(int argc,char * argv[])272 int main( int argc, char *argv[] )
273 {
274 init();
275
276 graw_set_display_func( draw );
277 graw_main_loop();
278 return 0;
279 }
280