1 /**************************************************************************
3 * Copyright © 2010 Jakob Bornecrantz
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
24 **************************************************************************/
34 /* pipe_*_state structs */
35 #include "pipe/p_state.h"
37 #include "pipe/p_context.h"
39 #include "pipe/p_screen.h"
41 #include "pipe/p_defines.h"
42 /* TGSI_SEMANTIC_{POSITION|GENERIC} */
43 #include "pipe/p_shader_tokens.h"
44 /* pipe_buffer_* helpers */
45 #include "util/u_inlines.h"
47 /* constant state object helper */
48 #include "cso_cache/cso_context.h"
50 /* u_sampler_view_default_template */
51 #include "util/u_sampler.h"
52 /* debug_dump_surface_bmp */
53 #include "util/u_debug.h"
54 /* util_draw_vertex_buffer helper */
55 #include "util/u_draw_quad.h"
56 /* FREE & CALLOC_STRUCT */
57 #include "util/u_memory.h"
58 /* util_make_[fragment|vertex]_passthrough_shader */
59 #include "util/u_simple_shaders.h"
61 /* sw_screen_create: to get a software pipe driver */
62 #include "target-helpers/inline_sw_helper.h"
63 /* debug_screen_wrap: to wrap with debug pipe drivers */
64 #include "target-helpers/inline_debug_helper.h"
65 /* null software winsys */
66 #include "sw/null/null_sw_winsys.h"
70 struct pipe_screen
*screen
;
71 struct pipe_context
*pipe
;
72 struct cso_context
*cso
;
74 struct pipe_blend_state blend
;
75 struct pipe_depth_stencil_alpha_state depthstencil
;
76 struct pipe_rasterizer_state rasterizer
;
77 struct pipe_sampler_state sampler
;
78 struct pipe_viewport_state viewport
;
79 struct pipe_framebuffer_state framebuffer
;
80 struct pipe_vertex_element velem
[2];
85 union pipe_color_union clear_color
;
87 struct pipe_resource
*vbuf
;
88 struct pipe_resource
*target
;
89 struct pipe_resource
*tex
;
90 struct pipe_sampler_view
*view
;
93 static void init_prog(struct program
*p
)
95 struct pipe_surface surf_tmpl
;
96 /* create the software rasterizer */
97 p
->screen
= sw_screen_create(null_sw_create());
98 /* wrap the screen with any debugger */
99 p
->screen
= debug_screen_wrap(p
->screen
);
101 /* create the pipe driver context and cso context */
102 p
->pipe
= p
->screen
->context_create(p
->screen
, NULL
);
103 p
->cso
= cso_create_context(p
->pipe
);
105 /* set clear color */
106 p
->clear_color
.f
[0] = 0.3;
107 p
->clear_color
.f
[1] = 0.1;
108 p
->clear_color
.f
[2] = 0.3;
109 p
->clear_color
.f
[3] = 1.0;
113 float vertices
[4][2][4] = {
115 { 0.9f
, 0.9f
, 0.0f
, 1.0f
},
116 { 1.0f
, 1.0f
, 0.0f
, 1.0f
}
119 { -0.9f
, 0.9f
, 0.0f
, 1.0f
},
120 { 0.0f
, 1.0f
, 0.0f
, 1.0f
}
123 { -0.9f
, -0.9f
, 0.0f
, 1.0f
},
124 { 0.0f
, 0.0f
, 1.0f
, 1.0f
}
127 { 0.9f
, -0.9f
, 0.0f
, 1.0f
},
128 { 1.0f
, 0.0f
, 1.0f
, 1.0f
}
132 p
->vbuf
= pipe_buffer_create(p
->screen
, PIPE_BIND_VERTEX_BUFFER
,
133 PIPE_USAGE_STATIC
, sizeof(vertices
));
134 pipe_buffer_write(p
->pipe
, p
->vbuf
, 0, sizeof(vertices
), vertices
);
137 /* render target texture */
139 struct pipe_resource tmplt
;
140 memset(&tmplt
, 0, sizeof(tmplt
));
141 tmplt
.target
= PIPE_TEXTURE_2D
;
142 tmplt
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
; /* All drivers support this */
143 tmplt
.width0
= WIDTH
;
144 tmplt
.height0
= HEIGHT
;
146 tmplt
.array_size
= 1;
147 tmplt
.last_level
= 0;
148 tmplt
.bind
= PIPE_BIND_RENDER_TARGET
;
150 p
->target
= p
->screen
->resource_create(p
->screen
, &tmplt
);
153 /* sampler texture */
156 struct pipe_transfer
*t
;
157 struct pipe_resource t_tmplt
;
158 struct pipe_sampler_view v_tmplt
;
161 memset(&t_tmplt
, 0, sizeof(t_tmplt
));
162 t_tmplt
.target
= PIPE_TEXTURE_2D
;
163 t_tmplt
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
; /* All drivers support this */
167 t_tmplt
.array_size
= 1;
168 t_tmplt
.last_level
= 0;
169 t_tmplt
.bind
= PIPE_BIND_RENDER_TARGET
;
171 p
->tex
= p
->screen
->resource_create(p
->screen
, &t_tmplt
);
173 memset(&box
, 0, sizeof(box
));
177 t
= p
->pipe
->get_transfer(p
->pipe
, p
->tex
, 0, PIPE_TRANSFER_WRITE
, &box
);
179 ptr
= p
->pipe
->transfer_map(p
->pipe
, t
);
184 p
->pipe
->transfer_unmap(p
->pipe
, t
);
186 p
->pipe
->transfer_destroy(p
->pipe
, t
);
188 u_sampler_view_default_template(&v_tmplt
, p
->tex
, p
->tex
->format
);
190 p
->view
= p
->pipe
->create_sampler_view(p
->pipe
, p
->tex
, &v_tmplt
);
193 /* disabled blending/masking */
194 memset(&p
->blend
, 0, sizeof(p
->blend
));
195 p
->blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
197 /* no-op depth/stencil/alpha */
198 memset(&p
->depthstencil
, 0, sizeof(p
->depthstencil
));
201 memset(&p
->rasterizer
, 0, sizeof(p
->rasterizer
));
202 p
->rasterizer
.cull_face
= PIPE_FACE_NONE
;
203 p
->rasterizer
.gl_rasterization_rules
= 1;
206 memset(&p
->sampler
, 0, sizeof(p
->sampler
));
207 p
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
208 p
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
209 p
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
210 p
->sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
211 p
->sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_LINEAR
;
212 p
->sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_LINEAR
;
213 p
->sampler
.normalized_coords
= 1;
215 surf_tmpl
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
; /* All drivers support this */
216 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
217 surf_tmpl
.u
.tex
.level
= 0;
218 surf_tmpl
.u
.tex
.first_layer
= 0;
219 surf_tmpl
.u
.tex
.last_layer
= 0;
220 /* drawing destination */
221 memset(&p
->framebuffer
, 0, sizeof(p
->framebuffer
));
222 p
->framebuffer
.width
= WIDTH
;
223 p
->framebuffer
.height
= HEIGHT
;
224 p
->framebuffer
.nr_cbufs
= 1;
225 p
->framebuffer
.cbufs
[0] = p
->pipe
->create_surface(p
->pipe
, p
->target
, &surf_tmpl
);
227 /* viewport, depth isn't really needed */
232 float half_width
= (float)WIDTH
/ 2.0f
;
233 float half_height
= (float)HEIGHT
/ 2.0f
;
234 float half_depth
= ((float)FAR
- (float)NEAR
) / 2.0f
;
239 bias
= (float)HEIGHT
;
245 p
->viewport
.scale
[0] = half_width
;
246 p
->viewport
.scale
[1] = half_height
* scale
;
247 p
->viewport
.scale
[2] = half_depth
;
248 p
->viewport
.scale
[3] = 1.0f
;
250 p
->viewport
.translate
[0] = half_width
+ x
;
251 p
->viewport
.translate
[1] = (half_height
+ y
) * scale
+ bias
;
252 p
->viewport
.translate
[2] = half_depth
+ z
;
253 p
->viewport
.translate
[3] = 0.0f
;
256 /* vertex elements state */
257 memset(p
->velem
, 0, sizeof(p
->velem
));
258 p
->velem
[0].src_offset
= 0 * 4 * sizeof(float); /* offset 0, first element */
259 p
->velem
[0].instance_divisor
= 0;
260 p
->velem
[0].vertex_buffer_index
= 0;
261 p
->velem
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
263 p
->velem
[1].src_offset
= 1 * 4 * sizeof(float); /* offset 16, second element */
264 p
->velem
[1].instance_divisor
= 0;
265 p
->velem
[1].vertex_buffer_index
= 0;
266 p
->velem
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
270 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
271 TGSI_SEMANTIC_GENERIC
};
272 const uint semantic_indexes
[] = { 0, 0 };
273 p
->vs
= util_make_vertex_passthrough_shader(p
->pipe
, 2, semantic_names
, semantic_indexes
);
276 /* fragment shader */
277 p
->fs
= util_make_fragment_tex_shader(p
->pipe
, TGSI_TEXTURE_2D
, TGSI_INTERPOLATE_LINEAR
);
280 static void close_prog(struct program
*p
)
282 /* unset bound textures as well */
283 cso_set_fragment_sampler_views(p
->cso
, 0, NULL
);
285 /* unset all state */
286 cso_release_all(p
->cso
);
288 p
->pipe
->delete_vs_state(p
->pipe
, p
->vs
);
289 p
->pipe
->delete_fs_state(p
->pipe
, p
->fs
);
291 pipe_surface_reference(&p
->framebuffer
.cbufs
[0], NULL
);
292 pipe_sampler_view_reference(&p
->view
, NULL
);
293 pipe_resource_reference(&p
->target
, NULL
);
294 pipe_resource_reference(&p
->tex
, NULL
);
295 pipe_resource_reference(&p
->vbuf
, NULL
);
297 cso_destroy_context(p
->cso
);
298 p
->pipe
->destroy(p
->pipe
);
299 p
->screen
->destroy(p
->screen
);
304 static void draw(struct program
*p
)
306 /* set the render target */
307 cso_set_framebuffer(p
->cso
, &p
->framebuffer
);
309 /* clear the render target */
310 p
->pipe
->clear(p
->pipe
, PIPE_CLEAR_COLOR
, &p
->clear_color
, 0, 0);
312 /* set misc state we care about */
313 cso_set_blend(p
->cso
, &p
->blend
);
314 cso_set_depth_stencil_alpha(p
->cso
, &p
->depthstencil
);
315 cso_set_rasterizer(p
->cso
, &p
->rasterizer
);
316 cso_set_viewport(p
->cso
, &p
->viewport
);
319 cso_single_sampler(p
->cso
, 0, &p
->sampler
);
320 cso_single_sampler_done(p
->cso
);
322 /* texture sampler view */
323 cso_set_fragment_sampler_views(p
->cso
, 1, &p
->view
);
326 cso_set_fragment_shader_handle(p
->cso
, p
->fs
);
327 cso_set_vertex_shader_handle(p
->cso
, p
->vs
);
329 /* vertex element data */
330 cso_set_vertex_elements(p
->cso
, 2, p
->velem
);
332 util_draw_vertex_buffer(p
->pipe
, p
->cso
,
336 2); /* attribs/vert */
338 p
->pipe
->flush(p
->pipe
, NULL
);
340 debug_dump_surface_bmp(p
->pipe
, "result.bmp", p
->framebuffer
.cbufs
[0]);
343 int main(int argc
, char** argv
)
345 struct program
*p
= CALLOC_STRUCT(program
);