From cd05cc45fffb4d45452caddcb3e9ea7c077d1e72 Mon Sep 17 00:00:00 2001 From: smea Date: Fri, 2 Jan 2015 17:15:44 -0800 Subject: [PATCH] started work on shaderProgram --- libctru/include/3ds.h | 2 +- libctru/include/3ds/gpu/shaderProgram.h | 29 ++++++ libctru/include/3ds/gpu/{shdr.h => shbin.h} | 11 +- libctru/source/gpu/gpu.c | 2 +- libctru/source/gpu/shaderProgram.c | 108 ++++++++++++++++++++ libctru/source/gpu/{shdr.c => shbin.c} | 6 +- 6 files changed, 147 insertions(+), 11 deletions(-) create mode 100644 libctru/include/3ds/gpu/shaderProgram.h rename libctru/include/3ds/gpu/{shdr.h => shbin.h} (88%) create mode 100644 libctru/source/gpu/shaderProgram.c rename libctru/source/gpu/{shdr.c => shbin.c} (97%) diff --git a/libctru/include/3ds.h b/libctru/include/3ds.h index 1713255..83e6d05 100644 --- a/libctru/include/3ds.h +++ b/libctru/include/3ds.h @@ -34,7 +34,7 @@ extern "C" { #include <3ds/gpu/gx.h> #include <3ds/gpu/gpu.h> -#include <3ds/gpu/shdr.h> +#include <3ds/gpu/shbin.h> #include <3ds/sdmc.h> diff --git a/libctru/include/3ds/gpu/shaderProgram.h b/libctru/include/3ds/gpu/shaderProgram.h new file mode 100644 index 0000000..e5f2cba --- /dev/null +++ b/libctru/include/3ds/gpu/shaderProgram.h @@ -0,0 +1,29 @@ +#pragma once + +#include <3ds/types.h> +#include <3ds/gpu/shbin.h> + +// this structure describes an instance of either a vertex or geometry shader +typedef struct +{ + DVLE_s* dvle; + u16 boolUniforms; +}shaderInstance_s; + +// this structure describes an instance of a full shader program +typedef struct +{ + shaderInstance_s* vertexShader; + shaderInstance_s* geometryShader; +}shaderProgram_s; + +Result shaderInstanceInit(shaderInstance_s* si, DVLE_s* dvle); +Result shaderInstanceFree(shaderInstance_s* si); +Result shaderInstanceSetBool(shaderInstance_s* si, int id, bool value); +Result shaderInstanceGetBool(shaderInstance_s* si, int id, bool* value); + +Result shaderProgramInit(shaderProgram_s* sp); +Result shaderProgramFree(shaderProgram_s* sp); +Result shaderProgramSetVsh(shaderProgram_s* sp, DVLE_s* dvle); +Result shaderProgramSetGsh(shaderProgram_s* sp, DVLE_s* dvle); +Result shaderProgramUse(shaderProgram_s* sp); diff --git a/libctru/include/3ds/gpu/shdr.h b/libctru/include/3ds/gpu/shbin.h similarity index 88% rename from libctru/include/3ds/gpu/shdr.h rename to libctru/include/3ds/gpu/shbin.h index ecfe41f..89cbde9 100644 --- a/libctru/include/3ds/gpu/shdr.h +++ b/libctru/include/3ds/gpu/shbin.h @@ -3,7 +3,7 @@ typedef enum{ VERTEX_SHDR=0x0, GEOMETRY_SHDR=0x1 -}SHDR_type; +}DVLE_type; typedef enum{ RESULT_POSITION = 0x0, @@ -14,7 +14,7 @@ typedef enum{ RESULT_TEXCOORD1 = 0x5, RESULT_TEXCOORD2 = 0x6, RESULT_VIEW = 0x8 -}SHDR_outType; +}DVLE_outputAttribute_t; typedef struct{ u32 codeSize; @@ -42,7 +42,7 @@ typedef struct{ }DVLE_uniformEntry_s; typedef struct{ - SHDR_type type; + DVLE_type type; u32 mainOffset, endmainOffset; u32 constTableSize; DVLE_constEntry_s* constTableData; @@ -59,14 +59,13 @@ typedef struct{ DVLE_s* DVLE; }DVLB_s; - DVLB_s* SHDR_ParseSHBIN(u32* shbinData, u32 shbinSize); void SHDR_UseProgram(DVLB_s* dvlb, u8 id); void SHDR_FreeDVLB(DVLB_s* dvlb); s8 SHDR_GetUniformRegister(DVLB_s* dvlb, const char* name, u8 programID); -void DVLP_SendCode(DVLP_s* dvlp, SHDR_type type); -void DVLP_SendOpDesc(DVLP_s* dvlp, SHDR_type type); +void DVLP_SendCode(DVLP_s* dvlp, DVLE_type type); +void DVLP_SendOpDesc(DVLP_s* dvlp, DVLE_type type); void DVLE_SendOutmap(DVLE_s* dvle); void DVLE_SendConstants(DVLE_s* dvle); diff --git a/libctru/source/gpu/gpu.c b/libctru/source/gpu/gpu.c index 11703cb..ba36fe1 100644 --- a/libctru/source/gpu/gpu.c +++ b/libctru/source/gpu/gpu.c @@ -7,7 +7,7 @@ #include <3ds/types.h> #include <3ds/gpu/gpu.h> #include <3ds/gpu/gx.h> -#include <3ds/gpu/shdr.h> +#include <3ds/gpu/shbin.h> u32* gpuCmdBuf; u32 gpuCmdBufSize; diff --git a/libctru/source/gpu/shaderProgram.c b/libctru/source/gpu/shaderProgram.c new file mode 100644 index 0000000..5a28802 --- /dev/null +++ b/libctru/source/gpu/shaderProgram.c @@ -0,0 +1,108 @@ +#include +#include <3ds/types.h> +#include <3ds/gpu/shaderProgram.h> + +Result shaderInstanceInit(shaderInstance_s* si, DVLE_s* dvle) +{ + if(!si || !dvle)return -1; + + si->dvle = dvle; + si->boolUniforms = 0xFFFF; + + return 0; +} + +Result shaderInstanceFree(shaderInstance_s* si) +{ + if(!si)return -1; + + free(si); + + return 0; +} + +Result shaderInstanceSetBool(shaderInstance_s* si, int id, bool value) +{ + if(!si)return -1; + if(id<0 || id>15)return -2; + + si->boolUniforms &= ~(1<boolUniforms |= (!value)<15)return -2; + if(!value)return -3; + + *value = !((si->boolUniforms>>id)&1); + + return 0; +} + +Result shaderProgramInit(shaderProgram_s* sp) +{ + if(!sp)return -1; + + sp->vertexShader = NULL; + sp->geometryShader = NULL; + + return 0; +} + +Result shaderProgramFree(shaderProgram_s* sp) +{ + if(!sp)return -1; + + shaderInstanceFree(sp->vertexShader); + shaderInstanceFree(sp->geometryShader); + + free(sp); + + return 0; +} + +Result shaderProgramSetVsh(shaderProgram_s* sp, DVLE_s* dvle) +{ + if(!sp || !dvle)return -1; + if(dvle->type != VERTEX_SHDR)return -2; + + if(sp->vertexShader)shaderInstanceFree(sp->vertexShader); + + sp->vertexShader = (shaderInstance_s*)malloc(sizeof(shaderInstance_s)); + if(!sp->vertexShader)return -3; + + return shaderInstanceInit(sp->vertexShader, dvle); +} + +Result shaderProgramSetGsh(shaderProgram_s* sp, DVLE_s* dvle) +{ + if(!sp || !dvle)return -1; + if(dvle->type != GEOMETRY_SHDR)return -2; + + if(sp->geometryShader)shaderInstanceFree(sp->geometryShader); + + sp->geometryShader = (shaderInstance_s*)malloc(sizeof(shaderInstance_s)); + if(!sp->geometryShader)return -3; + + return shaderInstanceInit(sp->geometryShader, dvle); +} + +Result shaderProgramUse(shaderProgram_s* sp) +{ + if(!sp)return -1; + + if(!sp->vertexShader)return -2; + + if(!sp->geometryShader) + { + // only deal with vertex shader + }else{ + // setup both vertex and geometry shader + } + + return 0; +} diff --git a/libctru/source/gpu/shdr.c b/libctru/source/gpu/shbin.c similarity index 97% rename from libctru/source/gpu/shdr.c rename to libctru/source/gpu/shbin.c index f79fbcd..e825f85 100644 --- a/libctru/source/gpu/shdr.c +++ b/libctru/source/gpu/shbin.c @@ -6,7 +6,7 @@ #include #include <3ds/types.h> #include <3ds/gpu/gpu.h> -#include <3ds/gpu/shdr.h> +#include <3ds/gpu/shbin.h> //please don't feed this an invalid SHBIN DVLB_s* SHDR_ParseSHBIN(u32* shbinData, u32 shbinSize) @@ -73,7 +73,7 @@ s8 SHDR_GetUniformRegister(DVLB_s* dvlb, const char* name, u8 programID) return -1; } -void DVLP_SendCode(DVLP_s* dvlp, SHDR_type type) +void DVLP_SendCode(DVLP_s* dvlp, DVLE_type type) { if(!dvlp)return; @@ -87,7 +87,7 @@ void DVLP_SendCode(DVLP_s* dvlp, SHDR_type type) GPUCMD_AddWrite(GPUREG_VSH_CODETRANSFER_END+regOffset, 0x00000001); } -void DVLP_SendOpDesc(DVLP_s* dvlp, SHDR_type type) +void DVLP_SendOpDesc(DVLP_s* dvlp, DVLE_type type) { if(!dvlp)return;