[PICA] We now know how to convert attributes to f32

This commit is contained in:
wheremyfoodat 2022-09-22 05:08:01 +03:00
parent dcad7846eb
commit 93e5c268e8
2 changed files with 35 additions and 24 deletions

View file

@ -23,13 +23,13 @@ namespace Floats {
template <unsigned M, unsigned E>
struct Float {
public:
static Float<M, E> FromFloat32(float val) {
static Float<M, E> fromFloat32(float val) {
Float<M, E> ret;
ret.value = val;
return ret;
}
static Float<M, E> FromRaw(u32 hex) {
static Float<M, E> fromRaw(u32 hex) {
Float<M, E> res;
const int width = M + E + 1;
@ -54,34 +54,34 @@ namespace Floats {
return res;
}
static Float<M, E> Zero() {
return FromFloat32(0.f);
static Float<M, E> zero() {
return fromFloat32(0.f);
}
// Not recommended for anything but logging
float ToFloat32() const {
float toFloat32() const {
return value;
}
Float<M, E> operator*(const Float<M, E>& flt) const {
float result = value * flt.ToFloat32();
float result = value * flt.toFloat32();
// PICA gives 0 instead of NaN when multiplying by inf
if (std::isnan(result))
if (!std::isnan(value) && !std::isnan(flt.ToFloat32()))
if (!std::isnan(value) && !std::isnan(flt.toFloat32()))
result = 0.f;
return Float<M, E>::FromFloat32(result);
return Float<M, E>::fromFloat32(result);
}
Float<M, E> operator/(const Float<M, E>& flt) const {
return Float<M, E>::FromFloat32(ToFloat32() / flt.ToFloat32());
return Float<M, E>::fromFloat32(toFloat32() / flt.toFloat32());
}
Float<M, E> operator+(const Float<M, E>& flt) const {
return Float<M, E>::FromFloat32(ToFloat32() + flt.ToFloat32());
return Float<M, E>::fromFloat32(toFloat32() + flt.toFloat32());
}
Float<M, E> operator-(const Float<M, E>& flt) const {
return Float<M, E>::FromFloat32(ToFloat32() - flt.ToFloat32());
return Float<M, E>::fromFloat32(toFloat32() - flt.toFloat32());
}
Float<M, E>& operator*=(const Float<M, E>& flt) {
@ -90,46 +90,46 @@ namespace Floats {
}
Float<M, E>& operator/=(const Float<M, E>& flt) {
value /= flt.ToFloat32();
value /= flt.toFloat32();
return *this;
}
Float<M, E>& operator+=(const Float<M, E>& flt) {
value += flt.ToFloat32();
value += flt.toFloat32();
return *this;
}
Float<M, E>& operator-=(const Float<M, E>& flt) {
value -= flt.ToFloat32();
value -= flt.toFloat32();
return *this;
}
Float<M, E> operator-() const {
return Float<M, E>::FromFloat32(-ToFloat32());
return Float<M, E>::fromFloat32(-toFloat32());
}
bool operator<(const Float<M, E>& flt) const {
return ToFloat32() < flt.ToFloat32();
return toFloat32() < flt.toFloat32();
}
bool operator>(const Float<M, E>& flt) const {
return ToFloat32() > flt.ToFloat32();
return toFloat32() > flt.toFloat32();
}
bool operator>=(const Float<M, E>& flt) const {
return ToFloat32() >= flt.ToFloat32();
return toFloat32() >= flt.toFloat32();
}
bool operator<=(const Float<M, E>& flt) const {
return ToFloat32() <= flt.ToFloat32();
return toFloat32() <= flt.toFloat32();
}
bool operator==(const Float<M, E>& flt) const {
return ToFloat32() == flt.ToFloat32();
return toFloat32() == flt.toFloat32();
}
bool operator!=(const Float<M, E>& flt) const {
return ToFloat32() != flt.ToFloat32();
return toFloat32() != flt.toFloat32();
}
private:
@ -142,8 +142,8 @@ namespace Floats {
float value;
};
using float24 = Float<16, 7>;
using float20 = Float<12, 7>;
using float16 = Float<10, 5>;
using f24 = Float<16, 7>;
using f20 = Float<12, 7>;
using f16 = Float<10, 5>;
} // namespace Pica

View file

@ -1,7 +1,10 @@
#include "PICA/gpu.hpp"
#include "PICA/float_types.hpp"
#include "PICA/regs.hpp"
#include <cstdio>
using namespace Floats;
void GPU::reset() {
regs.fill(0);
shaderUnit.reset();
@ -16,5 +19,13 @@ void GPU::drawArrays() {
const u32 vertexCount = regs[PICAInternalRegs::VertexCountReg];
const u32 vertexOffset = regs[PICAInternalRegs::VertexOffsetReg];
u32* attrBuffer = &regs[0x233];
auto a = f24::fromRaw(attrBuffer[0] >> 8);
auto b = f24::fromRaw(((attrBuffer[0] & 0xFF) << 16) | ((attrBuffer[1] >> 16) & 0xFFFF));
auto g = f24::fromRaw(((attrBuffer[1] & 0xFFFF) << 8) | ((attrBuffer[2] >> 24) & 0xFF));
auto r = f24::fromRaw(attrBuffer[2] & 0xFFFFFF);
printf("PICA::DrawArrays(vertex count = %d, vertexOffset = %d)\n", vertexCount, vertexOffset);
printf("(r: %f, g: %f, b: %f, a: %f)\n", r.toFloat32(), g.toFloat32(), b.toFloat32(), a.toFloat32());
}