From 2b1146d46e4993dd936f21ef713be88c7bcfc718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Jos=C3=A9=20Garc=C3=ADa=20Garc=C3=ADa?= Date: Sun, 5 Jul 2020 11:56:47 +0200 Subject: [PATCH] Squashed 'deps/vitaShaRK/' content from commit f54dc7bdd5 git-subtree-dir: deps/vitaShaRK git-subtree-split: f54dc7bdd5cb14fb9de9dc21962ccd8b6938e56d --- .gitattributes | 2 + .github/FUNDING.yml | 1 + .gitignore | 59 +++++++++++++ LICENSE | 165 ++++++++++++++++++++++++++++++++++ Makefile | 37 ++++++++ README.md | 18 ++++ SceShaccCg.yml | 43 +++++++++ samples/sample1/Makefile | 37 ++++++++ samples/sample1/main.c | 59 +++++++++++++ samples/sample2/Makefile | 37 ++++++++ samples/sample2/main.c | 85 ++++++++++++++++++ shacccg.h | 185 +++++++++++++++++++++++++++++++++++++++ source/vitashark.c | 124 ++++++++++++++++++++++++++ source/vitashark.h | 75 ++++++++++++++++ 14 files changed, 927 insertions(+) create mode 100644 .gitattributes create mode 100644 .github/FUNDING.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 SceShaccCg.yml create mode 100644 samples/sample1/Makefile create mode 100644 samples/sample1/main.c create mode 100644 samples/sample2/Makefile create mode 100644 samples/sample2/main.c create mode 100644 shacccg.h create mode 100644 source/vitashark.c create mode 100644 source/vitashark.h diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..dfe0770424 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000000..dbd470e946 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +patreon: Rinnegatamante diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..4a072bdd8b --- /dev/null +++ b/.gitignore @@ -0,0 +1,59 @@ +*.vpk +*.elf +*.velf +*.bin +*.sfo +*.S +*.wo + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# ========================= +# Operating System Files +# ========================= + +# OSX +# ========================= + +.DS_Store +.AppleDouble +.LSOverride + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Vita build stuffs +*.a +*.o diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000..8805d0dfad --- /dev/null +++ b/LICENSE @@ -0,0 +1,165 @@ +GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..58ce7401e2 --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +TARGET := libvitashark +SOURCES := source +SHADERS := shaders + +CFILES := $(foreach dir,$(SOURCES), $(wildcard $(dir)/*.c)) +ASMFILES := $(foreach dir,$(SOURCES), $(wildcard $(dir)/*.S)) +CGFILES := $(foreach dir,$(SHADERS), $(wildcard $(dir)/*.cg)) +HEADERS := $(CGFILES:.cg=.h) +OBJS := $(CFILES:.c=.o) $(ASMFILES:.S=.o) + +PREFIX = arm-vita-eabi +CC = $(PREFIX)-gcc +AR = $(PREFIX)-gcc-ar +CFLAGS = -g -Wl,-q -O2 -ffast-math -mtune=cortex-a9 -mfpu=neon -ftree-vectorize +ASFLAGS = $(CFLAGS) + +all: $(TARGET).a + +$(TARGET).a: $(OBJS) + $(AR) -rc $@ $^ + +clean: + @rm -rf $(TARGET).a $(TARGET).elf $(OBJS) + @make -C samples/sample1 clean + @make -C samples/sample2 clean + +install: $(TARGET).a + @mkdir -p $(VITASDK)/$(PREFIX)/lib/ + cp $(TARGET).a $(VITASDK)/$(PREFIX)/lib/ + @mkdir -p $(VITASDK)/$(PREFIX)/include/ + cp source/vitashark.h $(VITASDK)/$(PREFIX)/include/ + +samples: $(TARGET).a + @make -C samples/sample1 + cp "samples/sample1/vitaShaRK-Sample001.vpk" . + @make -C samples/sample2 + cp "samples/sample1/vitaShaRK-Sample002.vpk" . diff --git a/README.md b/README.md new file mode 100644 index 0000000000..53d4cd9d4b --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# vitaShaRK +**vita** **Sha**ders **R**untime **K**ompiler is a runtime shader compiler library for PSVITA/PSTV using the SceShaccCg module contained inside the PSM runtime. + +# Build Instructions +In order to build vitaShaRK, you'll first need to build SceShaccCg stubs. This is a full list of commands you can use to install this library and the required stubs: +``` +vita-libs-gen SceShaccCg.yml build +cd build +make install +cd .. +cp shacccg.h $VITASDK/arm-vita-eabi/psp2/shacccg.h +make install +``` + + +# Credits + +**frangarcj** for the original vita2d shader compiler source used as base to build up this library. diff --git a/SceShaccCg.yml b/SceShaccCg.yml new file mode 100644 index 0000000000..d20eae44a5 --- /dev/null +++ b/SceShaccCg.yml @@ -0,0 +1,43 @@ +version: 2 +firmware: 3.60 +modules: + SceShaccCg: + nid: 0xB3B90A35 + libraries: + SceShaccCg: + kernel: false + nid: 0xA05BBEBB + functions: + SceShaccCg_0205DE96: 0x0205DE96 + SceShaccCg_07DDFC78: 0x07DDFC78 + SceShaccCg_0E1285A6: 0x0E1285A6 + SceShaccCg_152971B1: 0x152971B1 + SceShaccCg_17223BEB: 0x17223BEB + SceShaccCg_2654E73A: 0x2654E73A + SceShaccCg_268FAEE9: 0x268FAEE9 + sceShaccCgInitializeCompileOptions: 0x3B58AFA0 + SceShaccCg_4595A388: 0x4595A388 + SceShaccCg_46FA0303: 0x46FA0303 + SceShaccCg_56BFA825: 0x56BFA825 + SceShaccCg_648739F3: 0x648739F3 + sceShaccCgCompileProgram: 0x66814F35 + SceShaccCg_6BB58825: 0x6BB58825 + sceShaccCgSetDefaultAllocator: 0x6F01D573 + SceShaccCg_6FB40CA9: 0x6FB40CA9 + SceShaccCg_7B2CF324: 0x7B2CF324 + SceShaccCg_7BC25091: 0x7BC25091 + SceShaccCg_7F430CCD: 0x7F430CCD + SceShaccCg_95F57A23: 0x95F57A23 + SceShaccCg_A067C481: 0xA067C481 + SceShaccCg_A13A8A1E: 0xA13A8A1E + SceShaccCg_A56B1A5B: 0xA56B1A5B + SceShaccCg_A7930FF6: 0xA7930FF6 + sceShaccCgInitializeCallbackList: 0xA8C2C1C8 + sceShaccCgDestroyCompileOutput: 0xAA82EF0C + SceShaccCg_B4AC9943: 0xB4AC9943 + SceShaccCg_BB703EE1: 0xBB703EE1 + SceShaccCg_D4378DB1: 0xD4378DB1 + SceShaccCg_DAD4AAE4: 0xDAD4AAE4 + SceShaccCg_DF3DDCFD: 0xDF3DDCFD + SceShaccCg_EF8D59D6: 0xEF8D59D6 + SceShaccCg_F4BAB902: 0xF4BAB902 diff --git a/samples/sample1/Makefile b/samples/sample1/Makefile new file mode 100644 index 0000000000..0202cf37cc --- /dev/null +++ b/samples/sample1/Makefile @@ -0,0 +1,37 @@ +SAMPLE_NUM := 001 +TARGET := vitaShaRK-Sample$(SAMPLE_NUM) +SOURCES := . + +INCLUDES := include + +LIBS = -lvitashark -lSceLibKernel_stub -lSceShaccCg_stub + +CFILES := $(foreach dir,$(SOURCES), $(wildcard $(dir)/*.c)) +CPPFILES := $(foreach dir,$(SOURCES), $(wildcard $(dir)/*.cpp)) +BINFILES := $(foreach dir,$(DATA), $(wildcard $(dir)/*.bin)) +OBJS := $(addsuffix .o,$(BINFILES)) $(CFILES:.c=.o) $(CPPFILES:.cpp=.o) + +PREFIX = arm-vita-eabi +CC = $(PREFIX)-gcc +CXX = $(PREFIX)-g++ +CFLAGS = -g -Wl,-q -O2 -ftree-vectorize +CXXFLAGS = $(CFLAGS) -fno-exceptions -std=gnu++11 -fpermissive +ASFLAGS = $(CFLAGS) + +all: $(TARGET).vpk + +$(TARGET).vpk: eboot.bin + vita-mksfoex -s TITLE_ID=VSHARK$(SAMPLE_NUM) "$(TARGET)" param.sfo + vita-pack-vpk -s param.sfo -b eboot.bin $@ + +eboot.bin: $(TARGET).velf + vita-make-fself -s $< eboot.bin + +%.velf: %.elf + vita-elf-create $< $@ + +$(TARGET).elf: $(OBJS) + $(CC) $(CFLAGS) $^ $(LIBS) -o $@ + +clean: + @rm -rf *.velf *.elf *.vpk $(OBJS) param.sfo eboot.bin diff --git a/samples/sample1/main.c b/samples/sample1/main.c new file mode 100644 index 0000000000..1e312c51a7 --- /dev/null +++ b/samples/sample1/main.c @@ -0,0 +1,59 @@ +// Simple compiler with no logging + +#include +#include +#include + +const char fragment_shader[] = + "float4 main(uniform float4 u_clear_color) : COLOR\n" + "{\n" + " return u_clear_color;\n" + "}" + ; + +const char vertex_shader[] = + "void main(\n" + "float3 aPosition,\n" + "float3 aColor,\n" + "uniform float4x4 wvp,\n" + "float4 out vPosition: POSITION,\n" + "float4 out vColor: COLOR)\n" + "{\n" + " vPosition = mul(float4(aPosition, 1.f), wvp);\n" + " vColor = float4(aColor, 1.f);\n" + "}" + ; + +void saveGXP(SceGxmProgram *p, uint32_t size, const char *fname) { + FILE *f = fopen(fname, "wb"); + fwrite(p, 1, size, f); + fclose(f); +} + +int main() { + // Initializing vitaShaRK + if (shark_init(NULL) < 0) // NOTE: libshacccg.suprx will need to be placed in ur0:data + return -1; + + // Compiling fragment shader + uint32_t size = sizeof(fragment_shader) - 1; + SceGxmProgram *p = shark_compile_shader(fragment_shader, &size, SHARK_FRAGMENT_SHADER); + + // Saving compiled GXP file on SD + if (p) saveGXP(p, size, "ux0:data/clear_f.gxp"); + + shark_clear_output(); + + // Compiling vertex shader + size = sizeof(vertex_shader) - 1; + p = shark_compile_shader(vertex_shader, &size, SHARK_VERTEX_SHADER); + + // Saving compiled GXP file on SD + if (p) saveGXP(p, size, "ux0:data/rgb_v.gxp"); + + shark_clear_output(); + + shark_end(); + + return 0; +} diff --git a/samples/sample2/Makefile b/samples/sample2/Makefile new file mode 100644 index 0000000000..e41643545a --- /dev/null +++ b/samples/sample2/Makefile @@ -0,0 +1,37 @@ +SAMPLE_NUM := 002 +TARGET := vitaShaRK-Sample$(SAMPLE_NUM) +SOURCES := . + +INCLUDES := include + +LIBS = -lvitashark -lSceLibKernel_stub -lSceShaccCg_stub + +CFILES := $(foreach dir,$(SOURCES), $(wildcard $(dir)/*.c)) +CPPFILES := $(foreach dir,$(SOURCES), $(wildcard $(dir)/*.cpp)) +BINFILES := $(foreach dir,$(DATA), $(wildcard $(dir)/*.bin)) +OBJS := $(addsuffix .o,$(BINFILES)) $(CFILES:.c=.o) $(CPPFILES:.cpp=.o) + +PREFIX = arm-vita-eabi +CC = $(PREFIX)-gcc +CXX = $(PREFIX)-g++ +CFLAGS = -g -Wl,-q -O2 -ftree-vectorize +CXXFLAGS = $(CFLAGS) -fno-exceptions -std=gnu++11 -fpermissive +ASFLAGS = $(CFLAGS) + +all: $(TARGET).vpk + +$(TARGET).vpk: eboot.bin + vita-mksfoex -s TITLE_ID=VSHARK$(SAMPLE_NUM) "$(TARGET)" param.sfo + vita-pack-vpk -s param.sfo -b eboot.bin $@ + +eboot.bin: $(TARGET).velf + vita-make-fself -s $< eboot.bin + +%.velf: %.elf + vita-elf-create $< $@ + +$(TARGET).elf: $(OBJS) + $(CC) $(CFLAGS) $^ $(LIBS) -o $@ + +clean: + @rm -rf *.velf *.elf *.vpk $(OBJS) param.sfo eboot.bin diff --git a/samples/sample2/main.c b/samples/sample2/main.c new file mode 100644 index 0000000000..0d585e0201 --- /dev/null +++ b/samples/sample2/main.c @@ -0,0 +1,85 @@ +// Simple compiler with file logging + +#include +#include +#include + +const char fragment_shader[] = + "float4 main(uniform float4 u_clear_color) : COLOR\n" + "{\n" + " return u_clear_color;\n" + "}" + ; + +const char vertex_shader[] = + "void main(\n" + "float3 aPosition,\n" + "float3 aColor,\n" + "uniform float4x4 wvp,\n" + "float4 out vPosition: POSITION,\n" + "float4 out vColor: COLOR)\n" + "{\n" + " vPosition = mul(float4(aPosition, 1.f), wvp);\n" + " vColor = float4(aColor, 1.f);\n" + "}" + ; + +char curr_compilation[256]; + +void log_cb(const char *msg, shark_log_level msg_level, int line) { + FILE *f = fopen("ux0:/data/shark.log", "a+"); + switch (msg_level) { + case SHARK_LOG_INFO: + fprintf(f, "%s) INFO: %s at line %d\n", curr_compilation, msg, line); + break; + case SHARK_LOG_WARNING: + fprintf(f, "%s) WARNING: %s at line %d\n", curr_compilation, msg, line); + break; + case SHARK_LOG_ERROR: + fprintf(f, "%s) ERROR: %s at line %d\n", curr_compilation, msg, line); + break; + default: + break; + } + fclose(f); +} + +void saveGXP(SceGxmProgram *p, uint32_t size, const char *fname) { + FILE *f = fopen(fname, "wb"); + fwrite(p, 1, size, f); + fclose(f); +} + +int main() { + // Initializing vitaShaRK + if (shark_init(NULL) < 0) // NOTE: libshacccg.suprx will need to be placed in ur0:data + return -1; + + // Setting up logger + shark_install_log_cb(log_cb); + shark_set_warnings_level(SHARK_WARN_MAX); + + // Compiling fragment shader + sprintf(curr_compilation, "clear_f.gxp"); + uint32_t size = sizeof(fragment_shader) - 1; + SceGxmProgram *p = shark_compile_shader(fragment_shader, &size, SHARK_FRAGMENT_SHADER); + + // Saving compiled GXP file on SD + if (p) saveGXP(p, size, "ux0:data/clear_f.gxp"); + + shark_clear_output(); + + // Compiling vertex shader + sprintf(curr_compilation, "rgb_v.gxp"); + size = sizeof(vertex_shader) - 1; + p = shark_compile_shader(vertex_shader, &size, SHARK_VERTEX_SHADER); + + // Saving compiled GXP file on SD + if (p) saveGXP(p, size, "ux0:data/rgb_v.gxp"); + + shark_clear_output(); + + shark_end(); + + return 0; +} diff --git a/shacccg.h b/shacccg.h new file mode 100644 index 0000000000..aa9f6f06eb --- /dev/null +++ b/shacccg.h @@ -0,0 +1,185 @@ +#ifndef _PSP2_SHACCCG_H +#define _PSP2_SHACCCG_H + +#ifdef __cplusplus +extern "C" { +#endif // def __cplusplus + +typedef struct SceShaccCgCompileOptions SceShaccCgCompileOptions; +typedef struct SceShaccCgSourceFile SceShaccCgSourceFile; +typedef struct SceShaccCgSourceLocation SceShaccCgSourceLocation; +typedef void const *SceShaccCgParameter; + +typedef SceShaccCgSourceFile* (*SceShaccCgCallbackOpenFile)( + const char *fileName, + const SceShaccCgSourceLocation *includedFrom, + const SceShaccCgCompileOptions *compileOptions, + const char **errorString); + +typedef void (*SceShaccCgCallbackReleaseFile)( + const SceShaccCgSourceFile *file, + const SceShaccCgCompileOptions *compileOptions); + +typedef const char* (*SceShaccCgCallbackLocateFile)( + const char *fileName, + const SceShaccCgSourceLocation *includedFrom, + uint32_t searchPathCount, + const char *const*searchPaths, + const SceShaccCgCompileOptions *compileOptions, + const char **errorString); + +typedef const char* (*SceShaccCgCallbackAbsolutePath)( + const char *fileName, + const SceShaccCgSourceLocation *includedFrom, + const SceShaccCgCompileOptions *compileOptions); + +typedef void (*SceShaccCgCallbackReleaseFileName)( + const char *fileName, + const SceShaccCgCompileOptions *compileOptions); + +typedef int32_t (*SceShaccCgCallbackFileDate)( + const SceShaccCgSourceFile *file, + const SceShaccCgSourceLocation *includedFrom, + const SceShaccCgCompileOptions *compileOptions, + int64_t *timeLastStatusChange, + int64_t *timeLastModified); + +typedef enum SceShaccCgDiagnosticLevel { + SCE_SHACCCG_DIAGNOSTIC_LEVEL_INFO, + SCE_SHACCCG_DIAGNOSTIC_LEVEL_WARNING, + SCE_SHACCCG_DIAGNOSTIC_LEVEL_ERROR +} SceShaccCgDiagnosticLevel; + +typedef enum SceShaccCgTargetProfile { + SCE_SHACCCG_PROFILE_VP, + SCE_SHACCCG_PROFILE_FP +} SceShaccCgTargetProfile; + +typedef enum SceShaccCgCallbackDefaults { + SCE_SHACCCG_SYSTEM_FILES, + SCE_SHACCCG_TRIVIAL +} SceShaccCgCallbackDefaults; + +typedef enum SceShaccCgLocale { + SCE_SHACCCG_ENGLISH, + SCE_SHACCCG_JAPANESE +} SceShaccCgLocale; + +typedef struct SceShaccCgSourceFile { + const char *fileName; + const char *text; + uint32_t size; +} SceShaccCgSourceFile; + +typedef struct SceShaccCgSourceLocation { + const SceShaccCgSourceFile *file; + uint32_t lineNumber; + uint32_t columnNumber; +} SceShaccCgSourceLocation; + +typedef struct SceShaccCgCallbackList { + SceShaccCgCallbackOpenFile openFile; + SceShaccCgCallbackReleaseFile releaseFile; + SceShaccCgCallbackLocateFile locateFile; + SceShaccCgCallbackAbsolutePath absolutePath; + SceShaccCgCallbackReleaseFileName releaseFileName; + SceShaccCgCallbackFileDate fileDate; +} SceShaccCgCallbackList; + +typedef struct SceShaccCgCompileOptions { + const char *mainSourceFile; + SceShaccCgTargetProfile targetProfile; + const char *entryFunctionName; + uint32_t searchPathCount; + const char* const *searchPaths; + uint32_t macroDefinitionCount; + const char* const *macroDefinitions; + uint32_t includeFileCount; + const char* const *includeFiles; + uint32_t suppressedWarningsCount; + const uint32_t *suppressedWarnings; + SceShaccCgLocale locale; + int32_t useFx; + int32_t noStdlib; + int32_t optimizationLevel; + int32_t useFastmath; + int32_t useFastprecision; + int32_t useFastint; + int32_t warningsAsErrors; + int32_t performanceWarnings; + int32_t warningLevel; + int32_t pedantic; + int32_t pedanticError; + int field_5C; + int field_60; + int field_64; +} SceShaccCgCompileOptions; + +typedef struct SceShaccCgDiagnosticMessage { + SceShaccCgDiagnosticLevel level; + uint32_t code; + const SceShaccCgSourceLocation *location; + const char *message; +} SceShaccCgDiagnosticMessage; + +typedef struct SceShaccCgCompileOutput { + const uint8_t *programData; + uint32_t programSize; + int32_t diagnosticCount; + const SceShaccCgDiagnosticMessage *diagnostics; +} SceShaccCgCompileOutput; + + +int SceShaccCg_0205DE96(int); +int SceShaccCg_07DDFC78(int); +int SceShaccCg_0E1285A6(int); +int SceShaccCg_152971B1(int); +int SceShaccCg_17223BEB(int); +int SceShaccCg_2654E73A(int); +int SceShaccCg_268FAEE9(int); + +int sceShaccCgInitializeCompileOptions( + SceShaccCgCompileOptions *options); + +int SceShaccCg_4595A388(int); +int SceShaccCg_46FA0303(int); +int SceShaccCg_56BFA825(int); +int SceShaccCg_648739F3(int); + +SceShaccCgCompileOutput const *sceShaccCgCompileProgram( + const SceShaccCgCompileOptions *options, + const SceShaccCgCallbackList *callbacks, + int unk); + +int SceShaccCg_6BB58825(int); +int sceShaccCgSetDefaultAllocator(void *(*malloc_cb)(unsigned int), void (*free_cb)(void *)); +int SceShaccCg_6FB40CA9(int); +int SceShaccCg_7B2CF324(int); +int SceShaccCg_7BC25091(int); +int SceShaccCg_7F430CCD(int); +int SceShaccCg_95F57A23(int); +int SceShaccCg_A067C481(int); +int SceShaccCg_A13A8A1E(int); +int SceShaccCg_A56B1A5B(int); +int SceShaccCg_A7930FF6(int); + +void sceShaccCgInitializeCallbackList( + SceShaccCgCallbackList *callbacks, + SceShaccCgCallbackDefaults defaults); + +void sceShaccCgDestroyCompileOutput( + SceShaccCgCompileOutput const *output); + +int SceShaccCg_B4AC9943(int); +int SceShaccCg_BB703EE1(int); +int SceShaccCg_D4378DB1(int); +int SceShaccCg_DAD4AAE4(int); +int SceShaccCg_DF3DDCFD(int); +int SceShaccCg_EF8D59D6(int); +int SceShaccCg_F4BAB902(int); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* _PSP2_SHACCCG_H */ diff --git a/source/vitashark.c b/source/vitashark.c new file mode 100644 index 0000000000..ff473aa0b3 --- /dev/null +++ b/source/vitashark.c @@ -0,0 +1,124 @@ +/* + * This file is part of vitaGL + * Copyright 2017, 2018, 2019, 2020 Rinnegatamante + * Copyright 2020 Asakura Reiko + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "vitashark.h" +#include +#include + +// Default path for SceShaccCg module location +#define DEFAULT_SHACCCG_PATH "ur0:/data/libshacccg.suprx" + +static void (*shark_log_cb)(const char *msg, shark_log_level msg_level, int line) = NULL; +static shark_warn_level shark_warnings_level = SHARK_WARN_SILENT; + +static SceUID shark_module_id = 0; +static uint8_t shark_initialized = 0; +static SceShaccCgCompileOutput *shark_output = NULL; +static SceShaccCgSourceFile shark_input; + +// Dummy Open File callback +static SceShaccCgSourceFile *shark_open_file_cb(const char *fileName, + const SceShaccCgSourceLocation *includedFrom, + const SceShaccCgCompileOptions *compileOptions, + const char **errorString) +{ + return &shark_input; +} + +int shark_init(const char *path) { + // Initializing sceShaccCg module + if (!shark_initialized) { + shark_module_id = sceKernelLoadStartModule(path ? path : DEFAULT_SHACCCG_PATH, 0, NULL, 0, NULL, NULL); + if (shark_module_id < 0) return -1; + sceShaccCgSetDefaultAllocator(malloc, free); + shark_initialized = 1; + } + return 0; +} + +void shark_end() { + if (!shark_initialized) return; + + // Terminating sceShaccCg module + sceKernelStopUnloadModule(shark_module_id, 0, NULL, 0, NULL, NULL); + shark_initialized = 0; +} + +void shark_install_log_cb(void (*cb)(const char *msg, shark_log_level msg_level, int line)) { + shark_log_cb = cb; +} + +void shark_set_warnings_level(shark_warn_level level) { + // Changing current warnings level + shark_warnings_level = level; +} + +void shark_clear_output() { + // Clearing sceShaccCg output + if (shark_output) { + sceShaccCgDestroyCompileOutput(shark_output); + shark_output = NULL; + } +} + +SceGxmProgram *shark_compile_shader_extended(const char *src, uint32_t *size, shark_type type, shark_opt opt, int32_t use_fastmath, int32_t use_fastprecision, int32_t use_fastint) { + if (!shark_initialized) return NULL; + + // Forcing usage for memory source for the shader to compile + shark_input.fileName = ""; + shark_input.text = src; + shark_input.size = *size; + + // Properly configuring SceShaccCg with requqested settings + SceShaccCgCompileOptions options = {0}; + options.mainSourceFile = shark_input.fileName; + options.targetProfile = type; + options.entryFunctionName = "main"; + options.macroDefinitions = NULL; + options.useFx = 1; + options.warningLevel = shark_warnings_level; + options.optimizationLevel = opt; + options.useFastmath = use_fastmath; + options.useFastint = use_fastint; + options.useFastprecision = use_fastprecision; + options.pedantic = shark_warnings_level > SHARK_WARN_MEDIUM ? SHARK_ENABLE : SHARK_DISABLE; + options.performanceWarnings = shark_warnings_level > SHARK_WARN_SILENT ? SHARK_ENABLE : SHARK_DISABLE; + + // Executing shader compilation + SceShaccCgCallbackList callbacks = {0}; + sceShaccCgInitializeCallbackList(&callbacks, SCE_SHACCCG_TRIVIAL); + callbacks.openFile = shark_open_file_cb; + const SceShaccCgCompileOutput *shark_output = sceShaccCgCompileProgram(&options, &callbacks, 0); + + // Executing logging + if (shark_log_cb) { + for (int i = 0; i < shark_output->diagnosticCount; ++i) { + const SceShaccCgDiagnosticMessage *log = &shark_output->diagnostics[i]; + shark_log_cb(log->message, log->level, log->location->lineNumber); + } + } + + // Returning output + if (shark_output->programData) *size = shark_output->programSize; + return (SceGxmProgram *)shark_output->programData; +} + +SceGxmProgram *shark_compile_shader(const char *src, uint32_t *size, shark_type type) { + return shark_compile_shader_extended(src, size, type, SHARK_OPT_DEFAULT, SHARK_DISABLE, SHARK_DISABLE, SHARK_DISABLE); +} diff --git a/source/vitashark.h b/source/vitashark.h new file mode 100644 index 0000000000..61d1966084 --- /dev/null +++ b/source/vitashark.h @@ -0,0 +1,75 @@ +/* + * This file is part of vitaShaRK + * Copyright 2017, 2018, 2019, 2020 Rinnegatamante + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _VITASHARK_H_ +#define _VITASHARK_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef enum shark_opt { + SHARK_OPT_SLOW, //!< Equivalent to O0 + SHARK_OPT_SAFE, //!< Equivalent to O1 + SHARK_OPT_DEFAULT, //!< Equivalent to O2 + SHARK_OPT_FAST, //!< Equivalent to O3 + SHARK_OPT_UNSAFE //!< Equivalent to Ofast +} shark_opt; + +typedef enum shark_type { + SHARK_VERTEX_SHADER, + SHARK_FRAGMENT_SHADER +} shark_type; + +typedef enum shark_log_level { + SHARK_LOG_INFO, + SHARK_LOG_WARNING, + SHARK_LOG_ERROR +} shark_log_level; + +typedef enum shark_warn_level { + SHARK_WARN_SILENT, + SHARK_WARN_LOW, + SHARK_WARN_MEDIUM, + SHARK_WARN_HIGH, + SHARK_WARN_MAX +} shark_warn_level; + +#define SHARK_DISABLE 0 +#define SHARK_ENABLE 1 + +// Init/term routines +int shark_init(const char *path); //!< Initializes runtime shader compiler +void shark_end(); //!< Terminates runtime shader compiler and frees used memory + +// Compiling routines +SceGxmProgram *shark_compile_shader_extended(const char *src, uint32_t *size, shark_type type, shark_opt opt, int32_t use_fastmath, int32_t use_fastprecision, int32_t use_fastint); //!< Compiles a shader with extended settings +SceGxmProgram *shark_compile_shader(const char *src, uint32_t *size, shark_type type); //!< Compiles a shader +void shark_clear_output(); //!< Clears output of a compilation attempt + +// Logging routines +void shark_install_log_cb(void (*cb)(const char *msg, shark_log_level msg_level, int line)); //!< Installs a log function for info, warnings and errors +void shark_set_warnings_level(shark_warn_level level); //!< Sets warnings level for logging + +#ifdef __cplusplus +} +#endif + +#endif