run clang format

This commit is contained in:
olebeck 2025-06-28 03:39:00 +02:00
parent 4744142c9d
commit 7c15d4f9d8
5 changed files with 616 additions and 525 deletions

View File

@ -102,7 +102,7 @@ jobs:
- name: Configure (CMake)
run: |
if [ "${{ matrix.vita }}" = "true" ]; then
PRESET=--preset vita-debug
PRESET="--preset vita-debug"
fi
${{ matrix.cmake-wrapper || '' }} cmake $PRESET -S . -B build -GNinja \
-DCMAKE_BUILD_TYPE=${{ matrix.build-type }} \

View File

@ -1,42 +1,37 @@
#include "memory.h"
#include "utils.h"
#include <psp2/gxm.h>
#include <SDL3/SDL_stdinc.h>
#include <psp2/gxm.h>
static SceUID cdramPoolUID = -1;
static SceClibMspace cdramPool = NULL;
void *patcher_host_alloc(void *user_data, unsigned int size)
void* patcher_host_alloc(void* user_data, unsigned int size)
{
void *mem = SDL_malloc(size);
(void)user_data;
void* mem = SDL_malloc(size);
(void) user_data;
return mem;
}
void patcher_host_free(void *user_data, void *mem)
void patcher_host_free(void* user_data, void* mem)
{
(void)user_data;
(void) user_data;
SDL_free(mem);
}
void* vita_mem_alloc(
unsigned int type,
size_t size,
size_t alignment,
int attribs,
SceUID *uid
) {
void *mem;
void* vita_mem_alloc(unsigned int type, size_t size, size_t alignment, int attribs, SceUID* uid)
{
void* mem;
if (type == SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW) {
size = ALIGN(size, 256 * 1024);
} else if (type == SCE_KERNEL_MEMBLOCK_TYPE_USER_MAIN_PHYCONT_NC_RW) {
}
else if (type == SCE_KERNEL_MEMBLOCK_TYPE_USER_MAIN_PHYCONT_NC_RW) {
size = ALIGN(size, 1024 * 1024);
} else {
}
else {
size = ALIGN(size, 4 * 1024);
}
@ -51,7 +46,7 @@ void* vita_mem_alloc(
return NULL;
}
if (sceGxmMapMemory(mem, size, (SceGxmMemoryAttribFlags)attribs) < 0) {
if (sceGxmMapMemory(mem, size, (SceGxmMemoryAttribFlags) attribs) < 0) {
SDL_Log("sceGxmMapMemory 0x%x 0x%x %d failed", mem, size, attribs);
return NULL;
}
@ -61,7 +56,7 @@ void* vita_mem_alloc(
void vita_mem_free(SceUID uid)
{
void *mem = NULL;
void* mem = NULL;
if (sceKernelGetMemBlockBase(uid, &mem) < 0) {
return;
}
@ -69,9 +64,9 @@ void vita_mem_free(SceUID uid)
sceKernelFreeMemBlock(uid);
}
void *vita_mem_vertex_usse_alloc(unsigned int size, SceUID *uid, unsigned int *usse_offset)
void* vita_mem_vertex_usse_alloc(unsigned int size, SceUID* uid, unsigned int* usse_offset)
{
void *mem = NULL;
void* mem = NULL;
size = ALIGN(size, 4096);
*uid = sceKernelAllocMemBlock("vertex_usse", SCE_KERNEL_MEMBLOCK_TYPE_USER_RW_UNCACHE, size, NULL);
@ -88,7 +83,7 @@ void *vita_mem_vertex_usse_alloc(unsigned int size, SceUID *uid, unsigned int *u
void vita_mem_vertex_usse_free(SceUID uid)
{
void *mem = NULL;
void* mem = NULL;
if (sceKernelGetMemBlockBase(uid, &mem) < 0) {
return;
}
@ -96,9 +91,9 @@ void vita_mem_vertex_usse_free(SceUID uid)
sceKernelFreeMemBlock(uid);
}
void *vita_mem_fragment_usse_alloc(unsigned int size, SceUID *uid, unsigned int *usse_offset)
void* vita_mem_fragment_usse_alloc(unsigned int size, SceUID* uid, unsigned int* usse_offset)
{
void *mem = NULL;
void* mem = NULL;
size = ALIGN(size, 4096);
*uid = sceKernelAllocMemBlock("fragment_usse", SCE_KERNEL_MEMBLOCK_TYPE_USER_RW_UNCACHE, size, NULL);
@ -115,7 +110,7 @@ void *vita_mem_fragment_usse_alloc(unsigned int size, SceUID *uid, unsigned int
void vita_mem_fragment_usse_free(SceUID uid)
{
void *mem = NULL;
void* mem = NULL;
if (sceKernelGetMemBlockBase(uid, &mem) < 0) {
return;
}
@ -123,8 +118,9 @@ void vita_mem_fragment_usse_free(SceUID uid)
sceKernelFreeMemBlock(uid);
}
bool cdramPool_init() {
if(cdramPool) {
bool cdramPool_init()
{
if (cdramPool) {
return true;
}
int poolsize;
@ -153,13 +149,18 @@ bool cdramPool_init() {
if (!cdramPool) {
return false;
}
ret = sceGxmMapMemory(mem, poolsize, (SceGxmMemoryAttribFlags)(SCE_GXM_MEMORY_ATTRIB_READ | SCE_GXM_MEMORY_ATTRIB_WRITE));
ret = sceGxmMapMemory(
mem,
poolsize,
(SceGxmMemoryAttribFlags) (SCE_GXM_MEMORY_ATTRIB_READ | SCE_GXM_MEMORY_ATTRIB_WRITE)
);
if (ret < 0) {
return false;
}
return true;
}
SceClibMspace cdramPool_get() {
SceClibMspace cdramPool_get()
{
return cdramPool;
}

View File

@ -1,25 +1,19 @@
#pragma once
#include <psp2/types.h>
#include <psp2/kernel/sysmem.h>
#include <psp2/kernel/clib.h>
#include <SDL3/SDL_stdinc.h>
#include <psp2/kernel/clib.h>
#include <psp2/kernel/sysmem.h>
#include <psp2/types.h>
void *patcher_host_alloc(void *user_data, unsigned int size);
void patcher_host_free(void *user_data, void *mem);
void* patcher_host_alloc(void* user_data, unsigned int size);
void patcher_host_free(void* user_data, void* mem);
void* vita_mem_alloc(
unsigned int type,
size_t size,
size_t alignment,
int attribs,
SceUID* uid
);
void* vita_mem_alloc(unsigned int type, size_t size, size_t alignment, int attribs, SceUID* uid);
void vita_mem_free(SceUID uid);
void *vita_mem_vertex_usse_alloc(unsigned int size, SceUID *uid, unsigned int *usse_offset);
void* vita_mem_vertex_usse_alloc(unsigned int size, SceUID* uid, unsigned int* usse_offset);
void vita_mem_vertex_usse_free(SceUID uid);
void *vita_mem_fragment_usse_alloc(unsigned int size, SceUID *uid, unsigned int *usse_offset);
void* vita_mem_fragment_usse_alloc(unsigned int size, SceUID* uid, unsigned int* usse_offset);
void vita_mem_fragment_usse_free(SceUID uid);
bool cdramPool_init();

View File

@ -1,17 +1,15 @@
#include "d3drmrenderer_gxm.h"
#include "memory.h"
#include "meshutils.h"
#include "utils.h"
#include <SDL3/SDL.h>
#include <algorithm>
#include <string>
#include <psp2/gxm.h>
#include <psp2/display.h>
#include <psp2/types.h>
#include <psp2/gxm.h>
#include <psp2/kernel/modulemgr.h>
#include "utils.h"
#include "memory.h"
#include <psp2/types.h>
#include <string>
#define INCBIN_PREFIX _inc_
#include "incbin.h"
@ -25,17 +23,15 @@ bool with_razor = false;
#define VITA_GXM_COLOR_FORMAT SCE_GXM_COLOR_FORMAT_A8B8G8R8
#define VITA_GXM_PIXEL_FORMAT SCE_DISPLAY_PIXELFORMAT_A8B8G8R8
INCBIN(main_vert_gxp, "shaders/main.vert.gxp");
INCBIN(main_frag_gxp, "shaders/main.frag.gxp");
INCBIN(color_frag_gxp, "shaders/color.frag.gxp");
INCBIN(image_frag_gxp, "shaders/image.frag.gxp");
const SceGxmProgram* mainVertexProgramGxp = (const SceGxmProgram*)_inc_main_vert_gxpData;
const SceGxmProgram* mainFragmentProgramGxp = (const SceGxmProgram*)_inc_main_frag_gxpData;
const SceGxmProgram* colorFragmentProgramGxp = (const SceGxmProgram*)_inc_color_frag_gxpData;
const SceGxmProgram* imageFragmentProgramGxp = (const SceGxmProgram*)_inc_image_frag_gxpData;
const SceGxmProgram* mainVertexProgramGxp = (const SceGxmProgram*) _inc_main_vert_gxpData;
const SceGxmProgram* mainFragmentProgramGxp = (const SceGxmProgram*) _inc_main_frag_gxpData;
const SceGxmProgram* colorFragmentProgramGxp = (const SceGxmProgram*) _inc_color_frag_gxpData;
const SceGxmProgram* imageFragmentProgramGxp = (const SceGxmProgram*) _inc_image_frag_gxpData;
static const SceGxmBlendInfo blendInfoOpaque = {
.colorMask = SCE_GXM_COLOR_MASK_ALL,
@ -63,8 +59,9 @@ extern "C" int sceRazorGpuCaptureSetTriggerNextFrame(const char* path);
static GXMRendererContext gxm_renderer_context;
static void display_callback(const void *callback_data) {
const GXMDisplayData *display_data = (const GXMDisplayData *)callback_data;
static void display_callback(const void* callback_data)
{
const GXMDisplayData* display_data = (const GXMDisplayData*) callback_data;
SceDisplayFrameBuf framebuf;
SDL_memset(&framebuf, 0x00, sizeof(SceDisplayFrameBuf));
@ -78,21 +75,23 @@ static void display_callback(const void *callback_data) {
sceDisplayWaitSetFrameBuf();
}
static void load_razor() {
static void load_razor()
{
int mod_id = _sceKernelLoadModule("app0:librazorcapture_es4.suprx", 0, nullptr);
int status;
if(!SCE_ERR(sceKernelStartModule, mod_id, 0, nullptr, 0, nullptr, &status)) {
if (!SCE_ERR(sceKernelStartModule, mod_id, 0, nullptr, 0, nullptr, &status)) {
with_razor = true;
}
if(with_razor) {
if (with_razor) {
sceRazorGpuCaptureEnableSalvage("ux0:data/gpu_crash.sgx");
}
}
bool gxm_initialized = false;
bool gxm_init() {
if(gxm_initialized) {
bool gxm_init()
{
if (gxm_initialized) {
return true;
}
@ -116,13 +115,14 @@ bool gxm_init() {
return cdramPool_init();
}
static bool create_gxm_context() {
static bool create_gxm_context()
{
GXMRendererContext* data = &gxm_renderer_context;
if(data->context) {
if (data->context) {
return true;
}
if(!gxm_init()) {
if (!gxm_init()) {
return false;
}
@ -131,7 +131,7 @@ static bool create_gxm_context() {
const unsigned int patcherFragmentUsseSize = 64 * 1024;
data->cdramPool = cdramPool_get();
if(!data->cdramPool) {
if (!data->cdramPool) {
SDL_Log("failed to allocate cdramPool");
return false;
}
@ -164,7 +164,8 @@ static bool create_gxm_context() {
data->fragmentUsseRingBuffer = vita_mem_fragment_usse_alloc(
SCE_GXM_DEFAULT_FRAGMENT_USSE_RING_BUFFER_SIZE,
&data->fragmentUsseRingBufferUid,
&data->fragmentUsseRingBufferOffset);
&data->fragmentUsseRingBufferOffset
);
// create context
SceGxmContextParams contextParams;
@ -181,7 +182,7 @@ static bool create_gxm_context() {
contextParams.fragmentUsseRingBufferMemSize = SCE_GXM_DEFAULT_FRAGMENT_USSE_RING_BUFFER_SIZE;
contextParams.fragmentUsseRingBufferOffset = data->fragmentUsseRingBufferOffset;
if(SCE_ERR(sceGxmCreateContext, &contextParams, &data->context)) {
if (SCE_ERR(sceGxmCreateContext, &contextParams, &data->context)) {
return false;
}
data->contextHostMem = contextParams.hostMem;
@ -195,15 +196,14 @@ static bool create_gxm_context() {
&data->patcherBufferUid
);
data->patcherVertexUsse = vita_mem_vertex_usse_alloc(
patcherVertexUsseSize,
&data->patcherVertexUsseUid,
&data->patcherVertexUsseOffset);
data->patcherVertexUsse =
vita_mem_vertex_usse_alloc(patcherVertexUsseSize, &data->patcherVertexUsseUid, &data->patcherVertexUsseOffset);
data->patcherFragmentUsse = vita_mem_fragment_usse_alloc(
patcherFragmentUsseSize,
&data->patcherFragmentUsseUid,
&data->patcherFragmentUsseOffset);
&data->patcherFragmentUsseOffset
);
SceGxmShaderPatcherParams patcherParams;
memset(&patcherParams, 0, sizeof(SceGxmShaderPatcherParams));
@ -225,13 +225,14 @@ static bool create_gxm_context() {
patcherParams.fragmentUsseMemSize = patcherFragmentUsseSize;
patcherParams.fragmentUsseOffset = data->patcherFragmentUsseOffset;
if(SCE_ERR(sceGxmShaderPatcherCreate, &patcherParams, &data->shaderPatcher)) {
if (SCE_ERR(sceGxmShaderPatcherCreate, &patcherParams, &data->shaderPatcher)) {
return false;
}
return true;
}
static void destroy_gxm_context() {
static void destroy_gxm_context()
{
sceGxmShaderPatcherDestroy(gxm_renderer_context.shaderPatcher);
sceGxmDestroyContext(gxm_renderer_context.context);
vita_mem_fragment_usse_free(gxm_renderer_context.fragmentUsseRingBufferUid);
@ -241,8 +242,9 @@ static void destroy_gxm_context() {
SDL_free(gxm_renderer_context.contextHostMem);
}
bool get_gxm_context(SceGxmContext** context, SceGxmShaderPatcher** shaderPatcher, SceClibMspace* cdramPool) {
if(!create_gxm_context()) {
bool get_gxm_context(SceGxmContext** context, SceGxmShaderPatcher** shaderPatcher, SceClibMspace* cdramPool)
{
if (!create_gxm_context()) {
return false;
}
*context = gxm_renderer_context.context;
@ -285,14 +287,15 @@ Direct3DRMRenderer* GXMRenderer::Create(DWORD width, DWORD height)
SDL_Log("GXMRenderer::Create width=%d height=%d", width, height);
bool success = gxm_init();
if(!success) {
if (!success) {
return nullptr;
}
return new GXMRenderer(width, height);
}
GXMRenderer::GXMRenderer(DWORD width, DWORD height) {
GXMRenderer::GXMRenderer(DWORD width, DWORD height)
{
m_width = VITA_GXM_SCREEN_WIDTH;
m_height = VITA_GXM_SCREEN_HEIGHT;
m_virtualWidth = width;
@ -303,7 +306,9 @@ GXMRenderer::GXMRenderer(DWORD width, DWORD height) {
const unsigned int sampleCount = alignedWidth * alignedHeight;
const unsigned int depthStrideInSamples = alignedWidth;
if(!get_gxm_context(&this->context, &this->shaderPatcher, &this->cdramPool)) return;
if (!get_gxm_context(&this->context, &this->shaderPatcher, &this->cdramPool)) {
return;
}
// render target
SceGxmRenderTargetParams renderTargetParams;
@ -315,9 +320,11 @@ GXMRenderer::GXMRenderer(DWORD width, DWORD height) {
renderTargetParams.multisampleMode = 0;
renderTargetParams.multisampleLocations = 0;
renderTargetParams.driverMemBlock = -1; // Invalid UID
if(SCE_ERR(sceGxmCreateRenderTarget, &renderTargetParams, &this->renderTarget)) return;
if (SCE_ERR(sceGxmCreateRenderTarget, &renderTargetParams, &this->renderTarget)) {
return;
}
for(int i = 0; i < VITA_GXM_DISPLAY_BUFFER_COUNT; i++) {
for (int i = 0; i < VITA_GXM_DISPLAY_BUFFER_COUNT; i++) {
this->displayBuffers[i] = vita_mem_alloc(
SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW,
4 * VITA_GXM_SCREEN_STRIDE * VITA_GXM_SCREEN_HEIGHT,
@ -326,20 +333,25 @@ GXMRenderer::GXMRenderer(DWORD width, DWORD height) {
&this->displayBuffersUid[i]
);
if(SCE_ERR(sceGxmColorSurfaceInit,
if (SCE_ERR(
sceGxmColorSurfaceInit,
&this->displayBuffersSurface[i],
SCE_GXM_COLOR_FORMAT_A8B8G8R8,
SCE_GXM_COLOR_SURFACE_LINEAR,
SCE_GXM_COLOR_SURFACE_SCALE_NONE,
SCE_GXM_OUTPUT_REGISTER_SIZE_32BIT,
m_width, m_height,
m_width,
m_height,
m_width,
this->displayBuffers[i]
)) return;
if(SCE_ERR(sceGxmSyncObjectCreate, &this->displayBuffersSync[i])) return;
)) {
return;
}
if (SCE_ERR(sceGxmSyncObjectCreate, &this->displayBuffersSync[i])) {
return;
}
}
// depth & stencil
this->depthBufferData = vita_mem_alloc(
@ -358,27 +370,57 @@ GXMRenderer::GXMRenderer(DWORD width, DWORD height) {
&this->stencilBufferUid
);
if(SCE_ERR(sceGxmDepthStencilSurfaceInit,
if (SCE_ERR(
sceGxmDepthStencilSurfaceInit,
&this->depthSurface,
SCE_GXM_DEPTH_STENCIL_FORMAT_S8D24,
SCE_GXM_DEPTH_STENCIL_SURFACE_TILED,
depthStrideInSamples,
this->depthBufferData,
this->stencilBufferData
)) return;
)) {
return;
}
// register shader programs
if(SCE_ERR(sceGxmShaderPatcherRegisterProgram, this->shaderPatcher, colorFragmentProgramGxp, &this->colorFragmentProgramId)) return;
if(SCE_ERR(sceGxmShaderPatcherRegisterProgram, this->shaderPatcher, mainVertexProgramGxp, &this->mainVertexProgramId)) return;
if(SCE_ERR(sceGxmShaderPatcherRegisterProgram, this->shaderPatcher, mainFragmentProgramGxp, &this->mainFragmentProgramId)) return;
if(SCE_ERR(sceGxmShaderPatcherRegisterProgram, this->shaderPatcher, imageFragmentProgramGxp, &this->imageFragmentProgramId)) return;
if (SCE_ERR(
sceGxmShaderPatcherRegisterProgram,
this->shaderPatcher,
colorFragmentProgramGxp,
&this->colorFragmentProgramId
)) {
return;
}
if (SCE_ERR(
sceGxmShaderPatcherRegisterProgram,
this->shaderPatcher,
mainVertexProgramGxp,
&this->mainVertexProgramId
)) {
return;
}
if (SCE_ERR(
sceGxmShaderPatcherRegisterProgram,
this->shaderPatcher,
mainFragmentProgramGxp,
&this->mainFragmentProgramId
)) {
return;
}
if (SCE_ERR(
sceGxmShaderPatcherRegisterProgram,
this->shaderPatcher,
imageFragmentProgramGxp,
&this->imageFragmentProgramId
)) {
return;
}
// main shader
{
GET_SHADER_PARAM(positionAttribute, mainVertexProgramGxp, "aPosition",);
GET_SHADER_PARAM(normalAttribute, mainVertexProgramGxp, "aNormal",);
GET_SHADER_PARAM(texCoordAttribute, mainVertexProgramGxp, "aTexCoord",);
GET_SHADER_PARAM(positionAttribute, mainVertexProgramGxp, "aPosition", );
GET_SHADER_PARAM(normalAttribute, mainVertexProgramGxp, "aNormal", );
GET_SHADER_PARAM(texCoordAttribute, mainVertexProgramGxp, "aTexCoord", );
SceGxmVertexAttribute vertexAttributes[3];
SceGxmVertexStream vertexStreams[1];
@ -406,17 +448,23 @@ GXMRenderer::GXMRenderer(DWORD width, DWORD height) {
vertexStreams[0].stride = sizeof(Vertex);
vertexStreams[0].indexSource = SCE_GXM_INDEX_SOURCE_INDEX_16BIT;
if(SCE_ERR(sceGxmShaderPatcherCreateVertexProgram,
if (SCE_ERR(
sceGxmShaderPatcherCreateVertexProgram,
this->shaderPatcher,
this->mainVertexProgramId,
vertexAttributes, 3,
vertexStreams, 1,
vertexAttributes,
3,
vertexStreams,
1,
&this->mainVertexProgram
)) return;
)) {
return;
}
}
// main opaque
if(SCE_ERR(sceGxmShaderPatcherCreateFragmentProgram,
if (SCE_ERR(
sceGxmShaderPatcherCreateFragmentProgram,
this->shaderPatcher,
this->mainFragmentProgramId,
SCE_GXM_OUTPUT_REGISTER_FORMAT_UCHAR4,
@ -424,10 +472,13 @@ GXMRenderer::GXMRenderer(DWORD width, DWORD height) {
&blendInfoOpaque,
mainVertexProgramGxp,
&this->opaqueFragmentProgram
)) return;
)) {
return;
}
// main transparent
if(SCE_ERR(sceGxmShaderPatcherCreateFragmentProgram,
if (SCE_ERR(
sceGxmShaderPatcherCreateFragmentProgram,
this->shaderPatcher,
this->mainFragmentProgramId,
SCE_GXM_OUTPUT_REGISTER_FORMAT_UCHAR4,
@ -435,10 +486,13 @@ GXMRenderer::GXMRenderer(DWORD width, DWORD height) {
&blendInfoTransparent,
mainVertexProgramGxp,
&this->transparentFragmentProgram
)) return;
)) {
return;
}
// image
if(SCE_ERR(sceGxmShaderPatcherCreateFragmentProgram,
if (SCE_ERR(
sceGxmShaderPatcherCreateFragmentProgram,
this->shaderPatcher,
this->imageFragmentProgramId,
SCE_GXM_OUTPUT_REGISTER_FORMAT_UCHAR4,
@ -446,10 +500,13 @@ GXMRenderer::GXMRenderer(DWORD width, DWORD height) {
&blendInfoTransparent,
mainVertexProgramGxp,
&this->imageFragmentProgram
)) return;
)) {
return;
}
// color
if(SCE_ERR(sceGxmShaderPatcherCreateFragmentProgram,
if (SCE_ERR(
sceGxmShaderPatcherCreateFragmentProgram,
this->shaderPatcher,
this->colorFragmentProgramId,
SCE_GXM_OUTPUT_REGISTER_FORMAT_UCHAR4,
@ -457,7 +514,9 @@ GXMRenderer::GXMRenderer(DWORD width, DWORD height) {
NULL,
mainVertexProgramGxp,
&this->colorFragmentProgram
)) return;
)) {
return;
}
// vertex uniforms
this->uModelViewMatrix = sceGxmProgramFindParameterByName(mainVertexProgramGxp, "uModelViewMatrix");
@ -475,28 +534,29 @@ GXMRenderer::GXMRenderer(DWORD width, DWORD height) {
this->colorShader_uColor = sceGxmProgramFindParameterByName(colorFragmentProgramGxp, "uColor"); // vec4
this->lights = static_cast<decltype(this->lights)>(sceClibMspaceMalloc(this->cdramPool, sizeof(*this->lights)));
for(int i = 0; i < VITA_GXM_UNIFORM_BUFFER_COUNT; i++) {
this->quadVertices[i] = (Vertex*)sceClibMspaceMalloc(this->cdramPool, sizeof(Vertex)*4*50);
for (int i = 0; i < VITA_GXM_UNIFORM_BUFFER_COUNT; i++) {
this->quadVertices[i] = (Vertex*) sceClibMspaceMalloc(this->cdramPool, sizeof(Vertex) * 4 * 50);
}
this->quadIndices = (uint16_t*)sceClibMspaceMalloc(this->cdramPool, sizeof(uint16_t)*4);
this->quadIndices = (uint16_t*) sceClibMspaceMalloc(this->cdramPool, sizeof(uint16_t) * 4);
this->quadIndices[0] = 0;
this->quadIndices[1] = 1;
this->quadIndices[2] = 2;
this->quadIndices[3] = 3;
volatile uint32_t *const notificationMem = sceGxmGetNotificationRegion();
volatile uint32_t* const notificationMem = sceGxmGetNotificationRegion();
for (uint32_t i = 0; i < VITA_GXM_UNIFORM_BUFFER_COUNT; i++) {
this->fragmentNotifications[i].address = notificationMem + (i*2);
this->fragmentNotifications[i].address = notificationMem + (i * 2);
this->fragmentNotifications[i].value = 0;
this->vertexNotifications[i].address = notificationMem + (i*2)+1;
this->vertexNotifications[i].address = notificationMem + (i * 2) + 1;
this->vertexNotifications[i].value = 0;
}
m_initialized = true;
}
GXMRenderer::~GXMRenderer() {
if(!m_initialized) {
GXMRenderer::~GXMRenderer()
{
if (!m_initialized) {
return;
}
@ -547,10 +607,18 @@ void GXMRenderer::AddTextureDestroyCallback(Uint32 id, IDirect3DRMTexture* textu
);
}
static void convertTextureMetadata(SDL_Surface* surface, bool* supportedFormat, SceGxmTextureFormat* textureFormat, size_t* textureSize, size_t* textureAlignment, size_t* textureStride) {
static void convertTextureMetadata(
SDL_Surface* surface,
bool* supportedFormat,
SceGxmTextureFormat* textureFormat,
size_t* textureSize,
size_t* textureAlignment,
size_t* textureStride
)
{
*supportedFormat = true;
*textureAlignment = SCE_GXM_TEXTURE_ALIGNMENT;
switch(surface->format) {
switch (surface->format) {
case SDL_PIXELFORMAT_ABGR8888: {
*textureFormat = SCE_GXM_TEXTURE_FORMAT_U8U8U8U8_ABGR;
*textureSize = surface->h * surface->pitch;
@ -574,7 +642,8 @@ static void convertTextureMetadata(SDL_Surface* surface, bool* supportedFormat,
}
}
void copySurfaceTo(SDL_Surface* src, void* dstData, size_t textureStride) {
void copySurfaceTo(SDL_Surface* src, void* dstData, size_t textureStride)
{
SDL_Surface* dst = SDL_CreateSurfaceFrom(src->w, src->h, SDL_PIXELFORMAT_ABGR8888, dstData, textureStride);
SDL_BlitSurface(src, nullptr, dst, nullptr);
SDL_DestroySurface(dst);
@ -585,7 +654,6 @@ Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture)
auto texture = static_cast<Direct3DRMTextureImpl*>(iTexture);
auto surface = static_cast<DirectDrawSurfaceImpl*>(texture->m_surface);
bool supportedFormat;
size_t textureSize;
size_t textureAlignment;
@ -594,9 +662,16 @@ Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture)
int textureWidth = surface->m_surface->w;
int textureHeight = surface->m_surface->h;
convertTextureMetadata(surface->m_surface, &supportedFormat, &textureFormat, &textureSize, &textureAlignment, &textureStride);
convertTextureMetadata(
surface->m_surface,
&supportedFormat,
&textureFormat,
&textureSize,
&textureAlignment,
&textureStride
);
if(!supportedFormat) {
if (!supportedFormat) {
textureAlignment = SCE_GXM_TEXTURE_ALIGNMENT;
textureStride = textureWidth * 4;
textureSize = textureHeight * textureStride;
@ -608,9 +683,10 @@ Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture)
if (tex.texture == texture) {
if (tex.version != texture->m_version) {
void* textureData = sceGxmTextureGetData(&tex.gxmTexture);
if(!supportedFormat) {
if (!supportedFormat) {
copySurfaceTo(surface->m_surface, textureData, textureStride);
} else {
}
else {
memcpy(textureData, surface->m_surface->pixels, textureSize);
}
tex.version = texture->m_version;
@ -619,19 +695,26 @@ Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture)
}
}
SDL_Log("Create Texture %s w=%d h=%d s=%d",
SDL_GetPixelFormatName(surface->m_surface->format), textureWidth, textureHeight, textureStride);
SDL_Log(
"Create Texture %s w=%d h=%d s=%d",
SDL_GetPixelFormatName(surface->m_surface->format),
textureWidth,
textureHeight,
textureStride
);
// allocate gpu memory
void* textureData = sceClibMspaceMemalign(this->cdramPool, textureAlignment, textureSize);
uint8_t* paletteData = nullptr;
if(!supportedFormat) {
SDL_Log("unsupported SDL texture format %s, falling back on SDL_PIXELFORMAT_ABGR8888", SDL_GetPixelFormatName(surface->m_surface->format));
if (!supportedFormat) {
SDL_Log(
"unsupported SDL texture format %s, falling back on SDL_PIXELFORMAT_ABGR8888",
SDL_GetPixelFormatName(surface->m_surface->format)
);
copySurfaceTo(surface->m_surface, textureData, textureStride);
}
else if(surface->m_surface->format == SDL_PIXELFORMAT_INDEX8)
{
else if (surface->m_surface->format == SDL_PIXELFORMAT_INDEX8) {
LPDIRECTDRAWPALETTE _palette;
surface->GetPalette(&_palette);
auto palette = static_cast<DirectDrawPaletteImpl*>(_palette);
@ -641,20 +724,27 @@ Uint32 GXMRenderer::GetTextureId(IDirect3DRMTexture* iTexture)
SDL_Log("copying indexed texture data from=%p to=%p", surface->m_surface->pixels, textureData);
memcpy(textureData, surface->m_surface->pixels, pixelsSize);
paletteData = (uint8_t*)textureData + pixelsSize + alignBytes;
memcpy(paletteData, palette->m_palette->colors, palette->m_palette->ncolors*sizeof(SDL_Color));
paletteData = (uint8_t*) textureData + pixelsSize + alignBytes;
memcpy(paletteData, palette->m_palette->colors, palette->m_palette->ncolors * sizeof(SDL_Color));
}
else
{
else {
SDL_Log("copying texture data from=%p to=%p", surface->m_surface->pixels, textureData);
memcpy(textureData, surface->m_surface->pixels, textureSize);
}
SceGxmTexture gxmTexture;
SCE_ERR(sceGxmTextureInitLinearStrided, &gxmTexture, textureData, textureFormat, textureWidth, textureHeight, textureStride);
//sceGxmTextureSetMinFilter(&gxmTexture, SCE_GXM_TEXTURE_FILTER_LINEAR);
SCE_ERR(
sceGxmTextureInitLinearStrided,
&gxmTexture,
textureData,
textureFormat,
textureWidth,
textureHeight,
textureStride
);
// sceGxmTextureSetMinFilter(&gxmTexture, SCE_GXM_TEXTURE_FILTER_LINEAR);
sceGxmTextureSetMagFilter(&gxmTexture, SCE_GXM_TEXTURE_FILTER_LINEAR);
if(paletteData) {
if (paletteData) {
sceGxmTextureSetPalette(&gxmTexture, paletteData);
}
@ -703,33 +793,36 @@ GXMMeshCacheEntry GXMRenderer::GXMUploadMesh(const MeshGroup& meshGroup)
});
}
size_t vertexBufferSize = sizeof(Vertex)*vertices.size();
size_t indexBufferSize = sizeof(uint16_t)*indices.size();
void* meshData = sceClibMspaceMemalign(this->cdramPool, 4, vertexBufferSize+indexBufferSize);
size_t vertexBufferSize = sizeof(Vertex) * vertices.size();
size_t indexBufferSize = sizeof(uint16_t) * indices.size();
void* meshData = sceClibMspaceMemalign(this->cdramPool, 4, vertexBufferSize + indexBufferSize);
Vertex* vertexBuffer = (Vertex*)meshData;
uint16_t* indexBuffer = (uint16_t*)((uint8_t*)meshData + vertexBufferSize);
Vertex* vertexBuffer = (Vertex*) meshData;
uint16_t* indexBuffer = (uint16_t*) ((uint8_t*) meshData + vertexBufferSize);
for(int i = 0; i < vertices.size(); i++) {
for (int i = 0; i < vertices.size(); i++) {
D3DRMVERTEX vertex = vertices.data()[i];
vertexBuffer[i] = Vertex{
.position = {
.position =
{
vertex.position.x,
vertex.position.y,
vertex.position.z,
},
.normal = {
.normal =
{
vertex.normal.x,
vertex.normal.y,
vertex.normal.z,
},
.texCoord = {
.texCoord =
{
vertex.tu,
vertex.tv,
}
};
}
memcpy(indexBuffer, indices.data(), indices.size()*sizeof(uint16_t));
memcpy(indexBuffer, indices.data(), indices.size() * sizeof(uint16_t));
cache.meshData = meshData;
cache.vertexBuffer = vertexBuffer;
@ -801,15 +894,18 @@ void GXMRenderer::GetDesc(D3DDEVICEDESC* halDesc, D3DDEVICEDESC* helDesc)
memset(helDesc, 0, sizeof(D3DDEVICEDESC));
}
const char* GXMRenderer::GetName() {
const char* GXMRenderer::GetName()
{
return "GXM";
}
bool razor_triggered = false;
void GXMRenderer::StartScene() {
if(sceneStarted) return;
void GXMRenderer::StartScene()
{
if (sceneStarted) {
return;
}
sceGxmBeginScene(
this->context,
0,
@ -827,15 +923,16 @@ void GXMRenderer::StartScene() {
this->activeUniformBuffer = (this->activeUniformBuffer + 1) % VITA_GXM_UNIFORM_BUFFER_COUNT;
sceGxmNotificationWait(&this->fragmentNotifications[this->activeUniformBuffer]);
//sceClibPrintf("this->activeUniformBuffer: %d notification: %d\n", this->activeUniformBuffer, this->fragmentNotifications[this->activeUniformBuffer].value);
// sceClibPrintf("this->activeUniformBuffer: %d notification: %d\n", this->activeUniformBuffer,
// this->fragmentNotifications[this->activeUniformBuffer].value);
}
int frames = 0;
HRESULT GXMRenderer::BeginFrame()
{
frames++;
if(with_razor) {
if(!razor_triggered && frames == 10) {
if (with_razor) {
if (!razor_triggered && frames == 10) {
SDL_Log("trigger razor");
sceRazorGpuCaptureSetTriggerNextFrame("ux0:/data/capture.sgx");
razor_triggered = true;
@ -869,14 +966,18 @@ HRESULT GXMRenderer::BeginFrame()
return DD_OK;
}
void GXMRenderer::EnableTransparency() {
void GXMRenderer::EnableTransparency()
{
this->transparencyEnabled = true;
}
static void transpose4x4(const float src[4][4], float dst[4][4]) {
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
static void transpose4x4(const float src[4][4], float dst[4][4])
{
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
dst[j][i] = src[i][j];
}
}
}
static const D3DRMMATRIX4D identity4x4 = {
@ -892,7 +993,6 @@ static const Matrix3x3 identity3x3 = {
{0.0, 0.0, 1.0},
};
void GXMRenderer::SubmitDraw(
DWORD meshId,
const D3DRMMATRIX4D& modelViewMatrix,
@ -900,7 +1000,8 @@ void GXMRenderer::SubmitDraw(
const D3DRMMATRIX4D& viewMatrix,
const Matrix3x3& normalMatrix,
const Appearance& appearance
) {
)
{
auto& mesh = m_meshes[meshId];
char marker[256];
@ -908,9 +1009,10 @@ void GXMRenderer::SubmitDraw(
sceGxmPushUserMarker(this->context, marker);
sceGxmSetVertexProgram(this->context, this->mainVertexProgram);
if(this->transparencyEnabled) {
if (this->transparencyEnabled) {
sceGxmSetFragmentProgram(this->context, this->transparentFragmentProgram);
} else {
}
else {
sceGxmSetFragmentProgram(this->context, this->opaqueFragmentProgram);
}
@ -934,35 +1036,32 @@ void GXMRenderer::SubmitDraw(
int useTexture = appearance.textureId != NO_TEXTURE_ID ? 1 : 0;
SET_UNIFORM(fragUniforms, this->uUseTexture, useTexture);
if(useTexture) {
if (useTexture) {
auto& texture = m_textures[appearance.textureId];
sceGxmSetFragmentTexture(this->context, 0, &texture.gxmTexture);
}
sceGxmSetVertexStream(this->context, 0, mesh.vertexBuffer);
sceGxmDraw(
this->context,
SCE_GXM_PRIMITIVE_TRIANGLES,
SCE_GXM_INDEX_FORMAT_U16,
mesh.indexBuffer,
mesh.indexCount
);
sceGxmDraw(this->context, SCE_GXM_PRIMITIVE_TRIANGLES, SCE_GXM_INDEX_FORMAT_U16, mesh.indexBuffer, mesh.indexCount);
sceGxmPopUserMarker(this->context);
}
HRESULT GXMRenderer::FinalizeFrame() {
HRESULT GXMRenderer::FinalizeFrame()
{
return DD_OK;
}
void GXMRenderer::Resize(int width, int height, const ViewportTransform& viewportTransform) {
void GXMRenderer::Resize(int width, int height, const ViewportTransform& viewportTransform)
{
m_width = width;
m_height = height;
m_viewportTransform = viewportTransform;
SDL_Log("GXMRenderer::Resize TODO");
}
void GXMRenderer::Clear(float r, float g, float b) {
void GXMRenderer::Clear(float r, float g, float b)
{
this->StartScene();
char marker[256];
@ -986,7 +1085,7 @@ void GXMRenderer::Clear(float r, float g, float b) {
SET_UNIFORM(vertUniforms, this->uNormalMatrix, normal); // float3x3
SET_UNIFORM(vertUniforms, this->uProjectionMatrix, projection); // float4x4
float color[] = {r,g,b,1};
float color[] = {r, g, b, 1};
SET_UNIFORM(fragUniforms, this->colorShader_uColor, color);
float x1 = 0;
@ -995,25 +1094,21 @@ void GXMRenderer::Clear(float r, float g, float b) {
float y2 = y1 + 1.0;
Vertex* quadVertices = this->QuadVerticesBuffer();
quadVertices[0] = Vertex{ .position = {x1, y1, -1.0}, .normal = {0,0,0}, .texCoord = {0,0}};
quadVertices[1] = Vertex{ .position = {x2, y1, -1.0}, .normal = {0,0,0}, .texCoord = {0,0}};
quadVertices[2] = Vertex{ .position = {x1, y2, -1.0}, .normal = {0,0,0}, .texCoord = {0,0}};
quadVertices[3] = Vertex{ .position = {x2, y2, -1.0}, .normal = {0,0,0}, .texCoord = {0,0}};
quadVertices[0] = Vertex{.position = {x1, y1, -1.0}, .normal = {0, 0, 0}, .texCoord = {0, 0}};
quadVertices[1] = Vertex{.position = {x2, y1, -1.0}, .normal = {0, 0, 0}, .texCoord = {0, 0}};
quadVertices[2] = Vertex{.position = {x1, y2, -1.0}, .normal = {0, 0, 0}, .texCoord = {0, 0}};
quadVertices[3] = Vertex{.position = {x2, y2, -1.0}, .normal = {0, 0, 0}, .texCoord = {0, 0}};
sceGxmSetVertexStream(this->context, 0, quadVertices);
sceGxmDraw(
this->context,
SCE_GXM_PRIMITIVE_TRIANGLE_STRIP,
SCE_GXM_INDEX_FORMAT_U16,
this->quadIndices, 4
);
sceGxmDraw(this->context, SCE_GXM_PRIMITIVE_TRIANGLE_STRIP, SCE_GXM_INDEX_FORMAT_U16, this->quadIndices, 4);
sceGxmPopUserMarker(this->context);
}
void GXMRenderer::Flip() {
if(!this->sceneStarted) {
this->Clear(0,0,0);
void GXMRenderer::Flip()
{
if (!this->sceneStarted) {
this->Clear(0, 0, 0);
}
// end scene
@ -1022,8 +1117,9 @@ void GXMRenderer::Flip() {
sceGxmEndScene(
this->context,
nullptr, //&this->vertexNotifications[this->activeUniformBuffer],
//nullptr
&this->fragmentNotifications[this->activeUniformBuffer] // wait for fragment processing to finish for this buffer, otherwise lighting corrupts
// nullptr
&this->fragmentNotifications[this->activeUniformBuffer] // wait for fragment processing to finish for this
// buffer, otherwise lighting corrupts
);
sceGxmPadHeartbeat(
&this->displayBuffersSurface[this->backBufferIndex],
@ -1045,7 +1141,8 @@ void GXMRenderer::Flip() {
this->backBufferIndex = (this->backBufferIndex + 1) % VITA_GXM_DISPLAY_BUFFER_COUNT;
}
void GXMRenderer::Draw2DImage(Uint32 textureId, const SDL_Rect& srcRect, const SDL_Rect& dstRect) {
void GXMRenderer::Draw2DImage(Uint32 textureId, const SDL_Rect& srcRect, const SDL_Rect& dstRect)
{
this->StartScene();
char marker[256];
@ -1065,8 +1162,8 @@ void GXMRenderer::Draw2DImage(Uint32 textureId, const SDL_Rect& srcRect, const S
float top = -this->m_viewportTransform.offsetY / this->m_viewportTransform.scale;
float bottom = (this->m_height - this->m_viewportTransform.offsetY) / this->m_viewportTransform.scale;
#define virtualToNDCX(x) (((x - left) / (right - left)));
#define virtualToNDCY(y) (((y - top) / (bottom - top)));
#define virtualToNDCX(x) (((x - left) / (right - left)));
#define virtualToNDCY(y) (((y - top) / (bottom - top)));
float x1_virtual = static_cast<float>(dstRect.x);
float y1_virtual = static_cast<float>(dstRect.y);
@ -1105,25 +1202,21 @@ void GXMRenderer::Draw2DImage(Uint32 textureId, const SDL_Rect& srcRect, const S
float v2 = static_cast<float>(srcRect.y + srcRect.h) / texH;
Vertex* quadVertices = this->QuadVerticesBuffer();
quadVertices[0] = Vertex{ .position = {x1, y1, 0}, .normal = {0,0,0}, .texCoord = {u1, v1}};
quadVertices[1] = Vertex{ .position = {x2, y1, 0}, .normal = {0,0,0}, .texCoord = {u2, v1}};
quadVertices[2] = Vertex{ .position = {x1, y2, 0}, .normal = {0,0,0}, .texCoord = {u1, v2}};
quadVertices[3] = Vertex{ .position = {x2, y2, 0}, .normal = {0,0,0}, .texCoord = {u2, v2}};
quadVertices[0] = Vertex{.position = {x1, y1, 0}, .normal = {0, 0, 0}, .texCoord = {u1, v1}};
quadVertices[1] = Vertex{.position = {x2, y1, 0}, .normal = {0, 0, 0}, .texCoord = {u2, v1}};
quadVertices[2] = Vertex{.position = {x1, y2, 0}, .normal = {0, 0, 0}, .texCoord = {u1, v2}};
quadVertices[3] = Vertex{.position = {x2, y2, 0}, .normal = {0, 0, 0}, .texCoord = {u2, v2}};
sceGxmSetVertexStream(this->context, 0, quadVertices);
sceGxmSetFrontDepthWriteEnable(this->context, SCE_GXM_DEPTH_WRITE_DISABLED);
sceGxmDraw(
this->context,
SCE_GXM_PRIMITIVE_TRIANGLE_STRIP,
SCE_GXM_INDEX_FORMAT_U16,
this->quadIndices, 4
);
sceGxmDraw(this->context, SCE_GXM_PRIMITIVE_TRIANGLE_STRIP, SCE_GXM_INDEX_FORMAT_U16, this->quadIndices, 4);
sceGxmSetFrontDepthWriteEnable(this->context, SCE_GXM_DEPTH_WRITE_ENABLED);
sceGxmPopUserMarker(this->context);
}
void GXMRenderer::Download(SDL_Surface* target) {
void GXMRenderer::Download(SDL_Surface* target)
{
SDL_Rect srcRect = {
static_cast<int>(m_viewportTransform.offsetX),
static_cast<int>(m_viewportTransform.offsetY),
@ -1131,9 +1224,11 @@ void GXMRenderer::Download(SDL_Surface* target) {
static_cast<int>(target->h * m_viewportTransform.scale),
};
SDL_Surface* src = SDL_CreateSurfaceFrom(
this->m_width, this->m_height,
this->m_width,
this->m_height,
SDL_PIXELFORMAT_RGBA32,
this->displayBuffers[this->frontBufferIndex], VITA_GXM_SCREEN_STRIDE
this->displayBuffers[this->frontBufferIndex],
VITA_GXM_SCREEN_STRIDE
);
SDL_BlitSurfaceScaled(src, &srcRect, target, nullptr, SDL_SCALEMODE_NEAREST);
SDL_DestroySurface(src);

View File

@ -4,37 +4,38 @@
#include <psp2/gxm.h>
#include <psp2/kernel/clib.h>
#define SCE_ERR(func, ...) ({ \
#define SCE_ERR(func, ...) \
({ \
sceClibPrintf(#func "\n"); \
int __sce_err_ret_val = func(__VA_ARGS__); \
if (__sce_err_ret_val < 0) { \
sceClibPrintf(#func " error: 0x%x\n", __sce_err_ret_val); \
} \
__sce_err_ret_val < 0; \
})
})
#define ALIGN(x, a) (((x) + ((a)-1)) & ~((a)-1))
#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
#define ALIGNMENT(n, a) (((a) - ((n) % (a))) % (a))
#define SET_UNIFORM(buffer, param, value) \
do { \
size_t __offset = sceGxmProgramParameterGetResourceIndex(param); \
void* __dst = (uint8_t*)(buffer) + (__offset * sizeof(uint32_t)); \
void* __dst = (uint8_t*) (buffer) + (__offset * sizeof(uint32_t)); \
memcpy(__dst, reinterpret_cast<const void*>(&(value)), sizeof(value)); \
} while (0)
#define GET_SHADER_PARAM(var, gxp, name, ret) \
const SceGxmProgramParameter* var = sceGxmProgramFindParameterByName(gxp, name); \
if(!var) { \
if (!var) { \
SDL_Log("Failed to find param %s", name); \
return ret; \
}
static void printMatrix4x4(const float mat[4][4]) {
static void printMatrix4x4(const float mat[4][4])
{
sceClibPrintf("mat4{\n");
for(int i = 0; i < 4; i++) {
for (int i = 0; i < 4; i++) {
sceClibPrintf("%f %f %f %f\n", mat[i][0], mat[i][1], mat[i][2], mat[i][3]);
}
sceClibPrintf("}\n");