2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// 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 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#include "math/lin/matrix4x4.h"
|
|
|
|
|
|
|
|
#include "../../Core/MemMap.h"
|
|
|
|
#include "../ge_constants.h"
|
|
|
|
|
|
|
|
#include "VertexDecoder.h"
|
|
|
|
|
2012-12-19 20:29:02 +00:00
|
|
|
void PrintDecodedVertex(VertexReader &vtx) {
|
|
|
|
if (vtx.hasNormal())
|
|
|
|
{
|
|
|
|
float nrm[3];
|
|
|
|
vtx.ReadNrm(nrm);
|
|
|
|
printf("N: %f %f %f\n", nrm[0], nrm[1], nrm[2]);
|
|
|
|
}
|
|
|
|
if (vtx.hasUV()) {
|
|
|
|
float uv[2];
|
|
|
|
vtx.ReadUV(uv);
|
|
|
|
printf("TC: %f %f\n", uv[0], uv[1]);
|
|
|
|
}
|
|
|
|
if (vtx.hasColor0()) {
|
|
|
|
float col0[4];
|
|
|
|
vtx.ReadColor0(col0);
|
|
|
|
printf("C0: %f %f %f %f\n", col0[0], col0[1], col0[2], col0[3]);
|
|
|
|
}
|
|
|
|
if (vtx.hasColor0()) {
|
|
|
|
float col1[3];
|
|
|
|
vtx.ReadColor1(col1);
|
|
|
|
printf("C1: %f %f %f\n", col1[0], col1[1], col1[2]);
|
|
|
|
}
|
|
|
|
// Etc..
|
|
|
|
float pos[3];
|
|
|
|
vtx.ReadPos(pos);
|
|
|
|
printf("P: %f %f %f\n", pos[0], pos[1], pos[2]);
|
2012-12-05 03:45:28 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
const int tcsize[4] = {0,2,4,8}, tcalign[4] = {0,1,2,4};
|
|
|
|
const int colsize[8] = {0,0,0,0,2,2,2,4}, colalign[8] = {0,0,0,0,2,2,2,4};
|
|
|
|
const int nrmsize[4] = {0,3,6,12}, nrmalign[4] = {0,1,2,4};
|
|
|
|
const int possize[4] = {0,3,6,12}, posalign[4] = {0,1,2,4};
|
|
|
|
const int wtsize[4] = {0,1,2,4}, wtalign[4] = {0,1,2,4};
|
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
inline int align(int n, int align) {
|
2012-12-05 03:45:28 +00:00
|
|
|
return (n + (align - 1)) & ~(align - 1);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
int DecFmtSize(u8 fmt) {
|
|
|
|
switch (fmt) {
|
|
|
|
case DEC_NONE: return 0;
|
|
|
|
case DEC_FLOAT_1: return 4;
|
|
|
|
case DEC_FLOAT_2: return 8;
|
|
|
|
case DEC_FLOAT_3: return 12;
|
|
|
|
case DEC_FLOAT_4: return 16;
|
|
|
|
case DEC_S8_3: return 4;
|
|
|
|
case DEC_S16_3: return 8;
|
|
|
|
case DEC_U8_4: return 4;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 15:03:40 +00:00
|
|
|
#if 0
|
2012-12-19 17:35:37 +00:00
|
|
|
// This is what the software transform spits out, and thus w
|
|
|
|
DecVtxFormat GetTransformedVtxFormat(const DecVtxFormat &fmt) {
|
|
|
|
DecVtxFormat tfm = {0};
|
|
|
|
int size = 0;
|
|
|
|
int offset = 0;
|
|
|
|
// Weights disappear during transform.
|
|
|
|
if (fmt.uvfmt) {
|
|
|
|
// UV always becomes float2.
|
|
|
|
tfm.uvfmt = DEC_FLOAT_2;
|
|
|
|
tfm.uvoff = offset;
|
|
|
|
offset += DecFmtSize(tfm.uvfmt);
|
|
|
|
}
|
|
|
|
// We always (?) get two colors out, they're floats (although we'd probably be fine with less precision).
|
|
|
|
tfm.c0fmt = DEC_FLOAT_4;
|
|
|
|
tfm.c0off = offset;
|
|
|
|
offset += DecFmtSize(tfm.c0fmt);
|
|
|
|
tfm.c1fmt = DEC_FLOAT_3; // color1 (specular) doesn't have alpha.
|
|
|
|
tfm.c1off = offset;
|
|
|
|
offset += DecFmtSize(tfm.c1fmt);
|
|
|
|
// We never get a normal, it's gone.
|
|
|
|
// But we do get a position, and it's always float3.
|
|
|
|
tfm.posfmt = DEC_FLOAT_3;
|
|
|
|
tfm.posoff = offset;
|
|
|
|
offset += DecFmtSize(tfm.posfmt);
|
|
|
|
// Update stride.
|
|
|
|
tfm.stride = offset;
|
|
|
|
return tfm;
|
|
|
|
}
|
2012-12-20 15:03:40 +00:00
|
|
|
#endif
|
2012-12-19 17:35:37 +00:00
|
|
|
|
2012-12-19 23:48:57 +00:00
|
|
|
void VertexDecoder::Step_WeightsU8() const
|
|
|
|
{
|
2012-12-20 17:31:21 +00:00
|
|
|
float *wt = (float *)(decoded_ + decFmt.w0off);
|
2012-12-19 23:48:57 +00:00
|
|
|
const u8 *wdata = (const u8*)(ptr_);
|
|
|
|
for (int j = 0; j < nweights; j++)
|
|
|
|
wt[j] = (float)wdata[j] / 128.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_WeightsU16() const
|
|
|
|
{
|
2012-12-20 17:31:21 +00:00
|
|
|
float *wt = (float *)(decoded_ + decFmt.w0off);
|
2012-12-19 23:48:57 +00:00
|
|
|
const u16 *wdata = (const u16*)(ptr_);
|
|
|
|
for (int j = 0; j < nweights; j++)
|
|
|
|
wt[j] = (float)wdata[j] / 32768.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_WeightsFloat() const
|
|
|
|
{
|
2012-12-20 17:31:21 +00:00
|
|
|
float *wt = (float *)(decoded_ + decFmt.w0off);
|
2012-12-19 23:48:57 +00:00
|
|
|
const float *wdata = (const float*)(ptr_);
|
|
|
|
for (int j = 0; j < nweights; j++)
|
|
|
|
wt[j] = wdata[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_TcU8() const
|
|
|
|
{
|
2012-12-20 17:31:21 +00:00
|
|
|
float *uv = (float *)(decoded_ + decFmt.uvoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
const u8 *uvdata = (const u8*)(ptr_ + tcoff);
|
|
|
|
for (int j = 0; j < 2; j++)
|
|
|
|
uv[j] = (float)uvdata[j] / 128.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_TcU16() const
|
|
|
|
{
|
2012-12-20 17:31:21 +00:00
|
|
|
float *uv = (float *)(decoded_ + decFmt.uvoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
const u16 *uvdata = (const u16*)(ptr_ + tcoff);
|
|
|
|
uv[0] = (float)uvdata[0] / 32768.0f;
|
|
|
|
uv[1] = (float)uvdata[1] / 32768.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_TcU16Through() const
|
|
|
|
{
|
2012-12-20 17:31:21 +00:00
|
|
|
float *uv = (float *)(decoded_ + decFmt.uvoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
const u16 *uvdata = (const u16*)(ptr_ + tcoff);
|
|
|
|
uv[0] = (float)uvdata[0] / (float)(gstate_c.curTextureWidth);
|
|
|
|
uv[1] = (float)uvdata[1] / (float)(gstate_c.curTextureHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_TcFloat() const
|
|
|
|
{
|
2012-12-20 17:31:21 +00:00
|
|
|
float *uv = (float *)(decoded_ + decFmt.uvoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
const float *uvdata = (const float*)(ptr_ + tcoff);
|
|
|
|
uv[0] = uvdata[0];
|
|
|
|
uv[1] = uvdata[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_TcFloatThrough() const
|
|
|
|
{
|
2012-12-20 17:31:21 +00:00
|
|
|
float *uv = (float *)(decoded_ + decFmt.uvoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
const float *uvdata = (const float*)(ptr_ + tcoff);
|
|
|
|
uv[0] = uvdata[0] / (float)(gstate_c.curTextureWidth);
|
|
|
|
uv[1] = uvdata[1] / (float)(gstate_c.curTextureHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_Color565() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
u8 *c = decoded_ + decFmt.c0off;
|
2012-12-19 23:48:57 +00:00
|
|
|
u16 cdata = *(u16*)(ptr_ + coloff);
|
|
|
|
c[0] = Convert5To8(cdata & 0x1f);
|
|
|
|
c[1] = Convert6To8((cdata>>5) & 0x3f);
|
|
|
|
c[2] = Convert5To8((cdata>>11) & 0x1f);
|
|
|
|
c[3] = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_Color5551() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
u8 *c = decoded_ + decFmt.c0off;
|
2012-12-19 23:48:57 +00:00
|
|
|
u16 cdata = *(u16*)(ptr_ + coloff);
|
|
|
|
c[0] = Convert5To8(cdata & 0x1f);
|
|
|
|
c[1] = Convert5To8((cdata>>5) & 0x1f);
|
|
|
|
c[2] = Convert5To8((cdata>>10) & 0x1f);
|
|
|
|
c[3] = (cdata>>15) ? 255 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_Color4444() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
u8 *c = decoded_ + decFmt.c0off;
|
2012-12-19 23:48:57 +00:00
|
|
|
u16 cdata = *(u16*)(ptr_ + coloff);
|
|
|
|
for (int j = 0; j < 4; j++)
|
|
|
|
c[j] = Convert4To8((cdata >> (j * 4)) & 0xF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_Color8888() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
u8 *c = decoded_ + decFmt.c0off;
|
2012-12-19 23:48:57 +00:00
|
|
|
// TODO: speedup
|
2012-12-20 15:03:40 +00:00
|
|
|
const u8 *cdata = (const u8*)(ptr_ + coloff);
|
2012-12-19 23:48:57 +00:00
|
|
|
for (int j = 0; j < 4; j++)
|
|
|
|
c[j] = cdata[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_Color565Morph() const
|
|
|
|
{
|
|
|
|
float col[3] = {0};
|
|
|
|
for (int n = 0; n < morphcount; n++)
|
|
|
|
{
|
|
|
|
float w = gstate_c.morphWeights[n];
|
|
|
|
u16 cdata = *(u16*)(ptr_ + onesize_*n + coloff);
|
|
|
|
col[0] += w * (cdata & 0x1f) / 31.f;
|
|
|
|
col[1] += w * ((cdata>>5) & 0x3f) / 63.f;
|
|
|
|
col[2] += w * ((cdata>>11) & 0x1f) / 31.f;
|
|
|
|
}
|
2012-12-20 15:03:40 +00:00
|
|
|
u8 *c = decoded_ + decFmt.c0off;
|
2012-12-19 23:48:57 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2012-12-20 15:03:40 +00:00
|
|
|
c[i] = (u8)(col[i] * 255.0f);
|
2012-12-19 23:48:57 +00:00
|
|
|
}
|
2012-12-20 15:03:40 +00:00
|
|
|
c[3] = 255;
|
2012-12-19 23:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_Color5551Morph() const
|
|
|
|
{
|
|
|
|
float col[4] = {0};
|
|
|
|
for (int n = 0; n < morphcount; n++)
|
|
|
|
{
|
|
|
|
float w = gstate_c.morphWeights[n];
|
|
|
|
u16 cdata = *(u16*)(ptr_ + onesize_*n + coloff);
|
|
|
|
col[0] += w * (cdata & 0x1f) / 31.f;
|
|
|
|
col[1] += w * ((cdata>>5) & 0x1f) / 31.f;
|
|
|
|
col[2] += w * ((cdata>>10) & 0x1f) / 31.f;
|
2012-12-22 00:57:44 +00:00
|
|
|
col[3] += w * ((cdata>>15) ? 1.0f : 0.0f);
|
2012-12-19 23:48:57 +00:00
|
|
|
}
|
2012-12-20 15:03:40 +00:00
|
|
|
u8 *c = decoded_ + decFmt.c0off;
|
2012-12-19 23:48:57 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2012-12-20 15:03:40 +00:00
|
|
|
c[i] = (u8)(col[i] * 255.0f);
|
2012-12-19 23:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_Color4444Morph() const
|
|
|
|
{
|
|
|
|
float col[4] = {0};
|
|
|
|
for (int n = 0; n < morphcount; n++)
|
|
|
|
{
|
|
|
|
float w = gstate_c.morphWeights[n];
|
|
|
|
u16 cdata = *(u16*)(ptr_ + onesize_*n + coloff);
|
|
|
|
for (int j = 0; j < 4; j++)
|
|
|
|
col[j] += w * ((cdata >> (j * 4)) & 0xF) / 15.f;
|
|
|
|
}
|
2012-12-20 15:03:40 +00:00
|
|
|
u8 *c = decoded_ + decFmt.c0off;
|
2012-12-19 23:48:57 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2012-12-20 15:03:40 +00:00
|
|
|
c[i] = (u8)(col[i] * 255.0f);
|
2012-12-19 23:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_Color8888Morph() const
|
|
|
|
{
|
|
|
|
float col[4] = {0};
|
|
|
|
for (int n = 0; n < morphcount; n++)
|
|
|
|
{
|
|
|
|
float w = gstate_c.morphWeights[n];
|
|
|
|
const u8 *cdata = (const u8*)(ptr_ + onesize_*n + coloff);
|
|
|
|
for (int j = 0; j < 4; j++)
|
|
|
|
col[j] += w * cdata[j];
|
|
|
|
}
|
2012-12-20 15:03:40 +00:00
|
|
|
u8 *c = decoded_ + decFmt.c0off;
|
2012-12-19 23:48:57 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2012-12-20 15:03:40 +00:00
|
|
|
c[i] = (u8)(col[i]);
|
2012-12-19 23:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_NormalS8() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *normal = (float *)(decoded_ + decFmt.nrmoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
float multiplier = 1.0f;
|
|
|
|
if (gstate.reversenormals & 0xFFFFFF)
|
|
|
|
multiplier = -multiplier;
|
|
|
|
const s8 *sv = (const s8*)(ptr_ + nrmoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
2012-12-20 15:03:40 +00:00
|
|
|
normal[j] = (sv[j] / 127.0f) * multiplier;
|
2012-12-19 23:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_NormalS16() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *normal = (float *)(decoded_ + decFmt.nrmoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
float multiplier = 1.0f;
|
|
|
|
if (gstate.reversenormals & 0xFFFFFF)
|
|
|
|
multiplier = -multiplier;
|
|
|
|
const short *sv = (const short*)(ptr_ + nrmoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
2012-12-20 15:03:40 +00:00
|
|
|
normal[j] = (sv[j] / 32767.0f) * multiplier;
|
2012-12-19 23:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_NormalFloat() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *normal = (float *)(decoded_ + decFmt.nrmoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
float multiplier = 1.0f;
|
|
|
|
if (gstate.reversenormals & 0xFFFFFF)
|
|
|
|
multiplier = -multiplier;
|
|
|
|
const float *fv = (const float*)(ptr_ + nrmoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
normal[j] = fv[j] * multiplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_NormalS8Morph() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *normal = (float *)(decoded_ + decFmt.nrmoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
memset(normal, 0, sizeof(float)*3);
|
|
|
|
for (int n = 0; n < morphcount; n++)
|
|
|
|
{
|
|
|
|
float multiplier = gstate_c.morphWeights[n];
|
|
|
|
if (gstate.reversenormals & 0xFFFFFF) {
|
|
|
|
multiplier = -multiplier;
|
|
|
|
}
|
|
|
|
const s8 *sv = (const s8*)(ptr_ + onesize_*n + nrmoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
normal[j] += (sv[j]/32767.0f) * multiplier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_NormalS16Morph() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *normal = (float *)(decoded_ + decFmt.nrmoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
memset(normal, 0, sizeof(float)*3);
|
|
|
|
for (int n = 0; n < morphcount; n++)
|
|
|
|
{
|
|
|
|
float multiplier = gstate_c.morphWeights[n];
|
|
|
|
if (gstate.reversenormals & 0xFFFFFF) {
|
|
|
|
multiplier = -multiplier;
|
|
|
|
}
|
|
|
|
const float *fv = (const float*)(ptr_ + onesize_*n + nrmoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
normal[j] += fv[j] * multiplier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_NormalFloatMorph() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *normal = (float *)(decoded_ + decFmt.nrmoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
memset(normal, 0, sizeof(float)*3);
|
|
|
|
for (int n = 0; n < morphcount; n++)
|
|
|
|
{
|
|
|
|
float multiplier = gstate_c.morphWeights[n];
|
|
|
|
if (gstate.reversenormals & 0xFFFFFF) {
|
|
|
|
multiplier = -multiplier;
|
|
|
|
}
|
|
|
|
const float *fv = (const float*)(ptr_ + onesize_*n + nrmoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
normal[j] += fv[j] * multiplier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_PosS8() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *v = (float *)(decoded_ + decFmt.posoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
float multiplier = 1.0f / 127.0f;
|
|
|
|
const s8 *sv = (const s8*)(ptr_ + posoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
v[j] = sv[j] * multiplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_PosS16() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *v = (float *)(decoded_ + decFmt.posoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
float multiplier = 1.0f / 32767.0f;
|
|
|
|
const short *sv = (const short*)(ptr_ + posoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
v[j] = sv[j] * multiplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_PosFloat() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *v = (float *)(decoded_ + decFmt.posoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
const float *fv = (const float*)(ptr_ + posoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
v[j] = fv[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_PosS8Through() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *v = (float *)(decoded_ + decFmt.posoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
const s8 *sv = (const s8*)(ptr_ + posoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
v[j] = sv[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_PosS16Through() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *v = (float *)(decoded_ + decFmt.posoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
const short *sv = (const short*)(ptr_ + posoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
v[j] = sv[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_PosFloatThrough() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *v = (float *)(decoded_ + decFmt.posoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
const float *fv = (const float*)(ptr_ + posoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
v[j] = fv[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_PosS8Morph() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *v = (float *)(decoded_ + decFmt.posoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
memset(v, 0, sizeof(float) * 3);
|
|
|
|
for (int n = 0; n < morphcount; n++) {
|
|
|
|
const s8 *sv = (const s8*)(ptr_ + onesize_*n + posoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
v[j] += (sv[j] / 127.f) * gstate_c.morphWeights[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_PosS16Morph() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *v = (float *)(decoded_ + decFmt.posoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
memset(v, 0, sizeof(float) * 3);
|
|
|
|
for (int n = 0; n < morphcount; n++) {
|
|
|
|
float multiplier = 1.0f / 32767.0f;
|
|
|
|
const short *sv = (const short*)(ptr_ + onesize_*n + posoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
v[j] += (sv[j] * multiplier) * gstate_c.morphWeights[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexDecoder::Step_PosFloatMorph() const
|
|
|
|
{
|
2012-12-20 15:03:40 +00:00
|
|
|
float *v = (float *)(decoded_ + decFmt.posoff);
|
2012-12-19 23:48:57 +00:00
|
|
|
memset(v, 0, sizeof(float) * 3);
|
|
|
|
for (int n = 0; n < morphcount; n++) {
|
|
|
|
const float *fv = (const float*)(ptr_ + onesize_*n + posoff);
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
v[j] += fv[j] * gstate_c.morphWeights[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const StepFunction wtstep[4] = {
|
|
|
|
0,
|
|
|
|
&VertexDecoder::Step_WeightsU8,
|
|
|
|
&VertexDecoder::Step_WeightsU16,
|
|
|
|
&VertexDecoder::Step_WeightsFloat,
|
|
|
|
};
|
|
|
|
|
|
|
|
const StepFunction tcstep[4] = {
|
|
|
|
0,
|
|
|
|
&VertexDecoder::Step_TcU8,
|
|
|
|
&VertexDecoder::Step_TcU16,
|
|
|
|
&VertexDecoder::Step_TcFloat,
|
|
|
|
};
|
|
|
|
|
|
|
|
const StepFunction tcstep_through[4] = {
|
|
|
|
0,
|
|
|
|
&VertexDecoder::Step_TcU8,
|
|
|
|
&VertexDecoder::Step_TcU16Through,
|
|
|
|
&VertexDecoder::Step_TcFloatThrough,
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Tc Morph
|
|
|
|
|
|
|
|
const StepFunction colstep[8] = {
|
|
|
|
0, 0, 0, 0,
|
|
|
|
&VertexDecoder::Step_Color565,
|
|
|
|
&VertexDecoder::Step_Color5551,
|
|
|
|
&VertexDecoder::Step_Color4444,
|
|
|
|
&VertexDecoder::Step_Color8888,
|
|
|
|
};
|
|
|
|
|
|
|
|
const StepFunction colstep_morph[8] = {
|
|
|
|
0, 0, 0, 0,
|
|
|
|
&VertexDecoder::Step_Color565Morph,
|
|
|
|
&VertexDecoder::Step_Color5551Morph,
|
|
|
|
&VertexDecoder::Step_Color4444Morph,
|
|
|
|
&VertexDecoder::Step_Color8888Morph,
|
|
|
|
};
|
|
|
|
|
|
|
|
const StepFunction nrmstep[4] = {
|
|
|
|
0,
|
|
|
|
&VertexDecoder::Step_NormalS8,
|
|
|
|
&VertexDecoder::Step_NormalS16,
|
|
|
|
&VertexDecoder::Step_NormalFloat,
|
|
|
|
};
|
|
|
|
|
|
|
|
const StepFunction nrmstep_morph[4] = {
|
|
|
|
0,
|
|
|
|
&VertexDecoder::Step_NormalS8Morph,
|
|
|
|
&VertexDecoder::Step_NormalS16Morph,
|
|
|
|
&VertexDecoder::Step_NormalFloatMorph,
|
|
|
|
};
|
|
|
|
|
|
|
|
const StepFunction posstep[4] = {
|
|
|
|
0,
|
|
|
|
&VertexDecoder::Step_PosS8,
|
|
|
|
&VertexDecoder::Step_PosS16,
|
|
|
|
&VertexDecoder::Step_PosFloat,
|
|
|
|
};
|
|
|
|
|
|
|
|
const StepFunction posstep_morph[4] = {
|
|
|
|
0,
|
|
|
|
&VertexDecoder::Step_PosS8Morph,
|
|
|
|
&VertexDecoder::Step_PosS16Morph,
|
|
|
|
&VertexDecoder::Step_PosFloatMorph,
|
|
|
|
};
|
|
|
|
|
|
|
|
const StepFunction posstep_through[4] = {
|
|
|
|
0,
|
|
|
|
&VertexDecoder::Step_PosS8Through,
|
|
|
|
&VertexDecoder::Step_PosS16Through,
|
|
|
|
&VertexDecoder::Step_PosFloatThrough,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
void VertexDecoder::SetVertexType(u32 fmt) {
|
2012-12-19 19:21:59 +00:00
|
|
|
fmt_ = fmt;
|
2012-11-01 15:19:01 +00:00
|
|
|
throughmode = (fmt & GE_VTYPE_THROUGH) != 0;
|
2012-12-19 23:48:57 +00:00
|
|
|
numSteps_ = 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
int biggest = 0;
|
|
|
|
size = 0;
|
2012-12-05 03:45:28 +00:00
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
tc = fmt & 0x3;
|
|
|
|
col = (fmt >> 2) & 0x7;
|
|
|
|
nrm = (fmt >> 5) & 0x3;
|
|
|
|
pos = (fmt >> 7) & 0x3;
|
2012-11-01 15:19:01 +00:00
|
|
|
weighttype = (fmt >> 9) & 0x3;
|
2012-12-19 17:35:37 +00:00
|
|
|
idx = (fmt >> 11) & 0x3;
|
2012-11-01 15:19:01 +00:00
|
|
|
morphcount = ((fmt >> 18) & 0x7)+1;
|
2012-12-19 17:35:37 +00:00
|
|
|
nweights = ((fmt >> 14) & 0x7)+1;
|
|
|
|
|
|
|
|
int decOff = 0;
|
|
|
|
memset(&decFmt, 0, sizeof(decFmt));
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
DEBUG_LOG(G3D,"VTYPE: THRU=%i TC=%i COL=%i POS=%i NRM=%i WT=%i NW=%i IDX=%i MC=%i", (int)throughmode, tc,col,pos,nrm,weighttype,nweights,idx,morphcount);
|
2012-12-05 03:45:28 +00:00
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
if (weighttype) { // && nweights?
|
2012-11-01 15:19:01 +00:00
|
|
|
//size = align(size, wtalign[weighttype]); unnecessary
|
|
|
|
size += wtsize[weighttype] * nweights;
|
|
|
|
if (wtalign[weighttype] > biggest)
|
|
|
|
biggest = wtalign[weighttype];
|
2012-12-19 17:35:37 +00:00
|
|
|
|
2012-12-19 23:48:57 +00:00
|
|
|
steps_[numSteps_++] = wtstep[weighttype];
|
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
if (nweights < 5) {
|
|
|
|
decFmt.w0off = decOff;
|
|
|
|
decFmt.w0fmt = DEC_FLOAT_1 + nweights - 1;
|
|
|
|
} else {
|
|
|
|
decFmt.w0off = decOff;
|
|
|
|
decFmt.w0fmt = DEC_FLOAT_4;
|
|
|
|
decFmt.w1off = decOff + 4 * 4;
|
|
|
|
decFmt.w1fmt = DEC_FLOAT_1 + nweights - 5;
|
|
|
|
}
|
|
|
|
decOff += nweights * 4;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
if (tc) {
|
2012-11-01 15:19:01 +00:00
|
|
|
size = align(size, tcalign[tc]);
|
|
|
|
tcoff = size;
|
|
|
|
size += tcsize[tc];
|
2012-11-16 14:16:14 +00:00
|
|
|
if (tcalign[tc] > biggest)
|
|
|
|
biggest = tcalign[tc];
|
2012-12-19 17:35:37 +00:00
|
|
|
|
2012-12-19 23:48:57 +00:00
|
|
|
steps_[numSteps_++] = throughmode ? tcstep_through[tc] : tcstep[tc];
|
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
// All UV decode to DEC_FLOAT2 currently.
|
|
|
|
decFmt.uvfmt = DEC_FLOAT_2;
|
|
|
|
decFmt.uvoff = decOff;
|
|
|
|
decOff += DecFmtSize(decFmt.uvfmt);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
if (col) {
|
2012-11-16 14:16:14 +00:00
|
|
|
size = align(size, colalign[col]);
|
2012-11-01 15:19:01 +00:00
|
|
|
coloff = size;
|
|
|
|
size += colsize[col];
|
|
|
|
if (colalign[col] > biggest)
|
|
|
|
biggest = colalign[col];
|
2012-12-19 17:35:37 +00:00
|
|
|
|
2012-12-19 23:48:57 +00:00
|
|
|
steps_[numSteps_++] = morphcount == 1 ? colstep[col] : colstep_morph[col];
|
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
// All color formats decode to DEC_U8_4 currently.
|
|
|
|
// They can become floats later during transform though.
|
|
|
|
decFmt.c0fmt = DEC_U8_4;
|
|
|
|
decFmt.c0off = decOff;
|
|
|
|
decOff += DecFmtSize(decFmt.c0fmt);
|
|
|
|
} else {
|
2012-11-01 15:19:01 +00:00
|
|
|
coloff = 0;
|
|
|
|
}
|
2012-11-16 14:16:14 +00:00
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
if (nrm) {
|
2012-11-16 14:16:14 +00:00
|
|
|
size = align(size, nrmalign[nrm]);
|
2012-11-01 15:19:01 +00:00
|
|
|
nrmoff = size;
|
|
|
|
size += nrmsize[nrm];
|
|
|
|
if (nrmalign[nrm] > biggest)
|
|
|
|
biggest = nrmalign[nrm];
|
2012-12-19 17:35:37 +00:00
|
|
|
|
2012-12-19 23:48:57 +00:00
|
|
|
steps_[numSteps_++] = morphcount == 1 ? nrmstep[nrm] : nrmstep_morph[nrm];
|
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
// The normal formats match the gl formats perfectly, let's use 'em.
|
|
|
|
switch (nrm) {
|
|
|
|
case GE_VTYPE_NRM_8BIT >> GE_VTYPE_NRM_SHIFT: decFmt.nrmfmt = DEC_S8_3; break;
|
|
|
|
case GE_VTYPE_NRM_16BIT >> GE_VTYPE_NRM_SHIFT: decFmt.nrmfmt = DEC_S16_3; break;
|
|
|
|
case GE_VTYPE_NRM_FLOAT >> GE_VTYPE_NRM_SHIFT: decFmt.nrmfmt = DEC_FLOAT_3; break;
|
|
|
|
}
|
2012-12-19 23:48:57 +00:00
|
|
|
|
2012-12-19 19:21:59 +00:00
|
|
|
// Actually, temporarily let's not.
|
|
|
|
decFmt.nrmfmt = DEC_FLOAT_3;
|
2012-12-19 17:35:37 +00:00
|
|
|
decFmt.nrmoff = decOff;
|
|
|
|
decOff += DecFmtSize(decFmt.nrmfmt);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-16 14:16:14 +00:00
|
|
|
//if (pos) - there's always a position
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-11-16 14:16:14 +00:00
|
|
|
size = align(size, posalign[pos]);
|
2012-11-01 15:19:01 +00:00
|
|
|
posoff = size;
|
|
|
|
size += possize[pos];
|
|
|
|
if (posalign[pos] > biggest)
|
|
|
|
biggest = posalign[pos];
|
2012-12-19 17:35:37 +00:00
|
|
|
|
|
|
|
if (throughmode) {
|
2012-12-19 23:48:57 +00:00
|
|
|
steps_[numSteps_++] = posstep_through[pos];
|
2012-12-19 17:35:37 +00:00
|
|
|
decFmt.posfmt = DEC_FLOAT_3;
|
|
|
|
} else {
|
2012-12-19 23:48:57 +00:00
|
|
|
steps_[numSteps_++] = morphcount == 1 ? posstep[pos] : posstep_morph[pos];
|
|
|
|
|
2012-12-19 17:35:37 +00:00
|
|
|
// The non-through-mode position formats match the gl formats perfectly, let's use 'em.
|
|
|
|
switch (pos) {
|
|
|
|
case GE_VTYPE_POS_8BIT >> GE_VTYPE_POS_SHIFT: decFmt.posfmt = DEC_S8_3; break;
|
|
|
|
case GE_VTYPE_POS_16BIT >> GE_VTYPE_POS_SHIFT: decFmt.posfmt = DEC_S16_3; break;
|
|
|
|
case GE_VTYPE_POS_FLOAT >> GE_VTYPE_POS_SHIFT: decFmt.posfmt = DEC_FLOAT_3; break;
|
|
|
|
}
|
2012-12-19 19:21:59 +00:00
|
|
|
// Actually, temporarily let's not.
|
|
|
|
decFmt.posfmt = DEC_FLOAT_3;
|
2012-12-19 17:35:37 +00:00
|
|
|
}
|
|
|
|
decFmt.posoff = decOff;
|
|
|
|
decOff += DecFmtSize(decFmt.posfmt);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2012-12-19 19:21:59 +00:00
|
|
|
decFmt.stride = decOff;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-11-16 14:16:14 +00:00
|
|
|
size = align(size, biggest);
|
|
|
|
onesize_ = size;
|
2012-11-01 15:19:01 +00:00
|
|
|
size *= morphcount;
|
|
|
|
DEBUG_LOG(G3D,"SVT : size = %i, aligned to biggest %i", size, biggest);
|
|
|
|
}
|
|
|
|
|
2013-01-04 08:54:19 +00:00
|
|
|
void VertexDecoder::DecodeVerts(u8 *decodedptr, const void *verts, const void *inds, int prim, int count, int *indexLowerBound, int *indexUpperBound) const {
|
2012-11-16 14:16:14 +00:00
|
|
|
// Find index bounds. Could cache this in display lists.
|
2012-12-21 11:57:43 +00:00
|
|
|
// Also, this could be greatly sped up with SSE2, although rarely a bottleneck.
|
2012-11-16 14:16:14 +00:00
|
|
|
int lowerBound = 0x7FFFFFFF;
|
|
|
|
int upperBound = 0;
|
|
|
|
if (idx == (GE_VTYPE_IDX_8BIT >> GE_VTYPE_IDX_SHIFT)) {
|
|
|
|
const u8 *ind8 = (const u8 *)inds;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (ind8[i] < lowerBound)
|
|
|
|
lowerBound = ind8[i];
|
|
|
|
if (ind8[i] > upperBound)
|
|
|
|
upperBound = ind8[i];
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2012-11-16 14:16:14 +00:00
|
|
|
} else if (idx == (GE_VTYPE_IDX_16BIT >> GE_VTYPE_IDX_SHIFT)) {
|
|
|
|
const u16 *ind16 = (const u16*)inds;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (ind16[i] < lowerBound)
|
|
|
|
lowerBound = ind16[i];
|
|
|
|
if (ind16[i] > upperBound)
|
|
|
|
upperBound = ind16[i];
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2012-11-16 14:16:14 +00:00
|
|
|
} else {
|
|
|
|
lowerBound = 0;
|
|
|
|
upperBound = count - 1;
|
|
|
|
}
|
2012-11-16 15:19:37 +00:00
|
|
|
*indexLowerBound = lowerBound;
|
|
|
|
*indexUpperBound = upperBound;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-12-19 23:48:57 +00:00
|
|
|
// Decode the vertices within the found bounds, once each
|
2012-12-21 17:46:15 +00:00
|
|
|
decoded_ = decodedptr; // + lowerBound * decFmt.stride;
|
2012-12-19 23:48:57 +00:00
|
|
|
ptr_ = (const u8*)verts + lowerBound * size;
|
2013-01-04 08:54:19 +00:00
|
|
|
for (int index = lowerBound; index <= upperBound; index++) {
|
2012-12-19 23:48:57 +00:00
|
|
|
for (int i = 0; i < numSteps_; i++) {
|
|
|
|
((*this).*steps_[i])();
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2012-12-19 23:48:57 +00:00
|
|
|
ptr_ += size;
|
2012-12-20 15:03:40 +00:00
|
|
|
decoded_ += decFmt.stride;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-04 08:54:19 +00:00
|
|
|
|
|
|
|
// TODO: Does not support morphs, skinning etc.
|
|
|
|
u32 VertexDecoder::InjectUVs(u8 *decoded, const void *verts, float *customuv, int count) const {
|
|
|
|
u32 customVertType = (gstate.vertType & ~GE_VTYPE_TC_MASK) | GE_VTYPE_TC_FLOAT;
|
|
|
|
VertexDecoder decOut;
|
|
|
|
decOut.SetVertexType(customVertType);
|
|
|
|
|
|
|
|
const u8 *inp = (const u8 *)verts;
|
|
|
|
u8 *out = decoded;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (pos) memcpy(out + decOut.posoff, inp + posoff, possize[pos]);
|
|
|
|
if (nrm) memcpy(out + decOut.nrmoff, inp + nrmoff, nrmsize[pos]);
|
|
|
|
if (col) memcpy(out + decOut.coloff, inp + coloff, colsize[pos]);
|
|
|
|
// Ignore others for now, this is all we need for puzbob.
|
|
|
|
// Inject!
|
|
|
|
memcpy(out + decOut.tcoff, &customuv[i * 2], tcsize[decOut.tc]);
|
|
|
|
inp += this->onesize_;
|
|
|
|
out += decOut.onesize_;
|
|
|
|
}
|
|
|
|
return customVertType;
|
|
|
|
}
|