Path: blob/21.2-virgl/src/panfrost/lib/pan_samples.c
4560 views
/*1* Copyright (C) 2019 Collabora, Ltd.2*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 (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*/2223#include "pan_device.h"24#include "pan_bo.h"2526/* Sample positions are specified partially in hardware, partially in software27* on Mali. On Midgard, sample positions are completely fixed but need to be28* software accessible to implement gl_SamplePosition. On Bifrost, sample29* positions are part fixed, part programmable, and again need to be accessible30* for gl_SamplePosition. The mali_sample_position data structure is31* hardware-defined on Bifrost as a packed 8:8 fixed point format. The32* mali_sample_positions array is part-hardware, part-software-defined: it must33* give the sample position when indexed by the sample index, as well as34* reading back the origin when indexed by 32.35*36* The upshot is all of this is known at compile-time, so we can just hardcode37* the LUT, upload it to GPU memory on device initialization, and forget about38* it.39*/4041struct mali_sample_position {42uint16_t x, y;43} __attribute__((packed));4445struct mali_sample_positions {46struct mali_sample_position positions[32];47struct mali_sample_position origin;48struct mali_sample_position padding[64 - (32 + 1)];49} __attribute__((packed));5051/* SAMPLE16 constructs a single sample in terms of 1/16's of the grid, centered52* at the origin. SAMPLE4/8 swap the units for legibility. */5354#define SAMPLE16(x, y) { \55(((x) + 8) * (256 / 16)), \56(((y) + 8) * (256 / 16)) \57}5859#define SAMPLE8(x, y) SAMPLE16((x) * 2, (y) * 2)60#define SAMPLE4(x, y) SAMPLE16((x) * 4, (y) * 4)6162const struct mali_sample_positions sample_position_lut[] = {63[MALI_SAMPLE_PATTERN_SINGLE_SAMPLED] = {64.positions = {65SAMPLE4(0, 0)66},67.origin = SAMPLE4(0, 0)68},6970[MALI_SAMPLE_PATTERN_ORDERED_4X_GRID] = {71.positions = {72SAMPLE4(-1, -1),73SAMPLE4( 1, -1),74SAMPLE4(-1, 1),75SAMPLE4( 1, 1),76},77.origin = SAMPLE4(0, 0)78},7980[MALI_SAMPLE_PATTERN_ROTATED_4X_GRID] = {81.positions = {82SAMPLE8(-1, -3),83SAMPLE8( 3, -1),84SAMPLE8(-3, 1),85SAMPLE8( 1, 3),86},87.origin = SAMPLE8(0, 0)88},8990[MALI_SAMPLE_PATTERN_D3D_8X_GRID] = {91.positions = {92SAMPLE16( 1, -3),93SAMPLE16(-1, 3),94SAMPLE16( 5, 1),95SAMPLE16(-3, -5),96SAMPLE16(-5, 5),97SAMPLE16(-7, -1),98SAMPLE16( 3, 7),99SAMPLE16( 7, -7),100},101.origin = SAMPLE16(0, 0)102},103104[MALI_SAMPLE_PATTERN_D3D_16X_GRID] = {105.positions = {106SAMPLE16( 1, 1),107SAMPLE16(-1, -3),108SAMPLE16(-3, 2),109SAMPLE16( 4, -1),110SAMPLE16(-5, -2),111SAMPLE16( 2, 5),112SAMPLE16( 5, 3),113SAMPLE16( 3, -5),114SAMPLE16(-2, 6),115SAMPLE16( 0, 7),116SAMPLE16(-4, -6),117SAMPLE16(-6, 4),118SAMPLE16(-8, 0),119SAMPLE16( 7, -4),120SAMPLE16( 6, 7),121SAMPLE16(-7, -8),122123},124.origin = SAMPLE16(0, 0)125}126};127128mali_ptr129panfrost_sample_positions(const struct panfrost_device *dev,130enum mali_sample_pattern pattern)131{132assert(pattern < ARRAY_SIZE(sample_position_lut));133unsigned offset = (pattern * sizeof(sample_position_lut[0]));134return dev->sample_positions->ptr.gpu + offset;135}136137void138panfrost_upload_sample_positions(struct panfrost_device *dev)139{140STATIC_ASSERT(sizeof(sample_position_lut) < 4096);141dev->sample_positions = panfrost_bo_create(dev, 4096, 0, "Sample positions");142143memcpy(dev->sample_positions->ptr.cpu, sample_position_lut,144sizeof(sample_position_lut));145}146147/* CPU side LUT query, to implement glGetMultisamplefv */148149void150panfrost_query_sample_position(151enum mali_sample_pattern pattern,152unsigned sample_idx,153float *out)154{155struct mali_sample_position pos =156sample_position_lut[pattern].positions[sample_idx];157158out[0] = DECODE_FIXED_16(pos.x);159out[1] = DECODE_FIXED_16(pos.y);160}161162163