1 /* Display a cleared blue window. This demo has no dependencies on
2 * any utility code, just the graw interface and gallium.
5 #include "state_tracker/graw.h"
6 #include "pipe/p_screen.h"
7 #include "pipe/p_context.h"
8 #include "pipe/p_state.h"
9 #include "pipe/p_defines.h"
11 #include "util/u_debug.h" /* debug_dump_surface_bmp() */
12 #include "util/u_memory.h" /* Offset() */
14 enum pipe_format formats
[] = {
15 PIPE_FORMAT_R8G8B8A8_UNORM
,
16 PIPE_FORMAT_B8G8R8A8_UNORM
,
20 static const int WIDTH
= 300;
21 static const int HEIGHT
= 300;
23 static struct pipe_screen
*screen
= NULL
;
24 static struct pipe_context
*ctx
= NULL
;
25 static struct pipe_surface
*surf
= NULL
;
26 static void *window
= NULL
;
33 static struct vertex vertices
[4] =
35 { { 0.0f
, -0.9f
, 0.0f
, 1.0f
},
36 { 1.0f
, 0.0f
, 0.0f
, 1.0f
}
38 { { -0.9f
, 0.9f
, 0.0f
, 1.0f
},
39 { 0.0f
, 1.0f
, 0.0f
, 1.0f
}
41 { { 0.9f
, 0.9f
, 0.0f
, 1.0f
},
42 { 0.0f
, 0.0f
, 1.0f
, 1.0f
}
49 static void set_viewport( float x
, float y
,
50 float width
, float height
,
51 float near
, float far
)
54 float half_width
= (float)width
/ 2.0f
;
55 float half_height
= (float)height
/ 2.0f
;
56 float half_depth
= ((float)far
- (float)near
) / 2.0f
;
57 struct pipe_viewport_state vp
;
59 vp
.scale
[0] = half_width
;
60 vp
.scale
[1] = half_height
;
61 vp
.scale
[2] = half_depth
;
64 vp
.translate
[0] = half_width
+ x
;
65 vp
.translate
[1] = half_height
+ y
;
66 vp
.translate
[2] = half_depth
+ z
;
67 vp
.translate
[3] = 0.0f
;
69 ctx
->set_viewport_state( ctx
, &vp
);
72 static void set_vertices( void )
74 struct pipe_vertex_element ve
[2];
75 struct pipe_vertex_buffer vbuf
;
78 memset(ve
, 0, sizeof ve
);
80 ve
[0].src_offset
= Offset(struct vertex
, position
);
81 ve
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
82 ve
[1].src_offset
= Offset(struct vertex
, color
);
83 ve
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
85 handle
= ctx
->create_vertex_elements_state(ctx
, 2, ve
);
86 ctx
->bind_vertex_elements_state(ctx
, handle
);
89 vbuf
.stride
= sizeof( struct vertex
);
90 vbuf
.max_index
= sizeof(vertices
) / vbuf
.stride
;
91 vbuf
.buffer_offset
= 0;
92 vbuf
.buffer
= screen
->user_buffer_create(screen
,
95 PIPE_BIND_VERTEX_BUFFER
);
97 ctx
->set_vertex_buffers(ctx
, 1, &vbuf
);
100 static void set_vertex_shader( void )
107 "DCL OUT[0], POSITION\n"
108 "DCL OUT[1], COLOR\n"
109 " 0: MOV OUT[1], IN[1]\n"
110 " 1: MOV OUT[0], IN[0]\n"
113 handle
= graw_parse_vertex_shader(ctx
, text
);
114 ctx
->bind_vs_state(ctx
, handle
);
117 static void set_fragment_shader( void )
122 "DCL IN[0], COLOR, LINEAR\n"
123 "DCL OUT[0], COLOR\n"
124 " 0: MOV OUT[0], IN[0]\n"
127 handle
= graw_parse_fragment_shader(ctx
, text
);
128 ctx
->bind_fs_state(ctx
, handle
);
132 static void draw( void )
134 float clear_color
[4] = {1,0,1,1};
136 ctx
->clear(ctx
, PIPE_CLEAR_COLOR
, clear_color
, 0, 0);
137 ctx
->draw_arrays(ctx
, PIPE_PRIM_TRIANGLES
, 0, 3);
138 ctx
->flush(ctx
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
141 /* At the moment, libgraw leaks out/makes available some of the
142 * symbols from gallium/auxiliary, including these debug helpers.
143 * Will eventually want to bless some of these paths, and lock the
144 * others down so they aren't accessible from test programs.
146 * This currently just happens to work on debug builds - a release
147 * build will probably fail to link here:
149 debug_dump_surface_bmp(ctx
, "result.bmp", surf
);
152 screen
->flush_frontbuffer(screen
, surf
, window
);
156 static void init( void )
158 struct pipe_framebuffer_state fb
;
159 struct pipe_resource
*tex
, templat
;
162 /* It's hard to say whether window or screen should be created
163 * first. Different environments would prefer one or the other.
165 * Also, no easy way of querying supported formats if the screen
166 * cannot be created first.
169 window
== NULL
&& formats
[i
] != PIPE_FORMAT_NONE
;
172 screen
= graw_create_window_and_screen(0,0,300,300,
177 ctx
= screen
->context_create(screen
, NULL
);
181 templat
.target
= PIPE_TEXTURE_2D
;
182 templat
.format
= formats
[i
];
183 templat
.width0
= WIDTH
;
184 templat
.height0
= HEIGHT
;
186 templat
.last_level
= 0;
187 templat
.nr_samples
= 1;
188 templat
.bind
= (PIPE_BIND_RENDER_TARGET
|
189 PIPE_BIND_DISPLAY_TARGET
);
191 tex
= screen
->resource_create(screen
,
196 surf
= screen
->get_tex_surface(screen
, tex
, 0, 0, 0,
197 PIPE_BIND_RENDER_TARGET
|
198 PIPE_BIND_DISPLAY_TARGET
);
202 memset(&fb
, 0, sizeof fb
);
208 ctx
->set_framebuffer_state(ctx
, &fb
);
211 struct pipe_blend_state blend
;
213 memset(&blend
, 0, sizeof blend
);
214 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
215 handle
= ctx
->create_blend_state(ctx
, &blend
);
216 ctx
->bind_blend_state(ctx
, handle
);
220 struct pipe_depth_stencil_alpha_state depthstencil
;
222 memset(&depthstencil
, 0, sizeof depthstencil
);
223 handle
= ctx
->create_depth_stencil_alpha_state(ctx
, &depthstencil
);
224 ctx
->bind_depth_stencil_alpha_state(ctx
, handle
);
228 struct pipe_rasterizer_state rasterizer
;
230 memset(&rasterizer
, 0, sizeof rasterizer
);
231 rasterizer
.cull_face
= PIPE_FACE_NONE
;
232 rasterizer
.gl_rasterization_rules
= 1;
233 handle
= ctx
->create_rasterizer_state(ctx
, &rasterizer
);
234 ctx
->bind_rasterizer_state(ctx
, handle
);
237 set_viewport(0, 0, WIDTH
, HEIGHT
, 30, 1000);
240 set_fragment_shader();
244 int main( int argc
, char *argv
[] )
248 graw_set_display_func( draw
);