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