CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/GPU/Common/SplineCommon.h
Views: 1401
// Copyright (c) 2013- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once18#include <unordered_map>1920#include "Common/CommonTypes.h"21#include "Common/Swap.h"22#include "GPU/Math3D.h"23#include "GPU/ge_constants.h"24#include "Core/Config.h"2526#define HALF_CEIL(x) (x + 1) / 2 // Integer ceil = (int)ceil((float)x / 2.0f)2728// PSP compatible format so we can use the end of the pipeline in beziers etc29// 8 + 4 + 12 + 12 = 36 bytes30struct SimpleVertex {31float uv[2];32union {33u8 color[4];34u32_le color_32;35};36Vec3Packedf nrm;37Vec3Packedf pos;38};3940class SimpleBufferManager;4142namespace Spline {4344void BuildIndex(u16 *indices, int &count, int num_u, int num_v, GEPatchPrimType prim_type, int total = 0);4546enum SplineQuality {47LOW_QUALITY = 0,48MEDIUM_QUALITY = 1,49HIGH_QUALITY = 2,50};5152class Bezier3DWeight;53class Spline3DWeight;5455// We decode all vertices into a common format for easy interpolation and stuff.56// Not fast but can be optimized later.5758struct SurfaceInfo {59int tess_u, tess_v;60int num_points_u, num_points_v;61int num_patches_u, num_patches_v;62int type_u, type_v;63GEPatchPrimType primType;64bool patchFacing;6566void BaseInit() {67// If specified as 0, uses 1.68if (tess_u < 1) tess_u = 1;69if (tess_v < 1) tess_v = 1;7071switch (g_Config.iSplineBezierQuality) {72case LOW_QUALITY:73tess_u = 2;74tess_v = 2;75break;76case MEDIUM_QUALITY:77// Don't cut below 2, though.78if (tess_u > 2) tess_u = HALF_CEIL(tess_u);79if (tess_v > 2) tess_v = HALF_CEIL(tess_v);80break;81}82}83};8485struct BezierSurface : public SurfaceInfo {86using WeightType = Bezier3DWeight;8788int num_verts_per_patch;8990void Init(int maxVertices) {91SurfaceInfo::BaseInit();92// Downsample until it fits, in case crazy tessellation factors are sent.93while ((tess_u + 1) * (tess_v + 1) * num_patches_u * num_patches_v > maxVertices) {94tess_u--;95tess_v--;96}97num_verts_per_patch = (tess_u + 1) * (tess_v + 1);98}99100int GetTessStart(int patch) const { return 0; }101102int GetPointIndex(int patch_u, int patch_v) const { return patch_v * 3 * num_points_u + patch_u * 3; }103104int GetIndexU(int patch_u, int tile_u) const { return tile_u; }105int GetIndexV(int patch_v, int tile_v) const { return tile_v; }106107int GetIndex(int index_u, int index_v, int patch_u, int patch_v) const {108int patch_index = patch_v * num_patches_u + patch_u;109return index_v * (tess_u + 1) + index_u + num_verts_per_patch * patch_index;110}111112void BuildIndex(u16 *indices, int &count) const {113for (int patch_u = 0; patch_u < num_patches_u; ++patch_u) {114for (int patch_v = 0; patch_v < num_patches_v; ++patch_v) {115int patch_index = patch_v * num_patches_u + patch_u;116int total = patch_index * num_verts_per_patch;117Spline::BuildIndex(indices + count, count, tess_u, tess_v, primType, total);118}119}120}121};122123struct SplineSurface : public SurfaceInfo {124using WeightType = Spline3DWeight;125126int num_vertices_u;127128void Init(int maxVertices) {129SurfaceInfo::BaseInit();130// Downsample until it fits, in case crazy tessellation factors are sent.131while ((num_patches_u * tess_u + 1) * (num_patches_v * tess_v + 1) > maxVertices) {132tess_u--;133tess_v--;134}135num_vertices_u = num_patches_u * tess_u + 1;136}137138int GetTessStart(int patch) const { return (patch == 0) ? 0 : 1; }139140int GetPointIndex(int patch_u, int patch_v) const { return patch_v * num_points_u + patch_u; }141142int GetIndexU(int patch_u, int tile_u) const { return patch_u * tess_u + tile_u; }143int GetIndexV(int patch_v, int tile_v) const { return patch_v * tess_v + tile_v; }144145int GetIndex(int index_u, int index_v, int patch_u, int patch_v) const {146return index_v * num_vertices_u + index_u;147}148149void BuildIndex(u16 *indices, int &count) const {150Spline::BuildIndex(indices, count, num_patches_u * tess_u, num_patches_v * tess_v, primType);151}152};153154struct Weight {155float basis[4], deriv[4];156};157158template<class T>159class WeightCache : public T {160private:161std::unordered_map<u32, Weight*> weightsCache;162public:163Weight* operator [] (u32 key) {164Weight *&weights = weightsCache[key];165if (!weights)166weights = T::CalcWeightsAll(key);167return weights;168}169170void Clear() {171for (auto it : weightsCache)172delete[] it.second;173weightsCache.clear();174}175};176177struct Weight2D {178const Weight *u, *v;179int size_u, size_v;180181template<class T>182Weight2D(WeightCache<T> &cache, u32 key_u, u32 key_v) {183u = cache[key_u];184v = (key_u != key_v) ? cache[key_v] : u; // Use same weights if u == v185}186};187188struct ControlPoints {189Vec3f *pos = nullptr;190Vec2f *tex = nullptr;191Vec4f *col = nullptr;192u32_le defcolor;193194ControlPoints() {}195ControlPoints(const SimpleVertex *const *points, int size, SimpleBufferManager &managedBuf);196void Convert(const SimpleVertex *const *points, int size);197bool IsValid() const {198return pos && tex && col;199}200};201202struct OutputBuffers {203SimpleVertex *vertices;204u16 *indices;205int count;206};207208template<class Surface>209void SoftwareTessellation(OutputBuffers &output, const Surface &surface, u32 origVertType, const ControlPoints &points);210211} // namespace Spline212213// Define function object for TemplateParameterDispatcher214#define TEMPLATE_PARAMETER_DISPATCHER_FUNCTION(NAME, FUNCNAME, FUNCTYPE) \215struct NAME { \216template<bool ...Params> \217static FUNCTYPE GetFunc() { \218return &FUNCNAME<Params...>; \219} \220};221222template<typename Func, int NumParams, class Dispatcher>223class TemplateParameterDispatcher {224225/* Store all combinations of template functions into an array */226template<int LoopCount, int Index = 0, bool ...Params>227struct Initializer {228static void Init(Func funcs[]) {229Initializer<LoopCount - 1, (Index << 1) + 1, true, Params...>::Init(funcs); // true230Initializer<LoopCount - 1, (Index << 1) + 0, false, Params...>::Init(funcs); // false231}232};233/* Specialized for terminates the recursive loop */234template<int Index, bool ...Params>235struct Initializer<0, Index, Params...> {236static void Init(Func funcs[]) {237funcs[Index] = Dispatcher::template GetFunc<Params...>(); // Resolve the nested dependent name as template function.238}239};240241private:242Func funcs[1 << NumParams]; /* Function pointers array */243public:244TemplateParameterDispatcher() {245Initializer<NumParams>::Init(funcs);246}247248Func GetFunc(const bool params[]) const {249/* Convert bool parameters to index of the array */250int index = 0;251for (int i = 0; i < NumParams; ++i)252index |= params[i] << i;253254return funcs[index];255}256};257258259