]> Chaos Git - corbenik/ctrulib.git/commitdiff
GPU : GPU_SetAttributeBuffers, GPU_SetStencilTest, GPU_SetFaceCulling
authorsmea <smealum@gmail.com>
Thu, 20 Mar 2014 13:40:57 +0000 (14:40 +0100)
committersmea <smealum@gmail.com>
Thu, 20 Mar 2014 13:40:57 +0000 (14:40 +0100)
libctru/include/ctr/GPU.h
libctru/source/GPU.c

index 14ce15d2520ca78e0e307b40ec8f1c3b02426678..9a6e145e415b22ed25d1769a9f750d37a0bedb00 100644 (file)
@@ -38,10 +38,29 @@ typedef enum
        GPU_GEQUAL = 7
 }GPU_TESTFUNC;
 
+typedef enum{
+       GPU_BYTE = 0,
+       GPU_UNSIGNED_BYTE = 1,
+       GPU_SHORT = 2,
+       GPU_FLOAT = 3
+}GPU_FORMATS;
+
+//defines for CW ?
+typedef enum{
+       GPU_CULL_NONE = 0,
+       GPU_CULL_FRONT_CCW = 1,
+       GPU_CULL_BACK_CCW = 2
+}GPU_CULLMODE;
+
+#define GPU_ATTRIBFMT(i, n, f) (((((n)-1)<<2)|((f)&3))<<((i)*4))
+
 void GPU_SetUniform(u32 startreg, u32* data, u32 numreg);
 void GPU_SetViewport(u32* depthBuffer, u32* colorBuffer, u32 x, u32 y, u32 w, u32 h);
 void GPU_DepthRange(float nearVal, float farVal);
 void GPU_SetDepthTest(bool enable, GPU_TESTFUNC function, u8 ref);
+void GPU_SetStencilTest(bool enable, GPU_TESTFUNC function, u8 ref);
+void GPU_SetAttributeBuffers(u8 totalAttributes, u32* baseAddress, u64 attributeFormats, u16 attributeMask, u64 attributePermutation, u8 numBuffers, u32 bufferOffsets[], u64 bufferPermutations[], u8 bufferNumAttributes[]);
+void GPU_SetFaceCulling(GPU_CULLMODE mode);
 void GPU_SetTexture(u32* data, u16 width, u16 height, u32 param, GPU_TEXCOLOR colorType);
 
 #endif
index f066e3f317a53616023b94ef5b235ac6c6393cb9..1d8c6ae9a455c6ba82ad75193663c828dd25e495 100644 (file)
@@ -200,6 +200,7 @@ void GPU_SetUniform(u32 startreg, u32* data, u32 numreg)
        GPUCMD_Add(0x000F02C1, data, numreg*4);
 }
 
+//TODO : fix
 u32 f32tof24(float f)
 {
        if(!f)return 0;
@@ -253,8 +254,8 @@ void GPU_SetViewport(u32* depthBuffer, u32* colorBuffer, u32 x, u32 y, u32 w, u3
        GPUCMD_Add(0x800F011C, param, 0x00000003);
 
        GPUCMD_AddSingleParam(0x000F006E, f116e);
-       GPUCMD_AddSingleParam(0x000F0116, 0x00000003); //depth format
-       GPUCMD_AddSingleParam(0x000F0117, 0x00000002); //color format
+       GPUCMD_AddSingleParam(0x000F0116, 0x00000003); //depth buffer format
+       GPUCMD_AddSingleParam(0x000F0117, 0x00000002); //color buffer format
        GPUCMD_AddSingleParam(0x000F011B, 0x00000000); //?
 
        param[0x0]=f32tof24(fw/2);
@@ -269,6 +270,13 @@ void GPU_SetViewport(u32* depthBuffer, u32* colorBuffer, u32 x, u32 y, u32 w, u3
        param[0x1]=0x00000000;
        param[0x2]=((h-1)<<16)|((w-1)&0xFFFF);
        GPUCMD_Add(0x800F0065, param, 0x00000003);
+
+       //enable depth buffer
+       param[0x0]=0x00000000;
+       param[0x1]=0x0000000F;
+       param[0x2]=0x00000002;
+       param[0x3]=0x00000002;
+       GPUCMD_Add(0x800F0112, param, 0x00000004);
 }
 
 void GPU_DepthRange(float nearVal, float farVal)
@@ -278,6 +286,11 @@ void GPU_DepthRange(float nearVal, float farVal)
        GPUCMD_AddSingleParam(0x000F004E, f32tof24(farVal));
 }
 
+void GPU_SetStencilTest(bool enable, GPU_TESTFUNC function, u8 ref)
+{
+       GPUCMD_AddSingleParam(0x000F0105, (enable&1)|((function&7)<<4)|(ref<<8));
+}
+
 void GPU_SetDepthTest(bool enable, GPU_TESTFUNC function, u8 ref)
 {
        GPUCMD_AddSingleParam(0x000F0107, (enable&1)|((function&7)<<4)|(ref<<8));
@@ -290,3 +303,47 @@ void GPU_SetTexture(u32* data, u16 width, u16 height, u32 param, GPU_TEXCOLOR co
        GPUCMD_AddSingleParam(0x000F0082, (width)|(height<<16));
        GPUCMD_AddSingleParam(0x000F0083, param);
 }
+
+const u8 GPU_FORMATSIZE[4]={1,1,2,4};
+
+void GPU_SetAttributeBuffers(u8 totalAttributes, u32* baseAddress, u64 attributeFormats, u16 attributeMask, u64 attributePermutation, u8 numBuffers, u32 bufferOffsets[], u64 bufferPermutations[], u8 bufferNumAttributes[])
+{
+       u32 param[0x28];
+
+       memset(param, 0x00, 0x28*4);
+
+       param[0x0]=((u32)baseAddress)>>3;
+       param[0x1]=attributeFormats&0xFFFFFFFF;
+       param[0x2]=((totalAttributes-1)<<28)|((attributeMask&0xFFF)<<16)|((attributeFormats>>32)&0xFFFF);
+
+       int i, j;
+       u8 sizeTable[0xC];
+       for(i=0;i<totalAttributes;i++)
+       {
+               u8 v=attributeFormats&0xF;
+               sizeTable[i]=GPU_FORMATSIZE[v&3]*((v>>2)+1);
+               attributeFormats>>=4;
+       }
+
+       for(i=0;i<numBuffers;i++)
+       {
+               u16 stride=0;
+               param[3*(i+1)+0]=bufferOffsets[i];
+               param[3*(i+1)+1]=bufferPermutations[i]&0xFFFFFFFF;
+               for(j=0;j<bufferNumAttributes[i];j++)stride+=sizeTable[(bufferPermutations[i]>>(4*j))&0xF];
+               param[3*(i+1)+2]=(bufferNumAttributes[i]<<28)|((stride&0xFFF)<<16)|((bufferPermutations[i]>>32)&0xFFFF);
+       }
+
+       GPUCMD_Add(0x800F0200, param, 0x00000027);
+
+       GPUCMD_AddSingleParam(0x000B02B9, 0xA0000000|(totalAttributes-1));
+       GPUCMD_AddSingleParam(0x000F0242, (totalAttributes-1));
+
+       GPUCMD_AddSingleParam(0x000F02BB, attributePermutation&0xFFFFFFFF);
+       GPUCMD_AddSingleParam(0x000F02BC, (attributePermutation>>32)&0xFFFF);
+}
+
+void GPU_SetFaceCulling(GPU_CULLMODE mode)
+{
+       GPUCMD_AddSingleParam(0x000F0040, mode&0x3); 
+}