isle-portable/LEGO1/tgl/d3drm/texture.cpp
Mark Langen 260772e374
Bootstrap decomp of Tgl rendering library (#293)
* Bootstrap decomp of D3DRM rendering code

* This PR kicks off work on decompiling the D3D Retained Mode (D3DRM)
  rendering part of the codebase.

* High level overview:

* There is a base IMxDirect3DRMObject class which all of the D3DRM
  rendering objects inherit from. Its only virtual method is one to get
  the underlying object handle.

* A hierarchy of abstract classes inherits from this base class, which
  I've called "IMxDirect3DRM<class>". These classes only have pure
  virtual methods on them and don't contain any data.

* Each one of the abstract classes has exactly one concrete
  implementation, which I've called "MxDirect3DRM<class>". These classes
  have exactly one piece of data, which is a pointer to the underlying
  D3D Retained Mode object.

* If the classes need to store additional data, they store it in a
  userdata blob which is attached to the D3DRM object rather than the
  additional data being stored in the class itself.

* I've worked out about twice this many classes related to D3DRM
  rendering so far but the PR was getting large enough as is, so I'm
  cutting it here for now.

* I decomped sufficiently many methods of these classe to convince
  myself that the above observations are correct. About 60% of the
  decomped methods here are perfect matches, including at least one
  non-trivial method per class.

* Formatting

* Restructure changes using Tgl naming / details

* Restructure the changes to use the naming that we know from Tgl.

* Fill in some parts of the implementation I couldn't initially figure
  out using the details from Tgl (got more 100% matches).

* Move d3drm link requirement

* Fixups FloatMatrix -> FloatMatrix4

* Fix order

* Full fix for ordering problems

* Put back accidentally removed include.

* Fix call which should have been Release

* Use new and delete for DeepClone

* Missing Tgl:: on CreateRenderer

* Revert change to bool return value.

* Rename Something -> Unk

* Return paramter naming convention to what Tgl used

* Add scalar ddtor to verify inline destructor

* Fix order

* Change malloc/free -> new/delete in Tgl

* Remove duplicate destructor.

* Check all inline destructors

* Fix dtor comments

* Third time's the charm

* Alphabetical sort

* Decomp adjustments

* Add d3drm files to clang-format

---------

Co-authored-by: Christian Semmler <mail@csemmler.com>
2023-12-07 13:10:42 +01:00

196 lines
5.2 KiB
C++

#include "impl.h"
using namespace TglImpl;
DECOMP_SIZE_ASSERT(TglD3DRMIMAGE, 0x40);
inline TglD3DRMIMAGE* TextureGetImage(IDirect3DRMTexture* pTexture)
{
return reinterpret_cast<TglD3DRMIMAGE*>(pTexture->GetAppData());
}
// Forward declare to satisfy order check
void TextureDestroyCallback(IDirect3DRMObject* pObject, void* pArg);
// FUNCTION: LEGO1 0x100a12a0
Result TextureImpl::SetImage(IDirect3DRMTexture* pSelf, TglD3DRMIMAGE* pImage)
{
unsigned long appData;
Result result;
appData = reinterpret_cast<unsigned long>(pImage);
// This is here because in the original code they asserted
// on the return value being NULL.
TextureGetImage(pSelf);
result = ResultVal(pSelf->SetAppData(appData));
if (Succeeded(result) && pImage) {
result = ResultVal(pSelf->AddDestroyCallback(TextureDestroyCallback, NULL));
if (!Succeeded(result)) {
pSelf->SetAppData(0);
}
}
return result;
}
// FUNCTION: LEGO1 0x100a1300
void TextureDestroyCallback(IDirect3DRMObject* pObject, void* pArg)
{
TglD3DRMIMAGE* pImage = reinterpret_cast<TglD3DRMIMAGE*>(pObject->GetAppData());
delete pImage;
pObject->SetAppData(0);
}
// FUNCTION: LEGO1 0x100a1330
TglD3DRMIMAGE::TglD3DRMIMAGE(
int width,
int height,
int depth,
void* pBuffer,
int useBuffer,
int paletteSize,
PaletteEntry* pEntries
)
{
m_image.aspectx = 1;
m_image.aspecty = 1;
m_image.width = 0;
m_image.height = 0;
m_image.depth = 0;
m_image.rgb = 0;
m_image.bytes_per_line = 0;
m_image.buffer1 = NULL;
m_image.buffer2 = NULL;
m_image.red_mask = 0xFF;
m_image.green_mask = 0xFF;
m_image.blue_mask = 0xFF;
m_image.alpha_mask = 0xFF;
m_image.palette_size = 0;
m_image.palette = NULL;
m_texelsAllocatedByClient = 0;
if (pBuffer != NULL) {
CreateBuffer(width, height, depth, pBuffer, useBuffer);
}
if (pEntries != NULL) {
InitializePalette(paletteSize, pEntries);
}
}
// FUNCTION: LEGO1 0x100a13b0
void TglD3DRMIMAGE::Destroy()
{
if (m_texelsAllocatedByClient == 0) {
delete m_image.buffer1;
}
delete m_image.palette;
}
// FUNCTION: LEGO1 0x100a13e0 STUB
Result TglD3DRMIMAGE::CreateBuffer(int width, int height, int depth, void* pBuffer, int useBuffer)
{
return Error;
}
// FUNCTION: LEGO1 0x100a1510
void TglD3DRMIMAGE::FillRowsOfTexture(int y, int height, char* pContent)
{
// The purpose is clearly this but I can't get the assembly to line up.
memcpy((char*) m_image.buffer1 + (y * m_image.bytes_per_line), pContent, height * m_image.bytes_per_line);
}
// FUNCTION: LEGO1 0x100a1550
Result TglD3DRMIMAGE::InitializePalette(int paletteSize, PaletteEntry* pEntries)
{
// This function is a 100% match if the PaletteEntry class is copied
// into into the TglD3DRMIMAGE class instead of being a global struct.
if (m_image.palette_size != paletteSize) {
if (m_image.palette != NULL) {
delete m_image.palette;
m_image.palette = NULL;
m_image.palette_size = 0;
}
if (paletteSize > 0) {
m_image.palette = new D3DRMPALETTEENTRY[paletteSize];
m_image.palette_size = paletteSize;
}
}
if (paletteSize > 0) {
for (int i = 0; i < paletteSize; i++) {
m_image.palette[i].red = pEntries[i].m_red;
m_image.palette[i].green = pEntries[i].m_green;
m_image.palette[i].blue = pEntries[i].m_blue;
m_image.palette[i].flags = D3DRMPALETTE_READONLY;
}
}
return Success;
}
// SYNTHETIC: LEGO1 0x100a2800
// TglImpl::TextureImpl::`scalar deleting destructor'
// FUNCTION: LEGO1 0x100a3c10
Result TextureImpl::SetTexels(int width, int height, int bitsPerTexel, void* pTexels)
{
TglD3DRMIMAGE* image = TextureGetImage(m_data);
Result result = image->CreateBuffer(width, height, bitsPerTexel, pTexels, TRUE);
if (Succeeded(result)) {
result = ResultVal(m_data->Changed(TRUE, FALSE));
}
return result;
}
// FUNCTION: LEGO1 0x100a3c60
void TextureImpl::FillRowsOfTexture(int y, int height, void* pBuffer)
{
TglD3DRMIMAGE* image = TextureGetImage(m_data);
image->FillRowsOfTexture(y, height, (char*) pBuffer);
}
// FUNCTION: LEGO1 0x100a3c90
Result TextureImpl::Changed(int texelsChanged, int paletteChanged)
{
return ResultVal(m_data->Changed(texelsChanged, paletteChanged));
}
// FUNCTION: LEGO1 0x100a3d00
Result TextureImpl::GetBufferAndPalette(
int* width,
int* height,
int* depth,
void** pBuffer,
int* paletteSize,
PaletteEntry** pEntries
)
{
// Something really doesn't match here, not sure what's up.
TglD3DRMIMAGE* image = TextureGetImage(m_data);
*width = image->m_image.width;
*height = image->m_image.height;
*depth = image->m_image.depth;
*pBuffer = image->m_image.buffer1;
*paletteSize = image->m_image.palette_size;
for (int i = 0; i < image->m_image.palette_size; i++) {
pEntries[i]->m_red = image->m_image.palette[i].red;
pEntries[i]->m_green = image->m_image.palette[i].green;
pEntries[i]->m_blue = image->m_image.palette[i].blue;
}
return Success;
}
// FUNCTION: LEGO1 0x100a3d40
Result TextureImpl::SetPalette(int entryCount, PaletteEntry* pEntries)
{
// Not 100% confident this is supposed to directly be forwarding arguments,
// but it probably is given FillRowsOfTexture matches doing that.
TglD3DRMIMAGE* image = TextureGetImage(m_data);
image->InitializePalette(entryCount, pEntries);
m_data->Changed(FALSE, TRUE);
return Success;
}
// FUNCTION: LEGO1 0x100a3d70
void* TextureImpl::ImplementationDataPtr()
{
return reinterpret_cast<void*>(&m_data);
}