Path: blob/21.2-virgl/src/gallium/tests/trivial/quad-tex.c
4561 views
/**************************************************************************1*2* Copyright © 2010 Jakob Bornecrantz3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*23**************************************************************************/242526#define USE_TRACE 027#define WIDTH 30028#define HEIGHT 30029#define NEAR 030#define FAR 131#define FLIP 03233/* pipe_*_state structs */34#include "pipe/p_state.h"35/* pipe_context */36#include "pipe/p_context.h"37/* pipe_screen */38#include "pipe/p_screen.h"39/* PIPE_* */40#include "pipe/p_defines.h"41/* TGSI_SEMANTIC_{POSITION|GENERIC} */42#include "pipe/p_shader_tokens.h"43/* pipe_buffer_* helpers */44#include "util/u_inlines.h"4546/* constant state object helper */47#include "cso_cache/cso_context.h"4849#include "util/macros.h"50/* u_sampler_view_default_template */51#include "util/u_sampler.h"52/* debug_dump_surface_bmp */53#include "util/u_debug_image.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"60/* to get a hardware pipe driver */61#include "pipe-loader/pipe_loader.h"6263struct program64{65struct pipe_loader_device *dev;66struct pipe_screen *screen;67struct pipe_context *pipe;68struct cso_context *cso;6970struct pipe_blend_state blend;71struct pipe_depth_stencil_alpha_state depthstencil;72struct pipe_rasterizer_state rasterizer;73struct pipe_sampler_state sampler;74struct pipe_viewport_state viewport;75struct pipe_framebuffer_state framebuffer;76struct cso_velems_state velem;7778void *vs;79void *fs;8081union pipe_color_union clear_color;8283struct pipe_resource *vbuf;84struct pipe_resource *target;85struct pipe_resource *tex;86struct pipe_sampler_view *view;87};8889static void init_prog(struct program *p)90{91struct pipe_surface surf_tmpl;92ASSERTED int ret;9394/* find a hardware device */95ret = pipe_loader_probe(&p->dev, 1);96assert(ret);9798/* init a pipe screen */99p->screen = pipe_loader_create_screen(p->dev);100assert(p->screen);101102/* create the pipe driver context and cso context */103p->pipe = p->screen->context_create(p->screen, NULL, 0);104p->cso = cso_create_context(p->pipe, 0);105106/* set clear color */107p->clear_color.f[0] = 0.3;108p->clear_color.f[1] = 0.1;109p->clear_color.f[2] = 0.3;110p->clear_color.f[3] = 1.0;111112/* vertex buffer */113{114float vertices[4][2][4] = {115{116{ 0.9f, 0.9f, 0.0f, 1.0f },117{ 1.0f, 1.0f, 0.0f, 1.0f }118},119{120{ -0.9f, 0.9f, 0.0f, 1.0f },121{ 0.0f, 1.0f, 0.0f, 1.0f }122},123{124{ -0.9f, -0.9f, 0.0f, 1.0f },125{ 0.0f, 0.0f, 1.0f, 1.0f }126},127{128{ 0.9f, -0.9f, 0.0f, 1.0f },129{ 1.0f, 0.0f, 1.0f, 1.0f }130}131};132133p->vbuf = pipe_buffer_create(p->screen, PIPE_BIND_VERTEX_BUFFER,134PIPE_USAGE_DEFAULT, sizeof(vertices));135pipe_buffer_write(p->pipe, p->vbuf, 0, sizeof(vertices), vertices);136}137138/* render target texture */139{140struct pipe_resource tmplt;141memset(&tmplt, 0, sizeof(tmplt));142tmplt.target = PIPE_TEXTURE_2D;143tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */144tmplt.width0 = WIDTH;145tmplt.height0 = HEIGHT;146tmplt.depth0 = 1;147tmplt.array_size = 1;148tmplt.last_level = 0;149tmplt.bind = PIPE_BIND_RENDER_TARGET;150151p->target = p->screen->resource_create(p->screen, &tmplt);152}153154/* sampler texture */155{156uint32_t *ptr;157struct pipe_transfer *t;158struct pipe_resource t_tmplt;159struct pipe_sampler_view v_tmplt;160struct pipe_box box;161162memset(&t_tmplt, 0, sizeof(t_tmplt));163t_tmplt.target = PIPE_TEXTURE_2D;164t_tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */165t_tmplt.width0 = 2;166t_tmplt.height0 = 2;167t_tmplt.depth0 = 1;168t_tmplt.array_size = 1;169t_tmplt.last_level = 0;170t_tmplt.bind = PIPE_BIND_RENDER_TARGET;171172p->tex = p->screen->resource_create(p->screen, &t_tmplt);173174memset(&box, 0, sizeof(box));175box.width = 2;176box.height = 2;177box.depth = 1;178179ptr = p->pipe->texture_map(p->pipe, p->tex, 0, PIPE_MAP_WRITE, &box, &t);180ptr[0] = 0xffff0000;181ptr[1] = 0xff0000ff;182ptr[2] = 0xff00ff00;183ptr[3] = 0xffffff00;184p->pipe->texture_unmap(p->pipe, t);185186u_sampler_view_default_template(&v_tmplt, p->tex, p->tex->format);187188p->view = p->pipe->create_sampler_view(p->pipe, p->tex, &v_tmplt);189}190191/* disabled blending/masking */192memset(&p->blend, 0, sizeof(p->blend));193p->blend.rt[0].colormask = PIPE_MASK_RGBA;194195/* no-op depth/stencil/alpha */196memset(&p->depthstencil, 0, sizeof(p->depthstencil));197198/* rasterizer */199memset(&p->rasterizer, 0, sizeof(p->rasterizer));200p->rasterizer.cull_face = PIPE_FACE_NONE;201p->rasterizer.half_pixel_center = 1;202p->rasterizer.bottom_edge_rule = 1;203p->rasterizer.depth_clip_near = 1;204p->rasterizer.depth_clip_far = 1;205206/* sampler */207memset(&p->sampler, 0, sizeof(p->sampler));208p->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;209p->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;210p->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;211p->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;212p->sampler.min_img_filter = PIPE_TEX_MIPFILTER_LINEAR;213p->sampler.mag_img_filter = PIPE_TEX_MIPFILTER_LINEAR;214p->sampler.normalized_coords = 1;215216surf_tmpl.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */217surf_tmpl.u.tex.level = 0;218surf_tmpl.u.tex.first_layer = 0;219surf_tmpl.u.tex.last_layer = 0;220/* drawing destination */221memset(&p->framebuffer, 0, sizeof(p->framebuffer));222p->framebuffer.width = WIDTH;223p->framebuffer.height = HEIGHT;224p->framebuffer.nr_cbufs = 1;225p->framebuffer.cbufs[0] = p->pipe->create_surface(p->pipe, p->target, &surf_tmpl);226227/* viewport, depth isn't really needed */228{229float x = 0;230float y = 0;231float z = NEAR;232float half_width = (float)WIDTH / 2.0f;233float half_height = (float)HEIGHT / 2.0f;234float half_depth = ((float)FAR - (float)NEAR) / 2.0f;235float scale, bias;236237if (FLIP) {238scale = -1.0f;239bias = (float)HEIGHT;240} else {241scale = 1.0f;242bias = 0.0f;243}244245p->viewport.scale[0] = half_width;246p->viewport.scale[1] = half_height * scale;247p->viewport.scale[2] = half_depth;248249p->viewport.translate[0] = half_width + x;250p->viewport.translate[1] = (half_height + y) * scale + bias;251p->viewport.translate[2] = half_depth + z;252253p->viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;254p->viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;255p->viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;256p->viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;257}258259/* vertex elements state */260memset(&p->velem, 0, sizeof(p->velem));261p->velem.count = 2;262263p->velem.velems[0].src_offset = 0 * 4 * sizeof(float); /* offset 0, first element */264p->velem.velems[0].instance_divisor = 0;265p->velem.velems[0].vertex_buffer_index = 0;266p->velem.velems[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;267268p->velem.velems[1].src_offset = 1 * 4 * sizeof(float); /* offset 16, second element */269p->velem.velems[1].instance_divisor = 0;270p->velem.velems[1].vertex_buffer_index = 0;271p->velem.velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;272273/* vertex shader */274{275const enum tgsi_semantic semantic_names[] =276{ TGSI_SEMANTIC_POSITION, TGSI_SEMANTIC_GENERIC };277const uint semantic_indexes[] = { 0, 0 };278p->vs = util_make_vertex_passthrough_shader(p->pipe, 2, semantic_names, semantic_indexes, FALSE);279}280281/* fragment shader */282p->fs = util_make_fragment_tex_shader(p->pipe, TGSI_TEXTURE_2D,283TGSI_INTERPOLATE_LINEAR,284TGSI_RETURN_TYPE_FLOAT,285TGSI_RETURN_TYPE_FLOAT, false,286false);287}288289static void close_prog(struct program *p)290{291cso_destroy_context(p->cso);292293p->pipe->delete_vs_state(p->pipe, p->vs);294p->pipe->delete_fs_state(p->pipe, p->fs);295296pipe_surface_reference(&p->framebuffer.cbufs[0], NULL);297pipe_sampler_view_reference(&p->view, NULL);298pipe_resource_reference(&p->target, NULL);299pipe_resource_reference(&p->tex, NULL);300pipe_resource_reference(&p->vbuf, NULL);301302p->pipe->destroy(p->pipe);303p->screen->destroy(p->screen);304pipe_loader_release(&p->dev, 1);305306FREE(p);307}308309static void draw(struct program *p)310{311const struct pipe_sampler_state *samplers[] = {&p->sampler};312313/* set the render target */314cso_set_framebuffer(p->cso, &p->framebuffer);315316/* clear the render target */317p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR, NULL, &p->clear_color, 0, 0);318319/* set misc state we care about */320cso_set_blend(p->cso, &p->blend);321cso_set_depth_stencil_alpha(p->cso, &p->depthstencil);322cso_set_rasterizer(p->cso, &p->rasterizer);323cso_set_viewport(p->cso, &p->viewport);324325/* sampler */326cso_set_samplers(p->cso, PIPE_SHADER_FRAGMENT, 1, samplers);327328/* texture sampler view */329p->pipe->set_sampler_views(p->pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, &p->view);330331/* shaders */332cso_set_fragment_shader_handle(p->cso, p->fs);333cso_set_vertex_shader_handle(p->cso, p->vs);334335/* vertex element data */336cso_set_vertex_elements(p->cso, &p->velem);337338util_draw_vertex_buffer(p->pipe, p->cso,339p->vbuf, 0, 0,340PIPE_PRIM_QUADS,3414, /* verts */3422); /* attribs/vert */343344p->pipe->flush(p->pipe, NULL, 0);345346debug_dump_surface_bmp(p->pipe, "result.bmp", p->framebuffer.cbufs[0]);347}348349int main(int argc, char** argv)350{351struct program *p = CALLOC_STRUCT(program);352353init_prog(p);354draw(p);355close_prog(p);356357return 0;358}359360361