Path: blob/21.2-virgl/src/gallium/drivers/crocus/crocus_resource.c
4570 views
/*1* Copyright © 2017 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included11* in all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING18* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER19* DEALINGS IN THE SOFTWARE.20*/2122/**23* @file crocus_resource.c24*25* Resources are images, buffers, and other objects used by the GPU.26*27* XXX: explain resources28*/2930#include <stdio.h>31#include <errno.h>32#include "pipe/p_defines.h"33#include "pipe/p_state.h"34#include "pipe/p_context.h"35#include "pipe/p_screen.h"36#include "util/os_memory.h"37#include "util/u_cpu_detect.h"38#include "util/u_inlines.h"39#include "util/format/u_format.h"40#include "util/u_threaded_context.h"41#include "util/u_transfer.h"42#include "util/u_transfer_helper.h"43#include "util/u_upload_mgr.h"44#include "util/ralloc.h"45#include "util/u_memory.h"46#include "crocus_batch.h"47#include "crocus_context.h"48#include "crocus_resource.h"49#include "crocus_screen.h"50#include "intel/dev/intel_debug.h"51#include "isl/isl.h"52#include "drm-uapi/drm_fourcc.h"53#include "drm-uapi/i915_drm.h"5455enum modifier_priority {56MODIFIER_PRIORITY_INVALID = 0,57MODIFIER_PRIORITY_LINEAR,58MODIFIER_PRIORITY_X,59MODIFIER_PRIORITY_Y,60MODIFIER_PRIORITY_Y_CCS,61};6263static const uint64_t priority_to_modifier[] = {64[MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,65[MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,66[MODIFIER_PRIORITY_X] = I915_FORMAT_MOD_X_TILED,67[MODIFIER_PRIORITY_Y] = I915_FORMAT_MOD_Y_TILED,68[MODIFIER_PRIORITY_Y_CCS] = I915_FORMAT_MOD_Y_TILED_CCS,69};7071static bool72modifier_is_supported(const struct intel_device_info *devinfo,73enum pipe_format pfmt, unsigned bind,74uint64_t modifier)75{76/* XXX: do something real */77switch (modifier) {78case I915_FORMAT_MOD_Y_TILED_CCS:79return false;80case I915_FORMAT_MOD_Y_TILED:81if (bind & PIPE_BIND_SCANOUT)82return false;83return devinfo->ver >= 6;84case I915_FORMAT_MOD_X_TILED:85case DRM_FORMAT_MOD_LINEAR:86return true;87case DRM_FORMAT_MOD_INVALID:88default:89return false;90}91}9293static uint64_t94select_best_modifier(struct intel_device_info *devinfo,95const struct pipe_resource *templ,96const uint64_t *modifiers,97int count)98{99enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;100101for (int i = 0; i < count; i++) {102if (!modifier_is_supported(devinfo, templ->format, templ->bind,103modifiers[i]))104continue;105106switch (modifiers[i]) {107case I915_FORMAT_MOD_Y_TILED_CCS:108prio = MAX2(prio, MODIFIER_PRIORITY_Y_CCS);109break;110case I915_FORMAT_MOD_Y_TILED:111prio = MAX2(prio, MODIFIER_PRIORITY_Y);112break;113case I915_FORMAT_MOD_X_TILED:114prio = MAX2(prio, MODIFIER_PRIORITY_X);115break;116case DRM_FORMAT_MOD_LINEAR:117prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);118break;119case DRM_FORMAT_MOD_INVALID:120default:121break;122}123}124125return priority_to_modifier[prio];126}127128static enum isl_surf_dim129crocus_target_to_isl_surf_dim(enum pipe_texture_target target)130{131switch (target) {132case PIPE_BUFFER:133case PIPE_TEXTURE_1D:134case PIPE_TEXTURE_1D_ARRAY:135return ISL_SURF_DIM_1D;136case PIPE_TEXTURE_2D:137case PIPE_TEXTURE_CUBE:138case PIPE_TEXTURE_RECT:139case PIPE_TEXTURE_2D_ARRAY:140case PIPE_TEXTURE_CUBE_ARRAY:141return ISL_SURF_DIM_2D;142case PIPE_TEXTURE_3D:143return ISL_SURF_DIM_3D;144case PIPE_MAX_TEXTURE_TYPES:145break;146}147unreachable("invalid texture type");148}149150static isl_surf_usage_flags_t151pipe_bind_to_isl_usage(unsigned bindings)152{153isl_surf_usage_flags_t usage = 0;154155if (bindings & PIPE_BIND_RENDER_TARGET)156usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;157158if (bindings & PIPE_BIND_SAMPLER_VIEW)159usage |= ISL_SURF_USAGE_TEXTURE_BIT;160161if (bindings & (PIPE_BIND_SHADER_IMAGE | PIPE_BIND_SHADER_BUFFER))162usage |= ISL_SURF_USAGE_STORAGE_BIT;163164if (bindings & PIPE_BIND_DISPLAY_TARGET)165usage |= ISL_SURF_USAGE_DISPLAY_BIT;166return usage;167}168169static bool170crocus_resource_configure_main(const struct crocus_screen *screen,171struct crocus_resource *res,172const struct pipe_resource *templ,173uint64_t modifier, uint32_t row_pitch_B)174{175const struct intel_device_info *devinfo = &screen->devinfo;176const struct util_format_description *format_desc =177util_format_description(templ->format);178const bool has_depth = util_format_has_depth(format_desc);179isl_surf_usage_flags_t usage = pipe_bind_to_isl_usage(templ->bind);180isl_tiling_flags_t tiling_flags = ISL_TILING_ANY_MASK;181182/* TODO: This used to be because there wasn't BLORP to handle Y-tiling. */183if (devinfo->ver < 6 && !util_format_is_depth_or_stencil(templ->format))184tiling_flags &= ~ISL_TILING_Y0_BIT;185186if (modifier != DRM_FORMAT_MOD_INVALID) {187res->mod_info = isl_drm_modifier_get_info(modifier);188189tiling_flags = 1 << res->mod_info->tiling;190} else {191if (templ->bind & PIPE_BIND_RENDER_TARGET && devinfo->ver < 6) {192modifier = I915_FORMAT_MOD_X_TILED;193res->mod_info = isl_drm_modifier_get_info(modifier);194tiling_flags = 1 << res->mod_info->tiling;195}196/* Use linear for staging buffers */197if (templ->usage == PIPE_USAGE_STAGING ||198templ->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR) )199tiling_flags = ISL_TILING_LINEAR_BIT;200else if (templ->bind & PIPE_BIND_SCANOUT)201tiling_flags = screen->devinfo.has_tiling_uapi ?202ISL_TILING_X_BIT : ISL_TILING_LINEAR_BIT;203}204205if (templ->target == PIPE_TEXTURE_CUBE ||206templ->target == PIPE_TEXTURE_CUBE_ARRAY)207usage |= ISL_SURF_USAGE_CUBE_BIT;208209if (templ->usage != PIPE_USAGE_STAGING) {210if (templ->format == PIPE_FORMAT_S8_UINT)211usage |= ISL_SURF_USAGE_STENCIL_BIT;212else if (has_depth) {213/* combined DS only on gen4/5 */214if (devinfo->ver < 6) {215if (templ->format == PIPE_FORMAT_Z24X8_UNORM ||216templ->format == PIPE_FORMAT_Z24_UNORM_S8_UINT ||217templ->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)218usage |= ISL_SURF_USAGE_STENCIL_BIT;219}220usage |= ISL_SURF_USAGE_DEPTH_BIT;221}222223if (templ->format == PIPE_FORMAT_S8_UINT)224tiling_flags = ISL_TILING_W_BIT;225}226227const enum isl_format format =228crocus_format_for_usage(&screen->devinfo, templ->format, usage).fmt;229230if (row_pitch_B == 0 && templ->usage == PIPE_USAGE_STAGING &&231templ->target == PIPE_TEXTURE_2D &&232devinfo->ver < 6) {233/* align row pitch to 4 so we can keep using BLT engine */234row_pitch_B = util_format_get_stride(templ->format, templ->width0);235row_pitch_B = ALIGN(row_pitch_B, 4);236}237238const struct isl_surf_init_info init_info = {239.dim = crocus_target_to_isl_surf_dim(templ->target),240.format = format,241.width = templ->width0,242.height = templ->height0,243.depth = templ->depth0,244.levels = templ->last_level + 1,245.array_len = templ->array_size,246.samples = MAX2(templ->nr_samples, 1),247.min_alignment_B = 0,248.row_pitch_B = row_pitch_B,249.usage = usage,250.tiling_flags = tiling_flags251};252253if (!isl_surf_init_s(&screen->isl_dev, &res->surf, &init_info))254return false;255256res->internal_format = templ->format;257258return true;259}260261static void262crocus_query_dmabuf_modifiers(struct pipe_screen *pscreen,263enum pipe_format pfmt,264int max,265uint64_t *modifiers,266unsigned int *external_only,267int *count)268{269struct crocus_screen *screen = (void *) pscreen;270const struct intel_device_info *devinfo = &screen->devinfo;271272uint64_t all_modifiers[] = {273DRM_FORMAT_MOD_LINEAR,274I915_FORMAT_MOD_X_TILED,275I915_FORMAT_MOD_Y_TILED,276I915_FORMAT_MOD_Y_TILED_CCS,277};278279int supported_mods = 0;280281for (int i = 0; i < ARRAY_SIZE(all_modifiers); i++) {282if (!modifier_is_supported(devinfo, pfmt, 0, all_modifiers[i]))283continue;284285if (supported_mods < max) {286if (modifiers)287modifiers[supported_mods] = all_modifiers[i];288289if (external_only)290external_only[supported_mods] = util_format_is_yuv(pfmt);291}292293supported_mods++;294}295296*count = supported_mods;297}298299static struct pipe_resource *300crocus_resource_get_separate_stencil(struct pipe_resource *p_res)301{302return _crocus_resource_get_separate_stencil(p_res);303}304305static void306crocus_resource_set_separate_stencil(struct pipe_resource *p_res,307struct pipe_resource *stencil)308{309assert(util_format_has_depth(util_format_description(p_res->format)));310pipe_resource_reference(&p_res->next, stencil);311}312313void314crocus_resource_disable_aux(struct crocus_resource *res)315{316crocus_bo_unreference(res->aux.bo);317free(res->aux.state);318319res->aux.usage = ISL_AUX_USAGE_NONE;320res->aux.has_hiz = 0;321res->aux.surf.size_B = 0;322res->aux.surf.levels = 0;323res->aux.bo = NULL;324res->aux.state = NULL;325}326327static void328crocus_resource_destroy(struct pipe_screen *screen,329struct pipe_resource *resource)330{331struct crocus_resource *res = (struct crocus_resource *)resource;332333if (resource->target == PIPE_BUFFER)334util_range_destroy(&res->valid_buffer_range);335336if (res->shadow)337pipe_resource_reference((struct pipe_resource **)&res->shadow, NULL);338crocus_resource_disable_aux(res);339340threaded_resource_deinit(resource);341crocus_bo_unreference(res->bo);342crocus_pscreen_unref(res->orig_screen);343free(res);344}345346static struct crocus_resource *347crocus_alloc_resource(struct pipe_screen *pscreen,348const struct pipe_resource *templ)349{350struct crocus_resource *res = calloc(1, sizeof(struct crocus_resource));351if (!res)352return NULL;353354res->base.b = *templ;355res->base.b.screen = pscreen;356res->orig_screen = crocus_pscreen_ref(pscreen);357pipe_reference_init(&res->base.b.reference, 1);358threaded_resource_init(&res->base.b);359360if (templ->target == PIPE_BUFFER)361util_range_init(&res->valid_buffer_range);362363return res;364}365366unsigned367crocus_get_num_logical_layers(const struct crocus_resource *res, unsigned level)368{369if (res->surf.dim == ISL_SURF_DIM_3D)370return minify(res->surf.logical_level0_px.depth, level);371else372return res->surf.logical_level0_px.array_len;373}374375static enum isl_aux_state **376create_aux_state_map(struct crocus_resource *res, enum isl_aux_state initial)377{378assert(res->aux.state == NULL);379380uint32_t total_slices = 0;381for (uint32_t level = 0; level < res->surf.levels; level++)382total_slices += crocus_get_num_logical_layers(res, level);383384const size_t per_level_array_size =385res->surf.levels * sizeof(enum isl_aux_state *);386387/* We're going to allocate a single chunk of data for both the per-level388* reference array and the arrays of aux_state. This makes cleanup389* significantly easier.390*/391const size_t total_size =392per_level_array_size + total_slices * sizeof(enum isl_aux_state);393394void *data = malloc(total_size);395if (!data)396return NULL;397398enum isl_aux_state **per_level_arr = data;399enum isl_aux_state *s = data + per_level_array_size;400for (uint32_t level = 0; level < res->surf.levels; level++) {401per_level_arr[level] = s;402const unsigned level_layers = crocus_get_num_logical_layers(res, level);403for (uint32_t a = 0; a < level_layers; a++)404*(s++) = initial;405}406assert((void *)s == data + total_size);407408return per_level_arr;409}410411/**412* Configure aux for the resource, but don't allocate it. For images which413* might be shared with modifiers, we must allocate the image and aux data in414* a single bo.415*416* Returns false on unexpected error (e.g. allocation failed, or invalid417* configuration result).418*/419static bool420crocus_resource_configure_aux(struct crocus_screen *screen,421struct crocus_resource *res, bool imported,422uint64_t *aux_size_B,423uint32_t *alloc_flags)424{425const struct intel_device_info *devinfo = &screen->devinfo;426427/* Try to create the auxiliary surfaces allowed by the modifier or by428* the user if no modifier is specified.429*/430assert(!res->mod_info || res->mod_info->aux_usage == ISL_AUX_USAGE_NONE);431432const bool has_mcs = devinfo->ver >= 7 && !res->mod_info &&433isl_surf_get_mcs_surf(&screen->isl_dev, &res->surf, &res->aux.surf);434435const bool has_hiz = devinfo->ver >= 6 && !res->mod_info &&436!(INTEL_DEBUG & DEBUG_NO_HIZ) &&437isl_surf_get_hiz_surf(&screen->isl_dev, &res->surf, &res->aux.surf);438439const bool has_ccs =440((devinfo->ver >= 7 && !res->mod_info && !(INTEL_DEBUG & DEBUG_NO_RBC)) ||441(res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE)) &&442isl_surf_get_ccs_surf(&screen->isl_dev, &res->surf, NULL,443&res->aux.surf, 0);444445/* Having more than one type of compression is impossible */446assert(has_ccs + has_mcs + has_hiz <= 1);447448if (res->mod_info && has_ccs) {449res->aux.usage = res->mod_info->aux_usage;450} else if (has_mcs) {451res->aux.usage = ISL_AUX_USAGE_MCS;452} else if (has_hiz) {453res->aux.usage = ISL_AUX_USAGE_HIZ;454} else if (has_ccs) {455if (isl_format_supports_ccs_d(devinfo, res->surf.format))456res->aux.usage = ISL_AUX_USAGE_CCS_D;457}458459enum isl_aux_state initial_state = ISL_AUX_STATE_AUX_INVALID;460*aux_size_B = 0;461*alloc_flags = 0;462assert(!res->aux.bo);463464switch (res->aux.usage) {465case ISL_AUX_USAGE_NONE:466/* Having no aux buffer is only okay if there's no modifier with aux. */467res->aux.surf.levels = 0;468return !res->mod_info || res->mod_info->aux_usage == ISL_AUX_USAGE_NONE;469case ISL_AUX_USAGE_HIZ:470initial_state = ISL_AUX_STATE_AUX_INVALID;471break;472case ISL_AUX_USAGE_MCS:473/* The Ivybridge PRM, Vol 2 Part 1 p326 says:474*475* "When MCS buffer is enabled and bound to MSRT, it is required476* that it is cleared prior to any rendering."477*478* Since we only use the MCS buffer for rendering, we just clear it479* immediately on allocation. The clear value for MCS buffers is all480* 1's, so we simply memset it to 0xff.481*/482initial_state = ISL_AUX_STATE_CLEAR;483break;484case ISL_AUX_USAGE_CCS_D:485/* When CCS_E is used, we need to ensure that the CCS starts off in486* a valid state. From the Sky Lake PRM, "MCS Buffer for Render487* Target(s)":488*489* "If Software wants to enable Color Compression without Fast490* clear, Software needs to initialize MCS with zeros."491*492* A CCS value of 0 indicates that the corresponding block is in the493* pass-through state which is what we want.494*495* For CCS_D, do the same thing. On Gen9+, this avoids having any496* undefined bits in the aux buffer.497*/498if (imported)499initial_state =500isl_drm_modifier_get_default_aux_state(res->mod_info->modifier);501else502initial_state = ISL_AUX_STATE_PASS_THROUGH;503*alloc_flags |= BO_ALLOC_ZEROED;504break;505default:506unreachable("non-crocus aux");507}508509/* Create the aux_state for the auxiliary buffer. */510res->aux.state = create_aux_state_map(res, initial_state);511if (!res->aux.state)512return false;513514/* Increase the aux offset if the main and aux surfaces will share a BO. */515res->aux.offset =516!res->mod_info || res->mod_info->aux_usage == res->aux.usage ?517ALIGN(res->surf.size_B, res->aux.surf.alignment_B) : 0;518uint64_t size = res->aux.surf.size_B;519520/* Allocate space in the buffer for storing the clear color. On modern521* platforms (gen > 9), we can read it directly from such buffer.522*523* On gen <= 9, we are going to store the clear color on the buffer524* anyways, and copy it back to the surface state during state emission.525*526* Also add some padding to make sure the fast clear color state buffer527* starts at a 4K alignment. We believe that 256B might be enough, but due528* to lack of testing we will leave this as 4K for now.529*/530size = ALIGN(size, 4096);531*aux_size_B = size;532533if (isl_aux_usage_has_hiz(res->aux.usage)) {534for (unsigned level = 0; level < res->surf.levels; ++level) {535uint32_t width = u_minify(res->surf.phys_level0_sa.width, level);536uint32_t height = u_minify(res->surf.phys_level0_sa.height, level);537538/* Disable HiZ for LOD > 0 unless the width/height are 8x4 aligned.539* For LOD == 0, we can grow the dimensions to make it work.540*/541if (devinfo->verx10 < 75 ||542(level == 0 || ((width & 7) == 0 && (height & 3) == 0)))543res->aux.has_hiz |= 1 << level;544}545}546547return true;548}549550/**551* Initialize the aux buffer contents.552*553* Returns false on unexpected error (e.g. mapping a BO failed).554*/555static bool556crocus_resource_init_aux_buf(struct crocus_resource *res, uint32_t alloc_flags)557{558if (!(alloc_flags & BO_ALLOC_ZEROED)) {559void *map = crocus_bo_map(NULL, res->aux.bo, MAP_WRITE | MAP_RAW);560561if (!map)562return false;563564if (crocus_resource_get_aux_state(res, 0, 0) != ISL_AUX_STATE_AUX_INVALID) {565uint8_t memset_value = isl_aux_usage_has_mcs(res->aux.usage) ? 0xFF : 0;566memset((char*)map + res->aux.offset, memset_value,567res->aux.surf.size_B);568}569570crocus_bo_unmap(res->aux.bo);571}572573return true;574}575576/**577* Allocate the initial aux surface for a resource based on aux.usage578*579* Returns false on unexpected error (e.g. allocation failed, or invalid580* configuration result).581*/582static bool583crocus_resource_alloc_separate_aux(struct crocus_screen *screen,584struct crocus_resource *res)585{586uint32_t alloc_flags;587uint64_t size;588if (!crocus_resource_configure_aux(screen, res, false, &size, &alloc_flags))589return false;590591if (size == 0)592return true;593594/* Allocate the auxiliary buffer. ISL has stricter set of alignment rules595* the drm allocator. Therefore, one can pass the ISL dimensions in terms596* of bytes instead of trying to recalculate based on different format597* block sizes.598*/599res->aux.bo = crocus_bo_alloc_tiled(screen->bufmgr, "aux buffer", size, 4096,600isl_tiling_to_i915_tiling(res->aux.surf.tiling),601res->aux.surf.row_pitch_B, alloc_flags);602if (!res->aux.bo) {603return false;604}605606if (!crocus_resource_init_aux_buf(res, alloc_flags))607return false;608609return true;610}611612void613crocus_resource_finish_aux_import(struct pipe_screen *pscreen,614struct crocus_resource *res)615{616struct crocus_screen *screen = (struct crocus_screen *)pscreen;617assert(crocus_resource_unfinished_aux_import(res));618assert(!res->mod_info->supports_clear_color);619620struct crocus_resource *aux_res = (void *) res->base.b.next;621assert(aux_res->aux.surf.row_pitch_B && aux_res->aux.offset &&622aux_res->aux.bo);623624assert(res->bo == aux_res->aux.bo);625crocus_bo_reference(aux_res->aux.bo);626res->aux.bo = aux_res->aux.bo;627628res->aux.offset = aux_res->aux.offset;629630assert(res->bo->size >= (res->aux.offset + res->aux.surf.size_B));631assert(aux_res->aux.surf.row_pitch_B == res->aux.surf.row_pitch_B);632633crocus_resource_destroy(&screen->base, res->base.b.next);634res->base.b.next = NULL;635}636637static struct pipe_resource *638crocus_resource_create_for_buffer(struct pipe_screen *pscreen,639const struct pipe_resource *templ)640{641struct crocus_screen *screen = (struct crocus_screen *)pscreen;642struct crocus_resource *res = crocus_alloc_resource(pscreen, templ);643644assert(templ->target == PIPE_BUFFER);645assert(templ->height0 <= 1);646assert(templ->depth0 <= 1);647assert(templ->format == PIPE_FORMAT_NONE ||648util_format_get_blocksize(templ->format) == 1);649650res->internal_format = templ->format;651res->surf.tiling = ISL_TILING_LINEAR;652653const char *name = templ->target == PIPE_BUFFER ? "buffer" : "miptree";654655res->bo = crocus_bo_alloc(screen->bufmgr, name, templ->width0);656if (!res->bo) {657crocus_resource_destroy(pscreen, &res->base.b);658return NULL;659}660661return &res->base.b;662}663664static struct pipe_resource *665crocus_resource_create_with_modifiers(struct pipe_screen *pscreen,666const struct pipe_resource *templ,667const uint64_t *modifiers,668int modifiers_count)669{670struct crocus_screen *screen = (struct crocus_screen *)pscreen;671struct intel_device_info *devinfo = &screen->devinfo;672struct crocus_resource *res = crocus_alloc_resource(pscreen, templ);673674if (!res)675return NULL;676677uint64_t modifier =678select_best_modifier(devinfo, templ, modifiers, modifiers_count);679680if (modifier == DRM_FORMAT_MOD_INVALID && modifiers_count > 0) {681fprintf(stderr, "Unsupported modifier, resource creation failed.\n");682goto fail;683}684685if (templ->usage == PIPE_USAGE_STAGING &&686templ->bind == PIPE_BIND_DEPTH_STENCIL &&687devinfo->ver < 6)688return NULL;689690UNUSED const bool isl_surf_created_successfully =691crocus_resource_configure_main(screen, res, templ, modifier, 0);692assert(isl_surf_created_successfully);693694const char *name = "miptree";695696unsigned int flags = 0;697if (templ->usage == PIPE_USAGE_STAGING)698flags |= BO_ALLOC_COHERENT;699700uint64_t aux_size = 0;701uint32_t aux_preferred_alloc_flags;702703if (!crocus_resource_configure_aux(screen, res, false, &aux_size,704&aux_preferred_alloc_flags)) {705goto fail;706}707708/* Modifiers require the aux data to be in the same buffer as the main709* surface, but we combine them even when a modifiers is not being used.710*/711const uint64_t bo_size =712MAX2(res->surf.size_B, res->aux.offset + aux_size);713uint32_t alignment = MAX2(4096, res->surf.alignment_B);714res->bo = crocus_bo_alloc_tiled(screen->bufmgr, name, bo_size, alignment,715isl_tiling_to_i915_tiling(res->surf.tiling),716res->surf.row_pitch_B, flags);717718if (!res->bo)719goto fail;720721if (aux_size > 0) {722res->aux.bo = res->bo;723crocus_bo_reference(res->aux.bo);724if (!crocus_resource_init_aux_buf(res, flags))725goto fail;726}727728if (templ->format == PIPE_FORMAT_S8_UINT && !(templ->usage == PIPE_USAGE_STAGING) &&729devinfo->ver == 7 && (templ->bind & PIPE_BIND_SAMPLER_VIEW)) {730struct pipe_resource templ_shadow = (struct pipe_resource) {731.usage = 0,732.bind = PIPE_BIND_SAMPLER_VIEW,733.width0 = res->base.b.width0,734.height0 = res->base.b.height0,735.depth0 = res->base.b.depth0,736.last_level = res->base.b.last_level,737.nr_samples = res->base.b.nr_samples,738.nr_storage_samples = res->base.b.nr_storage_samples,739.array_size = res->base.b.array_size,740.format = PIPE_FORMAT_R8_UINT,741.target = res->base.b.target,742};743res->shadow = (struct crocus_resource *)screen->base.resource_create(&screen->base, &templ_shadow);744assert(res->shadow);745}746747return &res->base.b;748749fail:750fprintf(stderr, "XXX: resource creation failed\n");751crocus_resource_destroy(pscreen, &res->base.b);752return NULL;753754}755756static struct pipe_resource *757crocus_resource_create(struct pipe_screen *pscreen,758const struct pipe_resource *templ)759{760if (templ->target == PIPE_BUFFER)761return crocus_resource_create_for_buffer(pscreen, templ);762else763return crocus_resource_create_with_modifiers(pscreen, templ, NULL, 0);764}765766static uint64_t767tiling_to_modifier(uint32_t tiling)768{769static const uint64_t map[] = {770[I915_TILING_NONE] = DRM_FORMAT_MOD_LINEAR,771[I915_TILING_X] = I915_FORMAT_MOD_X_TILED,772[I915_TILING_Y] = I915_FORMAT_MOD_Y_TILED,773};774775assert(tiling < ARRAY_SIZE(map));776777return map[tiling];778}779780static struct pipe_resource *781crocus_resource_from_user_memory(struct pipe_screen *pscreen,782const struct pipe_resource *templ,783void *user_memory)784{785struct crocus_screen *screen = (struct crocus_screen *)pscreen;786struct crocus_bufmgr *bufmgr = screen->bufmgr;787struct crocus_resource *res = crocus_alloc_resource(pscreen, templ);788if (!res)789return NULL;790791assert(templ->target == PIPE_BUFFER);792793res->internal_format = templ->format;794res->bo = crocus_bo_create_userptr(bufmgr, "user",795user_memory, templ->width0);796if (!res->bo) {797free(res);798return NULL;799}800801util_range_add(&res->base.b, &res->valid_buffer_range, 0, templ->width0);802803return &res->base.b;804}805806static struct pipe_resource *807crocus_resource_from_handle(struct pipe_screen *pscreen,808const struct pipe_resource *templ,809struct winsys_handle *whandle,810unsigned usage)811{812assert(templ->target != PIPE_BUFFER);813814struct crocus_screen *screen = (struct crocus_screen *)pscreen;815struct crocus_bufmgr *bufmgr = screen->bufmgr;816struct crocus_resource *res = crocus_alloc_resource(pscreen, templ);817818if (!res)819return NULL;820821switch (whandle->type) {822case WINSYS_HANDLE_TYPE_FD:823res->bo = crocus_bo_import_dmabuf(bufmgr, whandle->handle,824whandle->modifier);825break;826case WINSYS_HANDLE_TYPE_SHARED:827res->bo = crocus_bo_gem_create_from_name(bufmgr, "winsys image",828whandle->handle);829break;830default:831unreachable("invalid winsys handle type");832}833if (!res->bo)834return NULL;835836res->offset = whandle->offset;837res->external_format = whandle->format;838839if (whandle->plane < util_format_get_num_planes(whandle->format)) {840const uint64_t modifier =841whandle->modifier != DRM_FORMAT_MOD_INVALID ?842whandle->modifier : tiling_to_modifier(res->bo->tiling_mode);843844UNUSED const bool isl_surf_created_successfully =845crocus_resource_configure_main(screen, res, templ, modifier,846whandle->stride);847assert(isl_surf_created_successfully);848assert(res->bo->tiling_mode ==849isl_tiling_to_i915_tiling(res->surf.tiling));850851// XXX: create_ccs_buf_for_image?852if (whandle->modifier == DRM_FORMAT_MOD_INVALID) {853if (!crocus_resource_alloc_separate_aux(screen, res))854goto fail;855} else {856if (res->mod_info->aux_usage != ISL_AUX_USAGE_NONE) {857uint32_t alloc_flags;858uint64_t size;859UNUSED bool ok = crocus_resource_configure_aux(screen, res, true, &size,860&alloc_flags);861assert(ok);862/* The gallium dri layer will create a separate plane resource863* for the aux image. crocus_resource_finish_aux_import will864* merge the separate aux parameters back into a single865* crocus_resource.866*/867}868}869} else {870/* Save modifier import information to reconstruct later. After871* import, this will be available under a second image accessible872* from the main image with res->base.next. See873* crocus_resource_finish_aux_import.874*/875res->aux.surf.row_pitch_B = whandle->stride;876res->aux.offset = whandle->offset;877res->aux.bo = res->bo;878res->bo = NULL;879}880881return &res->base.b;882883fail:884crocus_resource_destroy(pscreen, &res->base.b);885return NULL;886}887888static struct pipe_resource *889crocus_resource_from_memobj(struct pipe_screen *pscreen,890const struct pipe_resource *templ,891struct pipe_memory_object *pmemobj,892uint64_t offset)893{894struct crocus_screen *screen = (struct crocus_screen *)pscreen;895struct crocus_memory_object *memobj = (struct crocus_memory_object *)pmemobj;896struct crocus_resource *res = crocus_alloc_resource(pscreen, templ);897898if (!res)899return NULL;900901/* Disable Depth, and combined Depth+Stencil for now. */902if (util_format_has_depth(util_format_description(templ->format)))903return NULL;904905if (templ->flags & PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY) {906UNUSED const bool isl_surf_created_successfully =907crocus_resource_configure_main(screen, res, templ, DRM_FORMAT_MOD_INVALID, 0);908assert(isl_surf_created_successfully);909}910911res->bo = memobj->bo;912res->offset = offset;913res->external_format = memobj->format;914915crocus_bo_reference(memobj->bo);916917return &res->base.b;918}919920static void921crocus_flush_resource(struct pipe_context *ctx, struct pipe_resource *resource)922{923struct crocus_context *ice = (struct crocus_context *)ctx;924struct crocus_resource *res = (void *) resource;925const struct isl_drm_modifier_info *mod = res->mod_info;926927crocus_resource_prepare_access(ice, res,9280, INTEL_REMAINING_LEVELS,9290, INTEL_REMAINING_LAYERS,930mod ? mod->aux_usage : ISL_AUX_USAGE_NONE,931mod ? mod->supports_clear_color : false);932}933934static void935crocus_resource_disable_aux_on_first_query(struct pipe_resource *resource,936unsigned usage)937{938struct crocus_resource *res = (struct crocus_resource *)resource;939bool mod_with_aux =940res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;941942/* Disable aux usage if explicit flush not set and this is the first time943* we are dealing with this resource and the resource was not created with944* a modifier with aux.945*/946if (!mod_with_aux &&947(!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) && res->aux.usage != 0) &&948p_atomic_read(&resource->reference.count) == 1) {949crocus_resource_disable_aux(res);950}951}952953static bool954crocus_resource_get_param(struct pipe_screen *pscreen,955struct pipe_context *context,956struct pipe_resource *resource,957unsigned plane,958unsigned layer,959unsigned level,960enum pipe_resource_param param,961unsigned handle_usage,962uint64_t *value)963{964struct crocus_screen *screen = (struct crocus_screen *)pscreen;965struct crocus_resource *res = (struct crocus_resource *)resource;966bool mod_with_aux =967res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;968bool wants_aux = mod_with_aux && plane > 0;969bool result;970unsigned handle;971972if (crocus_resource_unfinished_aux_import(res))973crocus_resource_finish_aux_import(pscreen, res);974975struct crocus_bo *bo = wants_aux ? res->aux.bo : res->bo;976977crocus_resource_disable_aux_on_first_query(resource, handle_usage);978979switch (param) {980case PIPE_RESOURCE_PARAM_NPLANES:981if (mod_with_aux) {982*value = util_format_get_num_planes(res->external_format);983} else {984unsigned count = 0;985for (struct pipe_resource *cur = resource; cur; cur = cur->next)986count++;987*value = count;988}989return true;990case PIPE_RESOURCE_PARAM_STRIDE:991*value = wants_aux ? res->aux.surf.row_pitch_B : res->surf.row_pitch_B;992return true;993case PIPE_RESOURCE_PARAM_OFFSET:994*value = wants_aux ? res->aux.offset : 0;995return true;996case PIPE_RESOURCE_PARAM_MODIFIER:997*value = res->mod_info ? res->mod_info->modifier :998tiling_to_modifier(isl_tiling_to_i915_tiling(res->surf.tiling));999return true;1000case PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED:1001result = crocus_bo_flink(bo, &handle) == 0;1002if (result)1003*value = handle;1004return result;1005case PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS: {1006/* Because we share the same drm file across multiple crocus_screen, when1007* we export a GEM handle we must make sure it is valid in the DRM file1008* descriptor the caller is using (this is the FD given at screen1009* creation).1010*/1011uint32_t handle;1012if (crocus_bo_export_gem_handle_for_device(bo, screen->winsys_fd, &handle))1013return false;1014*value = handle;1015return true;1016}1017case PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD:1018result = crocus_bo_export_dmabuf(bo, (int *) &handle) == 0;1019if (result)1020*value = handle;1021return result;1022default:1023return false;1024}1025}10261027static bool1028crocus_resource_get_handle(struct pipe_screen *pscreen,1029struct pipe_context *ctx,1030struct pipe_resource *resource,1031struct winsys_handle *whandle,1032unsigned usage)1033{1034struct crocus_screen *screen = (struct crocus_screen *) pscreen;1035struct crocus_resource *res = (struct crocus_resource *)resource;1036bool mod_with_aux =1037res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;10381039crocus_resource_disable_aux_on_first_query(resource, usage);10401041struct crocus_bo *bo;1042if (mod_with_aux && whandle->plane > 0) {1043assert(res->aux.bo);1044bo = res->aux.bo;1045whandle->stride = res->aux.surf.row_pitch_B;1046whandle->offset = res->aux.offset;1047} else {1048/* If this is a buffer, stride should be 0 - no need to special case */1049whandle->stride = res->surf.row_pitch_B;1050bo = res->bo;1051}1052whandle->format = res->external_format;1053whandle->modifier =1054res->mod_info ? res->mod_info->modifier1055: tiling_to_modifier(res->bo->tiling_mode);10561057#ifndef NDEBUG1058enum isl_aux_usage allowed_usage =1059res->mod_info ? res->mod_info->aux_usage : ISL_AUX_USAGE_NONE;10601061if (res->aux.usage != allowed_usage) {1062enum isl_aux_state aux_state = crocus_resource_get_aux_state(res, 0, 0);1063assert(aux_state == ISL_AUX_STATE_RESOLVED ||1064aux_state == ISL_AUX_STATE_PASS_THROUGH);1065}1066#endif10671068switch (whandle->type) {1069case WINSYS_HANDLE_TYPE_SHARED:1070return crocus_bo_flink(bo, &whandle->handle) == 0;1071case WINSYS_HANDLE_TYPE_KMS: {1072/* Because we share the same drm file across multiple crocus_screen, when1073* we export a GEM handle we must make sure it is valid in the DRM file1074* descriptor the caller is using (this is the FD given at screen1075* creation).1076*/1077uint32_t handle;1078if (crocus_bo_export_gem_handle_for_device(bo, screen->winsys_fd, &handle))1079return false;1080whandle->handle = handle;1081return true;1082}1083case WINSYS_HANDLE_TYPE_FD:1084return crocus_bo_export_dmabuf(bo, (int *) &whandle->handle) == 0;1085}10861087return false;1088}10891090static bool1091resource_is_busy(struct crocus_context *ice,1092struct crocus_resource *res)1093{1094bool busy = crocus_bo_busy(res->bo);10951096for (int i = 0; i < ice->batch_count; i++)1097busy |= crocus_batch_references(&ice->batches[i], res->bo);10981099return busy;1100}11011102void1103crocus_replace_buffer_storage(struct pipe_context *ctx,1104struct pipe_resource *p_dst,1105struct pipe_resource *p_src,1106unsigned num_rebinds,1107uint32_t rebind_mask,1108uint32_t delete_buffer_id)1109{1110struct crocus_screen *screen = (void *) ctx->screen;1111struct crocus_context *ice = (void *) ctx;1112struct crocus_resource *dst = (void *) p_dst;1113struct crocus_resource *src = (void *) p_src;11141115assert(memcmp(&dst->surf, &src->surf, sizeof(dst->surf)) == 0);11161117struct crocus_bo *old_bo = dst->bo;11181119/* Swap out the backing storage */1120crocus_bo_reference(src->bo);1121dst->bo = src->bo;11221123/* Rebind the buffer, replacing any state referring to the old BO's1124* address, and marking state dirty so it's reemitted.1125*/1126screen->vtbl.rebind_buffer(ice, dst);11271128crocus_bo_unreference(old_bo);1129}11301131static void1132crocus_invalidate_resource(struct pipe_context *ctx,1133struct pipe_resource *resource)1134{1135struct crocus_screen *screen = (void *) ctx->screen;1136struct crocus_context *ice = (void *) ctx;1137struct crocus_resource *res = (void *) resource;11381139if (resource->target != PIPE_BUFFER)1140return;11411142/* If it's already invalidated, don't bother doing anything. */1143if (res->valid_buffer_range.start > res->valid_buffer_range.end)1144return;11451146if (!resource_is_busy(ice, res)) {1147/* The resource is idle, so just mark that it contains no data and1148* keep using the same underlying buffer object.1149*/1150util_range_set_empty(&res->valid_buffer_range);1151return;1152}11531154/* Otherwise, try and replace the backing storage with a new BO. */11551156/* We can't reallocate memory we didn't allocate in the first place. */1157if (res->bo->userptr)1158return;11591160struct crocus_bo *old_bo = res->bo;1161struct crocus_bo *new_bo =1162crocus_bo_alloc(screen->bufmgr, res->bo->name, resource->width0);11631164if (!new_bo)1165return;11661167/* Swap out the backing storage */1168res->bo = new_bo;11691170/* Rebind the buffer, replacing any state referring to the old BO's1171* address, and marking state dirty so it's reemitted.1172*/1173screen->vtbl.rebind_buffer(ice, res);11741175util_range_set_empty(&res->valid_buffer_range);11761177crocus_bo_unreference(old_bo);1178}11791180static void1181crocus_flush_staging_region(struct pipe_transfer *xfer,1182const struct pipe_box *flush_box)1183{1184if (!(xfer->usage & PIPE_MAP_WRITE))1185return;11861187struct crocus_transfer *map = (void *) xfer;11881189struct pipe_box src_box = *flush_box;11901191/* Account for extra alignment padding in staging buffer */1192if (xfer->resource->target == PIPE_BUFFER)1193src_box.x += xfer->box.x % CROCUS_MAP_BUFFER_ALIGNMENT;11941195struct pipe_box dst_box = (struct pipe_box) {1196.x = xfer->box.x + flush_box->x,1197.y = xfer->box.y + flush_box->y,1198.z = xfer->box.z + flush_box->z,1199.width = flush_box->width,1200.height = flush_box->height,1201.depth = flush_box->depth,1202};12031204crocus_copy_region(map->blorp, map->batch, xfer->resource, xfer->level,1205dst_box.x, dst_box.y, dst_box.z, map->staging, 0,1206&src_box);1207}12081209static void1210crocus_unmap_copy_region(struct crocus_transfer *map)1211{1212crocus_resource_destroy(map->staging->screen, map->staging);12131214map->ptr = NULL;1215}12161217static void1218crocus_map_copy_region(struct crocus_transfer *map)1219{1220struct pipe_screen *pscreen = &map->batch->screen->base;1221struct pipe_transfer *xfer = &map->base.b;1222struct pipe_box *box = &xfer->box;1223struct crocus_resource *res = (void *) xfer->resource;12241225unsigned extra = xfer->resource->target == PIPE_BUFFER ?1226box->x % CROCUS_MAP_BUFFER_ALIGNMENT : 0;12271228struct pipe_resource templ = (struct pipe_resource) {1229.usage = PIPE_USAGE_STAGING,1230.width0 = box->width + extra,1231.height0 = box->height,1232.depth0 = 1,1233.nr_samples = xfer->resource->nr_samples,1234.nr_storage_samples = xfer->resource->nr_storage_samples,1235.array_size = box->depth,1236.format = res->internal_format,1237};12381239if (xfer->resource->target == PIPE_BUFFER)1240templ.target = PIPE_BUFFER;1241else if (templ.array_size > 1)1242templ.target = PIPE_TEXTURE_2D_ARRAY;1243else1244templ.target = PIPE_TEXTURE_2D;12451246map->staging = crocus_resource_create(pscreen, &templ);1247assert(map->staging);12481249if (templ.target != PIPE_BUFFER) {1250struct isl_surf *surf = &((struct crocus_resource *) map->staging)->surf;1251xfer->stride = isl_surf_get_row_pitch_B(surf);1252xfer->layer_stride = isl_surf_get_array_pitch(surf);1253}12541255if (!(xfer->usage & PIPE_MAP_DISCARD_RANGE)) {1256crocus_copy_region(map->blorp, map->batch, map->staging, 0, extra, 0, 0,1257xfer->resource, xfer->level, box);1258/* Ensure writes to the staging BO land before we map it below. */1259crocus_emit_pipe_control_flush(map->batch,1260"transfer read: flush before mapping",1261PIPE_CONTROL_RENDER_TARGET_FLUSH |1262PIPE_CONTROL_CS_STALL);1263}12641265struct crocus_bo *staging_bo = crocus_resource_bo(map->staging);12661267if (crocus_batch_references(map->batch, staging_bo))1268crocus_batch_flush(map->batch);12691270map->ptr =1271crocus_bo_map(map->dbg, staging_bo, xfer->usage & MAP_FLAGS) + extra;12721273map->unmap = crocus_unmap_copy_region;1274}12751276static void1277get_image_offset_el(const struct isl_surf *surf, unsigned level, unsigned z,1278unsigned *out_x0_el, unsigned *out_y0_el)1279{1280ASSERTED uint32_t z0_el, a0_el;1281if (surf->dim == ISL_SURF_DIM_3D) {1282isl_surf_get_image_offset_el(surf, level, 0, z,1283out_x0_el, out_y0_el, &z0_el, &a0_el);1284} else {1285isl_surf_get_image_offset_el(surf, level, z, 0,1286out_x0_el, out_y0_el, &z0_el, &a0_el);1287}1288assert(z0_el == 0 && a0_el == 0);1289}12901291void1292crocus_resource_get_image_offset(struct crocus_resource *res,1293uint32_t level, uint32_t z,1294uint32_t *x, uint32_t *y)1295{1296get_image_offset_el(&res->surf, level, z, x, y);1297}12981299/**1300* Get pointer offset into stencil buffer.1301*1302* The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we1303* must decode the tile's layout in software.1304*1305* See1306* - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile1307* Format.1308* - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm1309*1310* Even though the returned offset is always positive, the return type is1311* signed due to1312* commit e8b1c6d6f55f5be3bef25084fdd8b6127517e1371313* mesa: Fix return type of _mesa_get_format_bytes() (#37351)1314*/1315static intptr_t1316s8_offset(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)1317{1318uint32_t tile_size = 4096;1319uint32_t tile_width = 64;1320uint32_t tile_height = 64;1321uint32_t row_size = 64 * stride / 2; /* Two rows are interleaved. */13221323uint32_t tile_x = x / tile_width;1324uint32_t tile_y = y / tile_height;13251326/* The byte's address relative to the tile's base addres. */1327uint32_t byte_x = x % tile_width;1328uint32_t byte_y = y % tile_height;13291330uintptr_t u = tile_y * row_size1331+ tile_x * tile_size1332+ 512 * (byte_x / 8)1333+ 64 * (byte_y / 8)1334+ 32 * ((byte_y / 4) % 2)1335+ 16 * ((byte_x / 4) % 2)1336+ 8 * ((byte_y / 2) % 2)1337+ 4 * ((byte_x / 2) % 2)1338+ 2 * (byte_y % 2)1339+ 1 * (byte_x % 2);13401341if (swizzled) {1342/* adjust for bit6 swizzling */1343if (((byte_x / 8) % 2) == 1) {1344if (((byte_y / 8) % 2) == 0) {1345u += 64;1346} else {1347u -= 64;1348}1349}1350}13511352return u;1353}13541355static void1356crocus_unmap_s8(struct crocus_transfer *map)1357{1358struct pipe_transfer *xfer = &map->base.b;1359const struct pipe_box *box = &xfer->box;1360struct crocus_resource *res = (struct crocus_resource *) xfer->resource;1361struct isl_surf *surf = &res->surf;13621363if (xfer->usage & PIPE_MAP_WRITE) {1364uint8_t *untiled_s8_map = map->ptr;1365uint8_t *tiled_s8_map =1366crocus_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);13671368for (int s = 0; s < box->depth; s++) {1369unsigned x0_el, y0_el;1370get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);13711372for (uint32_t y = 0; y < box->height; y++) {1373for (uint32_t x = 0; x < box->width; x++) {1374ptrdiff_t offset = s8_offset(surf->row_pitch_B,1375x0_el + box->x + x,1376y0_el + box->y + y,1377map->has_swizzling);1378tiled_s8_map[offset] =1379untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x];1380}1381}1382}1383}13841385free(map->buffer);1386}13871388static void1389crocus_map_s8(struct crocus_transfer *map)1390{1391struct pipe_transfer *xfer = &map->base.b;1392const struct pipe_box *box = &xfer->box;1393struct crocus_resource *res = (struct crocus_resource *) xfer->resource;1394struct isl_surf *surf = &res->surf;13951396xfer->stride = surf->row_pitch_B;1397xfer->layer_stride = xfer->stride * box->height;13981399/* The tiling and detiling functions require that the linear buffer has1400* a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we1401* over-allocate the linear buffer to get the proper alignment.1402*/1403map->buffer = map->ptr = malloc(xfer->layer_stride * box->depth);1404assert(map->buffer);14051406/* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no1407* INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless1408* invalidate is set, since we'll be writing the whole rectangle from our1409* temporary buffer back out.1410*/1411if (!(xfer->usage & PIPE_MAP_DISCARD_RANGE)) {1412uint8_t *untiled_s8_map = map->ptr;1413uint8_t *tiled_s8_map =1414crocus_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);14151416for (int s = 0; s < box->depth; s++) {1417unsigned x0_el, y0_el;1418get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);14191420for (uint32_t y = 0; y < box->height; y++) {1421for (uint32_t x = 0; x < box->width; x++) {1422ptrdiff_t offset = s8_offset(surf->row_pitch_B,1423x0_el + box->x + x,1424y0_el + box->y + y,1425map->has_swizzling);1426untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x] =1427tiled_s8_map[offset];1428}1429}1430}1431}14321433map->unmap = crocus_unmap_s8;1434}14351436/* Compute extent parameters for use with tiled_memcpy functions.1437* xs are in units of bytes and ys are in units of strides.1438*/1439static inline void1440tile_extents(const struct isl_surf *surf,1441const struct pipe_box *box,1442unsigned level, int z,1443unsigned *x1_B, unsigned *x2_B,1444unsigned *y1_el, unsigned *y2_el)1445{1446const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);1447const unsigned cpp = fmtl->bpb / 8;14481449assert(box->x % fmtl->bw == 0);1450assert(box->y % fmtl->bh == 0);14511452unsigned x0_el, y0_el;1453get_image_offset_el(surf, level, box->z + z, &x0_el, &y0_el);14541455*x1_B = (box->x / fmtl->bw + x0_el) * cpp;1456*y1_el = box->y / fmtl->bh + y0_el;1457*x2_B = (DIV_ROUND_UP(box->x + box->width, fmtl->bw) + x0_el) * cpp;1458*y2_el = DIV_ROUND_UP(box->y + box->height, fmtl->bh) + y0_el;1459}14601461static void1462crocus_unmap_tiled_memcpy(struct crocus_transfer *map)1463{1464struct pipe_transfer *xfer = &map->base.b;1465const struct pipe_box *box = &xfer->box;1466struct crocus_resource *res = (struct crocus_resource *) xfer->resource;1467struct isl_surf *surf = &res->surf;14681469if (xfer->usage & PIPE_MAP_WRITE) {1470char *dst =1471crocus_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);14721473for (int s = 0; s < box->depth; s++) {1474unsigned x1, x2, y1, y2;1475tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);14761477void *ptr = map->ptr + s * xfer->layer_stride;14781479isl_memcpy_linear_to_tiled(x1, x2, y1, y2, dst, ptr,1480surf->row_pitch_B, xfer->stride,1481map->has_swizzling,1482surf->tiling, ISL_MEMCPY);1483}1484}1485os_free_aligned(map->buffer);1486map->buffer = map->ptr = NULL;1487}14881489static void1490crocus_map_tiled_memcpy(struct crocus_transfer *map)1491{1492struct pipe_transfer *xfer = &map->base.b;1493const struct pipe_box *box = &xfer->box;1494struct crocus_resource *res = (struct crocus_resource *) xfer->resource;1495struct isl_surf *surf = &res->surf;14961497xfer->stride = ALIGN(surf->row_pitch_B, 16);1498xfer->layer_stride = xfer->stride * box->height;14991500unsigned x1, x2, y1, y2;1501tile_extents(surf, box, xfer->level, 0, &x1, &x2, &y1, &y2);15021503/* The tiling and detiling functions require that the linear buffer has1504* a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we1505* over-allocate the linear buffer to get the proper alignment.1506*/1507map->buffer =1508os_malloc_aligned(xfer->layer_stride * box->depth, 16);1509assert(map->buffer);1510map->ptr = (char *)map->buffer + (x1 & 0xf);15111512if (!(xfer->usage & PIPE_MAP_DISCARD_RANGE)) {1513char *src =1514crocus_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);15151516for (int s = 0; s < box->depth; s++) {1517unsigned x1, x2, y1, y2;1518tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);15191520/* Use 's' rather than 'box->z' to rebase the first slice to 0. */1521void *ptr = map->ptr + s * xfer->layer_stride;15221523isl_memcpy_tiled_to_linear(x1, x2, y1, y2, ptr, src, xfer->stride,1524surf->row_pitch_B,1525map->has_swizzling,1526surf->tiling,1527#if defined(USE_SSE41)1528util_get_cpu_caps()->has_sse4_1 ? ISL_MEMCPY_STREAMING_LOAD :1529#endif1530ISL_MEMCPY);1531}1532}15331534map->unmap = crocus_unmap_tiled_memcpy;1535}15361537static void1538crocus_map_direct(struct crocus_transfer *map)1539{1540struct pipe_transfer *xfer = &map->base.b;1541struct pipe_box *box = &xfer->box;1542struct crocus_resource *res = (struct crocus_resource *) xfer->resource;15431544void *ptr = crocus_bo_map(map->dbg, res->bo, xfer->usage & MAP_FLAGS);15451546if (res->base.b.target == PIPE_BUFFER) {1547xfer->stride = 0;1548xfer->layer_stride = 0;15491550map->ptr = ptr + box->x;1551} else {1552struct isl_surf *surf = &res->surf;1553const struct isl_format_layout *fmtl =1554isl_format_get_layout(surf->format);1555const unsigned cpp = fmtl->bpb / 8;1556unsigned x0_el, y0_el;15571558assert(box->x % fmtl->bw == 0);1559assert(box->y % fmtl->bh == 0);1560get_image_offset_el(surf, xfer->level, box->z, &x0_el, &y0_el);15611562x0_el += box->x / fmtl->bw;1563y0_el += box->y / fmtl->bh;15641565xfer->stride = isl_surf_get_row_pitch_B(surf);1566xfer->layer_stride = isl_surf_get_array_pitch(surf);15671568map->ptr = ptr + y0_el * xfer->stride + x0_el * cpp;1569}1570}15711572static bool1573can_promote_to_async(const struct crocus_resource *res,1574const struct pipe_box *box,1575unsigned usage)1576{1577/* If we're writing to a section of the buffer that hasn't even been1578* initialized with useful data, then we can safely promote this write1579* to be unsynchronized. This helps the common pattern of appending data.1580*/1581return res->base.b.target == PIPE_BUFFER && (usage & PIPE_MAP_WRITE) &&1582!(usage & TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED) &&1583!util_ranges_intersect(&res->valid_buffer_range, box->x,1584box->x + box->width);1585}15861587static void *1588crocus_transfer_map(struct pipe_context *ctx,1589struct pipe_resource *resource,1590unsigned level,1591unsigned usage,1592const struct pipe_box *box,1593struct pipe_transfer **ptransfer)1594{1595struct crocus_context *ice = (struct crocus_context *)ctx;1596struct crocus_resource *res = (struct crocus_resource *)resource;1597struct isl_surf *surf = &res->surf;1598struct crocus_screen *screen = (struct crocus_screen *)ctx->screen;15991600if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) {1601/* Replace the backing storage with a fresh buffer for non-async maps */1602if (!(usage & (PIPE_MAP_UNSYNCHRONIZED |1603TC_TRANSFER_MAP_NO_INVALIDATE)))1604crocus_invalidate_resource(ctx, resource);16051606/* If we can discard the whole resource, we can discard the range. */1607usage |= PIPE_MAP_DISCARD_RANGE;1608}16091610if (!(usage & PIPE_MAP_UNSYNCHRONIZED) &&1611can_promote_to_async(res, box, usage)) {1612usage |= PIPE_MAP_UNSYNCHRONIZED;1613}16141615bool map_would_stall = false;16161617if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {1618map_would_stall = resource_is_busy(ice, res) ||1619crocus_has_invalid_primary(res, level, 1, box->z, box->depth);162016211622if (map_would_stall && (usage & PIPE_MAP_DONTBLOCK) &&1623(usage & PIPE_MAP_DIRECTLY))1624return NULL;1625}16261627if (surf->tiling != ISL_TILING_LINEAR &&1628(usage & PIPE_MAP_DIRECTLY))1629return NULL;16301631struct crocus_transfer *map;1632if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)1633map = slab_alloc(&ice->transfer_pool_unsync);1634else1635map = slab_alloc(&ice->transfer_pool);16361637struct pipe_transfer *xfer = &map->base.b;16381639if (!map)1640return NULL;16411642memset(map, 0, sizeof(*map));1643map->dbg = &ice->dbg;16441645map->has_swizzling = ((struct crocus_screen *)ctx->screen)->has_swizzling;1646pipe_resource_reference(&xfer->resource, resource);1647xfer->level = level;1648xfer->usage = usage;1649xfer->box = *box;1650*ptransfer = xfer;16511652map->dest_had_defined_contents =1653util_ranges_intersect(&res->valid_buffer_range, box->x,1654box->x + box->width);16551656if (usage & PIPE_MAP_WRITE)1657util_range_add(&res->base.b, &res->valid_buffer_range, box->x, box->x + box->width);16581659/* Avoid using GPU copies for persistent/coherent buffers, as the idea1660* there is to access them simultaneously on the CPU & GPU. This also1661* avoids trying to use GPU copies for our u_upload_mgr buffers which1662* contain state we're constructing for a GPU draw call, which would1663* kill us with infinite stack recursion.1664*/1665bool no_gpu = usage & (PIPE_MAP_PERSISTENT |1666PIPE_MAP_COHERENT |1667PIPE_MAP_DIRECTLY);16681669/* GPU copies are not useful for buffer reads. Instead of stalling to1670* read from the original buffer, we'd simply copy it to a temporary...1671* then stall (a bit longer) to read from that buffer.1672*1673* Images are less clear-cut. Color resolves are destructive, removing1674* the underlying compression, so we'd rather blit the data to a linear1675* temporary and map that, to avoid the resolve. (It might be better to1676* a tiled temporary and use the tiled_memcpy paths...)1677*/1678if (!(usage & PIPE_MAP_DISCARD_RANGE) &&1679!crocus_has_invalid_primary(res, level, 1, box->z, box->depth))1680no_gpu = true;16811682const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);1683if (fmtl->txc == ISL_TXC_ASTC)1684no_gpu = true;16851686if (map_would_stall && !no_gpu) {1687/* If we need a synchronous mapping and the resource is busy, or needs1688* resolving, we copy to/from a linear temporary buffer using the GPU.1689*/1690map->batch = &ice->batches[CROCUS_BATCH_RENDER];1691map->blorp = &ice->blorp;1692crocus_map_copy_region(map);1693} else {1694/* Otherwise we're free to map on the CPU. */16951696if (resource->target != PIPE_BUFFER) {1697crocus_resource_access_raw(ice, res,1698level, box->z, box->depth,1699usage & PIPE_MAP_WRITE);1700}17011702if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {1703for (int i = 0; i < ice->batch_count; i++) {1704if (crocus_batch_references(&ice->batches[i], res->bo))1705crocus_batch_flush(&ice->batches[i]);1706}1707}17081709if (surf->tiling == ISL_TILING_W) {1710/* TODO: Teach crocus_map_tiled_memcpy about W-tiling... */1711crocus_map_s8(map);1712} else if (surf->tiling != ISL_TILING_LINEAR && screen->devinfo.ver > 4) {1713crocus_map_tiled_memcpy(map);1714} else {1715crocus_map_direct(map);1716}1717}17181719return map->ptr;1720}17211722static void1723crocus_transfer_flush_region(struct pipe_context *ctx,1724struct pipe_transfer *xfer,1725const struct pipe_box *box)1726{1727struct crocus_context *ice = (struct crocus_context *)ctx;1728struct crocus_resource *res = (struct crocus_resource *) xfer->resource;1729struct crocus_transfer *map = (void *) xfer;17301731if (map->staging)1732crocus_flush_staging_region(xfer, box);17331734uint32_t history_flush = 0;17351736if (res->base.b.target == PIPE_BUFFER) {1737if (map->staging)1738history_flush |= PIPE_CONTROL_RENDER_TARGET_FLUSH;17391740if (map->dest_had_defined_contents)1741history_flush |= crocus_flush_bits_for_history(res);17421743util_range_add(&res->base.b, &res->valid_buffer_range, box->x, box->x + box->width);1744}17451746if (history_flush & ~PIPE_CONTROL_CS_STALL) {1747for (int i = 0; i < ice->batch_count; i++) {1748struct crocus_batch *batch = &ice->batches[i];17491750if (!batch->command.bo)1751continue;1752if (batch->contains_draw || batch->cache.render->entries) {1753crocus_batch_maybe_flush(batch, 24);1754crocus_emit_pipe_control_flush(batch,1755"cache history: transfer flush",1756history_flush);1757}1758}1759}17601761/* Make sure we flag constants dirty even if there's no need to emit1762* any PIPE_CONTROLs to a batch.1763*/1764crocus_dirty_for_history(ice, res);1765}17661767static void1768crocus_transfer_unmap(struct pipe_context *ctx, struct pipe_transfer *xfer)1769{1770struct crocus_context *ice = (struct crocus_context *)ctx;1771struct crocus_transfer *map = (void *) xfer;17721773if (!(xfer->usage & (PIPE_MAP_FLUSH_EXPLICIT |1774PIPE_MAP_COHERENT))) {1775struct pipe_box flush_box = {1776.x = 0, .y = 0, .z = 0,1777.width = xfer->box.width,1778.height = xfer->box.height,1779.depth = xfer->box.depth,1780};1781crocus_transfer_flush_region(ctx, xfer, &flush_box);1782}17831784if (map->unmap)1785map->unmap(map);17861787pipe_resource_reference(&xfer->resource, NULL);1788/* transfer_unmap is always called from the driver thread, so we have to1789* use transfer_pool, not transfer_pool_unsync. Freeing an object into a1790* different pool is allowed, however.1791*/1792slab_free(&ice->transfer_pool, map);1793}17941795/**1796* Mark state dirty that needs to be re-emitted when a resource is written.1797*/1798void1799crocus_dirty_for_history(struct crocus_context *ice,1800struct crocus_resource *res)1801{1802uint64_t stage_dirty = 0ull;18031804if (res->bind_history & PIPE_BIND_CONSTANT_BUFFER) {1805stage_dirty |= ((uint64_t)res->bind_stages) << CROCUS_SHIFT_FOR_STAGE_DIRTY_CONSTANTS;1806}18071808ice->state.stage_dirty |= stage_dirty;1809}18101811/**1812* Produce a set of PIPE_CONTROL bits which ensure data written to a1813* resource becomes visible, and any stale read cache data is invalidated.1814*/1815uint32_t1816crocus_flush_bits_for_history(struct crocus_resource *res)1817{1818uint32_t flush = PIPE_CONTROL_CS_STALL;18191820if (res->bind_history & PIPE_BIND_CONSTANT_BUFFER) {1821flush |= PIPE_CONTROL_CONST_CACHE_INVALIDATE |1822PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;1823}18241825if (res->bind_history & PIPE_BIND_SAMPLER_VIEW)1826flush |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;18271828if (res->bind_history & (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER))1829flush |= PIPE_CONTROL_VF_CACHE_INVALIDATE;18301831if (res->bind_history & (PIPE_BIND_SHADER_BUFFER | PIPE_BIND_SHADER_IMAGE))1832flush |= PIPE_CONTROL_DATA_CACHE_FLUSH;18331834return flush;1835}18361837void1838crocus_flush_and_dirty_for_history(struct crocus_context *ice,1839struct crocus_batch *batch,1840struct crocus_resource *res,1841uint32_t extra_flags,1842const char *reason)1843{1844if (res->base.b.target != PIPE_BUFFER)1845return;18461847uint32_t flush = crocus_flush_bits_for_history(res) | extra_flags;18481849crocus_emit_pipe_control_flush(batch, reason, flush);18501851crocus_dirty_for_history(ice, res);1852}18531854bool1855crocus_resource_set_clear_color(struct crocus_context *ice,1856struct crocus_resource *res,1857union isl_color_value color)1858{1859if (memcmp(&res->aux.clear_color, &color, sizeof(color)) != 0) {1860res->aux.clear_color = color;1861return true;1862}18631864return false;1865}18661867union isl_color_value1868crocus_resource_get_clear_color(const struct crocus_resource *res)1869{1870assert(res->aux.bo);18711872return res->aux.clear_color;1873}18741875static enum pipe_format1876crocus_resource_get_internal_format(struct pipe_resource *p_res)1877{1878struct crocus_resource *res = (void *) p_res;1879return res->internal_format;1880}18811882static const struct u_transfer_vtbl transfer_vtbl = {1883.resource_create = crocus_resource_create,1884.resource_destroy = crocus_resource_destroy,1885.transfer_map = crocus_transfer_map,1886.transfer_unmap = crocus_transfer_unmap,1887.transfer_flush_region = crocus_transfer_flush_region,1888.get_internal_format = crocus_resource_get_internal_format,1889.set_stencil = crocus_resource_set_separate_stencil,1890.get_stencil = crocus_resource_get_separate_stencil,1891};18921893static bool1894crocus_is_dmabuf_modifier_supported(struct pipe_screen *pscreen,1895uint64_t modifier, enum pipe_format pfmt,1896bool *external_only)1897{1898struct crocus_screen *screen = (void *) pscreen;1899const struct intel_device_info *devinfo = &screen->devinfo;19001901if (modifier_is_supported(devinfo, pfmt, 0, modifier)) {1902if (external_only)1903*external_only = false;19041905return true;1906}19071908return false;1909}19101911static unsigned int1912crocus_get_dmabuf_modifier_planes(struct pipe_screen *pscreen, uint64_t modifier,1913enum pipe_format format)1914{1915return util_format_get_num_planes(format);1916}19171918static struct pipe_memory_object *1919crocus_memobj_create_from_handle(struct pipe_screen *pscreen,1920struct winsys_handle *whandle,1921bool dedicated)1922{1923struct crocus_screen *screen = (struct crocus_screen *)pscreen;1924struct crocus_memory_object *memobj = CALLOC_STRUCT(crocus_memory_object);1925struct crocus_bo *bo;1926const struct isl_drm_modifier_info *mod_inf;19271928if (!memobj)1929return NULL;19301931switch (whandle->type) {1932case WINSYS_HANDLE_TYPE_SHARED:1933bo = crocus_bo_gem_create_from_name(screen->bufmgr, "winsys image",1934whandle->handle);1935break;1936case WINSYS_HANDLE_TYPE_FD:1937mod_inf = isl_drm_modifier_get_info(whandle->modifier);1938if (mod_inf) {1939bo = crocus_bo_import_dmabuf(screen->bufmgr, whandle->handle,1940whandle->modifier);1941} else {1942/* If we can't get information about the tiling from the1943* kernel we ignore it. We are going to set it when we1944* create the resource.1945*/1946bo = crocus_bo_import_dmabuf_no_mods(screen->bufmgr,1947whandle->handle);1948}19491950break;1951default:1952unreachable("invalid winsys handle type");1953}19541955if (!bo) {1956free(memobj);1957return NULL;1958}19591960memobj->b.dedicated = dedicated;1961memobj->bo = bo;1962memobj->format = whandle->format;1963memobj->stride = whandle->stride;19641965return &memobj->b;1966}19671968static void1969crocus_memobj_destroy(struct pipe_screen *pscreen,1970struct pipe_memory_object *pmemobj)1971{1972struct crocus_memory_object *memobj = (struct crocus_memory_object *)pmemobj;19731974crocus_bo_unreference(memobj->bo);1975free(memobj);1976}19771978void1979crocus_init_screen_resource_functions(struct pipe_screen *pscreen)1980{1981struct crocus_screen *screen = (void *) pscreen;1982pscreen->query_dmabuf_modifiers = crocus_query_dmabuf_modifiers;1983pscreen->is_dmabuf_modifier_supported = crocus_is_dmabuf_modifier_supported;1984pscreen->get_dmabuf_modifier_planes = crocus_get_dmabuf_modifier_planes;1985pscreen->resource_create_with_modifiers =1986crocus_resource_create_with_modifiers;1987pscreen->resource_create = u_transfer_helper_resource_create;1988pscreen->resource_from_user_memory = crocus_resource_from_user_memory;1989pscreen->resource_from_handle = crocus_resource_from_handle;1990pscreen->resource_from_memobj = crocus_resource_from_memobj;1991pscreen->resource_get_handle = crocus_resource_get_handle;1992pscreen->resource_get_param = crocus_resource_get_param;1993pscreen->resource_destroy = u_transfer_helper_resource_destroy;1994pscreen->memobj_create_from_handle = crocus_memobj_create_from_handle;1995pscreen->memobj_destroy = crocus_memobj_destroy;1996pscreen->transfer_helper =1997u_transfer_helper_create(&transfer_vtbl, screen->devinfo.ver >= 6,1998screen->devinfo.ver >= 6, false, true);1999}20002001void2002crocus_init_resource_functions(struct pipe_context *ctx)2003{2004ctx->flush_resource = crocus_flush_resource;2005ctx->invalidate_resource = crocus_invalidate_resource;2006ctx->buffer_map = u_transfer_helper_transfer_map;2007ctx->texture_map = u_transfer_helper_transfer_map;2008ctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;2009ctx->buffer_unmap = u_transfer_helper_transfer_unmap;2010ctx->texture_unmap = u_transfer_helper_transfer_unmap;2011ctx->buffer_subdata = u_default_buffer_subdata;2012ctx->texture_subdata = u_default_texture_subdata;2013}201420152016