tri.c revision 3e3d6864da093a99bad282e78945a1b1c4503ca4
1/************************************************************************** 2 * 3 * Copyright © 2010 Jakob Bornecrantz 4 * 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: 11 * 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 14 * Software. 15 * 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. 23 * 24 **************************************************************************/ 25 26 27#define USE_TRACE 0 28#define WIDTH 300 29#define HEIGHT 300 30#define NEAR 30 31#define FAR 1000 32#define FLIP 0 33 34/* pipe_*_state structs */ 35#include "pipe/p_state.h" 36/* pipe_context */ 37#include "pipe/p_context.h" 38/* pipe_screen */ 39#include "pipe/p_screen.h" 40/* PIPE_* */ 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" 46 47/* constant state object helper */ 48#include "cso_cache/cso_context.h" 49 50/* debug_dump_surface_bmp */ 51#include "util/u_debug.h" 52/* util_draw_vertex_buffer helper */ 53#include "util/u_draw_quad.h" 54/* FREE & CALLOC_STRUCT */ 55#include "util/u_memory.h" 56/* util_make_[fragment|vertex]_passthrough_shader */ 57#include "util/u_simple_shaders.h" 58 59/* softpipe software driver */ 60#include "softpipe/sp_public.h" 61 62/* null software winsys */ 63#include "sw/null/null_sw_winsys.h" 64 65/* traceing support see src/gallium/drivers/trace/README for more info. */ 66#if USE_TRACE 67#include "trace/tr_screen.h" 68#include "trace/tr_context.h" 69#endif 70 71struct program 72{ 73 struct pipe_screen *screen; 74 struct pipe_context *pipe; 75 struct cso_context *cso; 76 77 struct pipe_blend_state blend; 78 struct pipe_depth_stencil_alpha_state depthstencil; 79 struct pipe_rasterizer_state rasterizer; 80 struct pipe_viewport_state viewport; 81 struct pipe_framebuffer_state framebuffer; 82 struct pipe_vertex_element velem[2]; 83 84 void *vs; 85 void *fs; 86 87 float clear_color[4]; 88 89 struct pipe_resource *vbuf; 90 struct pipe_resource *target; 91}; 92 93static void init_prog(struct program *p) 94{ 95 /* create the software rasterizer */ 96 p->screen = softpipe_create_screen(null_sw_create()); 97#if USE_TRACE 98 p->screen = trace_screen_create(p->screen); 99#endif 100 p->pipe = p->screen->context_create(p->screen, NULL); 101 p->cso = cso_create_context(p->pipe); 102 103 /* set clear color */ 104 p->clear_color[0] = 0.3; 105 p->clear_color[1] = 0.1; 106 p->clear_color[2] = 0.3; 107 p->clear_color[3] = 1.0; 108 109 /* vertex buffer */ 110 { 111 float vertices[4][2][4] = { 112 { 113 { 0.0f, -0.9f, 0.0f, 1.0f }, 114 { 1.0f, 0.0f, 0.0f, 1.0f } 115 }, 116 { 117 { -0.9f, 0.9f, 0.0f, 1.0f }, 118 { 0.0f, 1.0f, 0.0f, 1.0f } 119 }, 120 { 121 { 0.9f, 0.9f, 0.0f, 1.0f }, 122 { 0.0f, 0.0f, 1.0f, 1.0f } 123 } 124 }; 125 126 p->vbuf = pipe_buffer_create(p->screen, PIPE_BIND_VERTEX_BUFFER, sizeof(vertices)); 127 pipe_buffer_write(p->pipe, p->vbuf, 0, sizeof(vertices), vertices); 128 } 129 130 /* render target texture */ 131 { 132 struct pipe_resource tmplt; 133 memset(&tmplt, 0, sizeof(tmplt)); 134 tmplt.target = PIPE_TEXTURE_2D; 135 tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */ 136 tmplt.width0 = WIDTH; 137 tmplt.height0 = HEIGHT; 138 tmplt.depth0 = 1; 139 tmplt.last_level = 0; 140 tmplt.bind = PIPE_BIND_RENDER_TARGET; 141 142 p->target = p->screen->resource_create(p->screen, &tmplt); 143 } 144 145 /* disabled blending/masking */ 146 memset(&p->blend, 0, sizeof(p->blend)); 147 p->blend.rt[0].colormask = PIPE_MASK_RGBA; 148 149 /* no-op depth/stencil/alpha */ 150 memset(&p->depthstencil, 0, sizeof(p->depthstencil)); 151 152 /* rasterizer */ 153 memset(&p->rasterizer, 0, sizeof(p->rasterizer)); 154 p->rasterizer.front_winding = PIPE_WINDING_CW; 155 p->rasterizer.cull_mode = PIPE_WINDING_NONE; 156 p->rasterizer.gl_rasterization_rules = 1; 157 158 /* drawing destination */ 159 memset(&p->framebuffer, 0, sizeof(p->framebuffer)); 160 p->framebuffer.width = WIDTH; 161 p->framebuffer.height = HEIGHT; 162 p->framebuffer.nr_cbufs = 1; 163 p->framebuffer.cbufs[0] = p->screen->get_tex_surface(p->screen, p->target, 0, 0, 0, PIPE_BIND_RENDER_TARGET); 164 165 /* viewport, depth isn't really needed */ 166 { 167 float x = 0; 168 float y = 0; 169 float z = FAR; 170 float half_width = (float)WIDTH / 2.0f; 171 float half_height = (float)HEIGHT / 2.0f; 172 float half_depth = ((float)FAR - (float)NEAR) / 2.0f; 173 float scale, bias; 174 175 if (FLIP) { 176 scale = -1.0f; 177 bias = (float)HEIGHT; 178 } else { 179 scale = 1.0f; 180 bias = 0.0f; 181 } 182 183 p->viewport.scale[0] = half_width; 184 p->viewport.scale[1] = half_height * scale; 185 p->viewport.scale[2] = half_depth; 186 p->viewport.scale[3] = 1.0f; 187 188 p->viewport.translate[0] = half_width + x; 189 p->viewport.translate[1] = (half_height + y) * scale + bias; 190 p->viewport.translate[2] = half_depth + z; 191 p->viewport.translate[3] = 0.0f; 192 } 193 194 /* vertex elements state */ 195 memset(p->velem, 0, sizeof(p->velem)); 196 p->velem[0].src_offset = 0 * 4 * sizeof(float); /* offset 0, first element */ 197 p->velem[0].instance_divisor = 0; 198 p->velem[0].vertex_buffer_index = 0; 199 p->velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 200 201 p->velem[1].src_offset = 1 * 4 * sizeof(float); /* offset 16, second element */ 202 p->velem[1].instance_divisor = 0; 203 p->velem[1].vertex_buffer_index = 0; 204 p->velem[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 205 206 /* vertex shader */ 207 { 208 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION, 209 TGSI_SEMANTIC_COLOR }; 210 const uint semantic_indexes[] = { 0, 0 }; 211 p->vs = util_make_vertex_passthrough_shader(p->pipe, 2, semantic_names, semantic_indexes); 212 } 213 214 /* fragment shader */ 215 p->fs = util_make_fragment_passthrough_shader(p->pipe); 216} 217 218static void close_prog(struct program *p) 219{ 220 /* unset all state */ 221 cso_release_all(p->cso); 222 223 p->pipe->delete_vs_state(p->pipe, p->vs); 224 p->pipe->delete_fs_state(p->pipe, p->fs); 225 226 pipe_surface_reference(&p->framebuffer.cbufs[0], NULL); 227 pipe_resource_reference(&p->target, NULL); 228 pipe_resource_reference(&p->vbuf, NULL); 229 230 cso_destroy_context(p->cso); 231 p->pipe->destroy(p->pipe); 232 p->screen->destroy(p->screen); 233 234 FREE(p); 235} 236 237static void draw(struct program *p) 238{ 239 /* set the render target */ 240 cso_set_framebuffer(p->cso, &p->framebuffer); 241 242 /* clear the render target */ 243 p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR, p->clear_color, 0, 0); 244 245 /* set misc state we care about */ 246 cso_set_blend(p->cso, &p->blend); 247 cso_set_depth_stencil_alpha(p->cso, &p->depthstencil); 248 cso_set_rasterizer(p->cso, &p->rasterizer); 249 cso_set_viewport(p->cso, &p->viewport); 250 251 /* shaders */ 252 cso_set_fragment_shader_handle(p->cso, p->fs); 253 cso_set_vertex_shader_handle(p->cso, p->vs); 254 255 /* vertex element data */ 256 cso_set_vertex_elements(p->cso, 2, p->velem); 257 258 util_draw_vertex_buffer(p->pipe, 259 p->vbuf, 0, 260 PIPE_PRIM_TRIANGLES, 261 3, /* verts */ 262 2); /* attribs/vert */ 263 264 p->pipe->flush(p->pipe, PIPE_FLUSH_RENDER_CACHE, NULL); 265 266 debug_dump_surface_bmp(p->pipe, "result.bmp", p->framebuffer.cbufs[0]); 267} 268 269int main(int argc, char** argv) 270{ 271 struct program *p = CALLOC_STRUCT(program); 272 273 init_prog(p); 274 draw(p); 275 close_prog(p); 276 277 return 0; 278} 279