Path: blob/21.2-virgl/src/gallium/tests/graw/graw_util.h
4565 views
1#include "frontend/graw.h"23#include "pipe/p_context.h"4#include "pipe/p_defines.h"5#include "pipe/p_screen.h"6#include "pipe/p_shader_tokens.h"7#include "pipe/p_state.h"89#include "util/u_box.h"10#include "util/u_debug.h"11#include "util/u_debug_image.h"12#include "util/u_draw_quad.h"13#include "util/format/u_format.h"14#include "util/u_inlines.h"15#include "util/u_memory.h"161718struct graw_info19{20struct pipe_screen *screen;21struct pipe_context *ctx;22struct pipe_resource *color_buf[PIPE_MAX_COLOR_BUFS], *zs_buf;23struct pipe_surface *color_surf[PIPE_MAX_COLOR_BUFS], *zs_surf;24void *window;25};26272829static inline boolean30graw_util_create_window(struct graw_info *info,31int width, int height,32int num_cbufs, bool zstencil_buf)33{34static const enum pipe_format formats[] = {35PIPE_FORMAT_RGBA8888_UNORM,36PIPE_FORMAT_BGRA8888_UNORM,37PIPE_FORMAT_NONE38};39enum pipe_format format;40struct pipe_resource resource_temp;41struct pipe_surface surface_temp;42int i;4344memset(info, 0, sizeof(*info));45memset(&resource_temp, 0, sizeof(resource_temp));4647/* It's hard to say whether window or screen should be created48* first. Different environments would prefer one or the other.49*50* Also, no easy way of querying supported formats if the screen51* cannot be created first.52*/53for (i = 0; info->window == NULL && formats[i] != PIPE_FORMAT_NONE; i++) {54info->screen = graw_create_window_and_screen(0, 0, width, height,55formats[i],56&info->window);57format = formats[i];58}59if (!info->screen || !info->window) {60debug_printf("graw: Failed to create screen/window\n");61return FALSE;62}6364info->ctx = info->screen->context_create(info->screen, NULL, 0);65if (info->ctx == NULL) {66debug_printf("graw: Failed to create context\n");67return FALSE;68}6970for (i = 0; i < num_cbufs; i++) {71/* create color texture */72resource_temp.target = PIPE_TEXTURE_2D;73resource_temp.format = format;74resource_temp.width0 = width;75resource_temp.height0 = height;76resource_temp.depth0 = 1;77resource_temp.array_size = 1;78resource_temp.last_level = 0;79resource_temp.bind = (PIPE_BIND_RENDER_TARGET |80PIPE_BIND_DISPLAY_TARGET);81info->color_buf[i] = info->screen->resource_create(info->screen,82&resource_temp);83if (info->color_buf[i] == NULL) {84debug_printf("graw: Failed to create color texture\n");85return FALSE;86}8788/* create color surface */89surface_temp.format = resource_temp.format;90surface_temp.u.tex.level = 0;91surface_temp.u.tex.first_layer = 0;92surface_temp.u.tex.last_layer = 0;93info->color_surf[i] = info->ctx->create_surface(info->ctx,94info->color_buf[i],95&surface_temp);96if (info->color_surf[i] == NULL) {97debug_printf("graw: Failed to get color surface\n");98return FALSE;99}100}101102/* create Z texture (XXX try other Z/S formats if needed) */103resource_temp.target = PIPE_TEXTURE_2D;104resource_temp.format = PIPE_FORMAT_S8_UINT_Z24_UNORM;105resource_temp.width0 = width;106resource_temp.height0 = height;107resource_temp.depth0 = 1;108resource_temp.array_size = 1;109resource_temp.last_level = 0;110resource_temp.bind = PIPE_BIND_DEPTH_STENCIL;111info->zs_buf = info->screen->resource_create(info->screen, &resource_temp);112if (!info->zs_buf) {113debug_printf("graw: Failed to create Z texture\n");114return FALSE;115}116117/* create z surface */118surface_temp.format = resource_temp.format;119surface_temp.u.tex.level = 0;120surface_temp.u.tex.first_layer = 0;121surface_temp.u.tex.last_layer = 0;122info->zs_surf = info->ctx->create_surface(info->ctx,123info->zs_buf,124&surface_temp);125if (info->zs_surf == NULL) {126debug_printf("graw: Failed to get Z surface\n");127return FALSE;128}129130{131struct pipe_framebuffer_state fb;132memset(&fb, 0, sizeof fb);133fb.nr_cbufs = num_cbufs;134fb.width = width;135fb.height = height;136for (i = 0; i < num_cbufs; i++)137fb.cbufs[i] = info->color_surf[i];138fb.zsbuf = info->zs_surf;139info->ctx->set_framebuffer_state(info->ctx, &fb);140}141142return TRUE;143}144145146static inline void147graw_util_default_state(struct graw_info *info, boolean depth_test)148{149{150struct pipe_blend_state blend;151void *handle;152memset(&blend, 0, sizeof blend);153blend.rt[0].colormask = PIPE_MASK_RGBA;154handle = info->ctx->create_blend_state(info->ctx, &blend);155info->ctx->bind_blend_state(info->ctx, handle);156}157158{159struct pipe_depth_stencil_alpha_state depthStencilAlpha;160void *handle;161memset(&depthStencilAlpha, 0, sizeof depthStencilAlpha);162depthStencilAlpha.depth_enabled = depth_test;163depthStencilAlpha.depth_writemask = 1;164depthStencilAlpha.depth_func = PIPE_FUNC_LESS;165handle = info->ctx->create_depth_stencil_alpha_state(info->ctx,166&depthStencilAlpha);167info->ctx->bind_depth_stencil_alpha_state(info->ctx, handle);168}169170{171struct pipe_rasterizer_state rasterizer;172void *handle;173memset(&rasterizer, 0, sizeof rasterizer);174rasterizer.cull_face = PIPE_FACE_NONE;175rasterizer.half_pixel_center = 1;176rasterizer.bottom_edge_rule = 1;177handle = info->ctx->create_rasterizer_state(info->ctx, &rasterizer);178info->ctx->bind_rasterizer_state(info->ctx, handle);179}180}181182183static inline void184graw_util_viewport(struct graw_info *info,185float x, float y,186float width, float height,187float zNear, float zFar)188{189float z = zNear;190float half_width = width / 2.0f;191float half_height = height / 2.0f;192float half_depth = (zFar - zNear) / 2.0f;193struct pipe_viewport_state vp;194195vp.scale[0] = half_width;196vp.scale[1] = half_height;197vp.scale[2] = half_depth;198199vp.translate[0] = half_width + x;200vp.translate[1] = half_height + y;201vp.translate[2] = half_depth + z;202203vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;204vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;205vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;206vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;207208info->ctx->set_viewport_states(info->ctx, 0, 1, &vp);209}210211212static inline void213graw_util_flush_front(const struct graw_info *info)214{215info->screen->flush_frontbuffer(info->screen, info->ctx, info->color_buf[0],2160, 0, info->window, NULL);217}218219220static inline struct pipe_resource *221graw_util_create_tex2d(const struct graw_info *info,222int width, int height, enum pipe_format format,223const void *data)224{225const int row_stride = width * util_format_get_blocksize(format);226const int image_bytes = row_stride * height;227struct pipe_resource temp, *tex;228struct pipe_box box;229230memset(&temp, 0, sizeof(temp));231temp.target = PIPE_TEXTURE_2D;232temp.format = format;233temp.width0 = width;234temp.height0 = height;235temp.depth0 = 1;236temp.last_level = 0;237temp.array_size = 1;238temp.bind = PIPE_BIND_SAMPLER_VIEW;239240tex = info->screen->resource_create(info->screen, &temp);241if (!tex) {242debug_printf("graw: failed to create texture\n");243return NULL;244}245246u_box_2d(0, 0, width, height, &box);247248info->ctx->texture_subdata(info->ctx,249tex,2500,251PIPE_MAP_WRITE,252&box,253data,254row_stride,255image_bytes);256257/* Possibly read back & compare against original data:258*/259#if 0260{261struct pipe_transfer *t;262uint32_t *ptr;263t = pipe_texture_map(info->ctx, samptex,2640, 0, /* level, layer */265PIPE_MAP_READ,2660, 0, SIZE, SIZE); /* x, y, width, height */267268ptr = info->ctx->texture_map(info->ctx, t);269270if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {271assert(0);272exit(9);273}274275info->ctx->texture_unmap(info->ctx, t);276}277#endif278279return tex;280}281282283static inline void *284graw_util_create_simple_sampler(const struct graw_info *info,285unsigned wrap_mode,286unsigned img_filter)287{288struct pipe_sampler_state sampler_desc;289void *sampler;290291memset(&sampler_desc, 0, sizeof sampler_desc);292sampler_desc.wrap_s =293sampler_desc.wrap_t =294sampler_desc.wrap_r = wrap_mode;295sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;296sampler_desc.min_img_filter =297sampler_desc.mag_img_filter = img_filter;298sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;299sampler_desc.compare_func = 0;300sampler_desc.normalized_coords = 1;301sampler_desc.max_anisotropy = 0;302303sampler = info->ctx->create_sampler_state(info->ctx, &sampler_desc);304305return sampler;306}307308309static inline struct pipe_sampler_view *310graw_util_create_simple_sampler_view(const struct graw_info *info,311struct pipe_resource *texture)312{313struct pipe_sampler_view sv_temp;314struct pipe_sampler_view *sv;315316memset(&sv_temp, 0, sizeof(sv_temp));317sv_temp.format = texture->format;318sv_temp.texture = texture;319sv_temp.swizzle_r = PIPE_SWIZZLE_X;320sv_temp.swizzle_g = PIPE_SWIZZLE_Y;321sv_temp.swizzle_b = PIPE_SWIZZLE_Z;322sv_temp.swizzle_a = PIPE_SWIZZLE_W;323324sv = info->ctx->create_sampler_view(info->ctx, texture, &sv_temp);325326return sv;327}328329330331