FFL
Loading...
Searching...
No Matches
FFLiCompressorImpl.h
Go to the documentation of this file.
1#ifndef FFLI_COMPRESSOR_IMPL_H_
2#define FFLI_COMPRESSOR_IMPL_H_
3
4#include <nn/ffl/FFLiCompressShader.h>
5#include <nn/ffl/FFLiContextState.h>
6#include <nn/ffl/FFLiInvalidate.h>
7#include <nn/ffl/FFLiTemp.h>
8#include <nn/ffl/FFLiTexture.h>
9
10#include <nn/ffl/detail/FFLiBufferAllocator.h>
11#include <nn/ffl/detail/FFLiCompressDrawer.h>
12#include <nn/ffl/detail/FFLiCompressorDesc.h>
13#include <nn/ffl/detail/FFLiCompressorParam.h>
14
15#include <cafe/gx2.h>
16
17template <typename T>
19{
20public:
21 FFLiCompressorImpl(FFLiBufferAllocator* pAllocator, const void* pShaderData, u32 shaderIndex);
22 ~FFLiCompressorImpl(); // Deleted in NSMBU
23
24 static u32 GetBufferSize(const void* pShaderData, u32 shaderIndex);
25 static u32 GetTextureBufferSize(u32 width, u32 height, u32 numMips);
26
27 bool SetupCPU(const void* pShaderData, u32 shaderIndex);
28 void SetupGPU();
29
30 bool UseUB() const;
31
32 void CompressImpl(GX2Texture* pDst, const GX2Texture* pSrc, const FFLiCompressorParam* pParam);
33
34private:
40};
41
44
45NN_STATIC_ASSERT(sizeof(FFLiCompressorImplBC1) == 0xF8);
46NN_STATIC_ASSERT(sizeof(FFLiCompressorImplBC3) == 0xF8);
47
48template <typename T>
49FFLiCompressorImpl<T>::FFLiCompressorImpl(FFLiBufferAllocator* pAllocator, const void* pShaderData, u32 shaderIndex)
55{
56 u32 bufferSize = GetBufferSize(pShaderData, shaderIndex);
57 void* pBuffer = pAllocator->Allocate(bufferSize);
58
59 m_BufferAllocator.Init(pBuffer, bufferSize);
60}
61
62template <typename T>
66
67template <typename T>
68u32 FFLiCompressorImpl<T>::GetBufferSize(const void* pShaderData, u32 shaderIndex)
69{
70 u32 bufferSize = FFLiContextState::GetBufferSize();
71 bufferSize += FFLiCompressShader::GetBufferSize(pShaderData, shaderIndex);
72 bufferSize += FFLiCompressDrawer::GetBufferSize();
73
74 return bufferSize;
75}
76
77template <typename T>
78u32 FFLiCompressorImpl<T>::GetTextureBufferSize(u32 width, u32 height, u32 numMips)
79{
80 GX2Texture texture;
81 GX2InitTexture(&texture, width, height, 1, numMips, T::FORMAT, GX2_SURFACE_DIM_2D);
82 return texture.surface.alignment * 2 + texture.surface.imageSize + texture.surface.mipSize;
83}
84
85template <typename T>
86bool FFLiCompressorImpl<T>::SetupCPU(const void* pShaderData, u32 shaderIndex)
87{
88 m_ContextState.Initialize(&m_BufferAllocator);
89
90 if (!m_CompressShader.SetupCPU(pShaderData, shaderIndex, &m_BufferAllocator))
91 return false;
92
93 m_CompressDrawer.SetupCPU(&m_BufferAllocator);
94 return true;
95}
96
97template <typename T>
99{
100 m_CompressShader.SetupGPU();
102 m_ContextState.Setup();
103 m_ContextState.Bind();
104 FFLiTempSetShaderMode(UseUB() ? GX2_SHADER_MODE_UNIFORM_BLOCK : GX2_SHADER_MODE_UNIFORM_REGISTER);
105 GX2SetTargetChannelMasks(
106 GX2_CHANNEL_MASK_RGBA,
107 GX2_CHANNEL_MASK_NONE,
108 GX2_CHANNEL_MASK_NONE,
109 GX2_CHANNEL_MASK_NONE,
110 GX2_CHANNEL_MASK_NONE,
111 GX2_CHANNEL_MASK_NONE,
112 GX2_CHANNEL_MASK_NONE,
113 GX2_CHANNEL_MASK_NONE
114 );
115 GX2SetAlphaTest(GX2_DISABLE, GX2_COMPARE_ALWAYS, 0.0f);
116 GX2SetColorControl(GX2_LOGIC_OP_NONE, 0xFF, GX2_DISABLE, GX2_ENABLE);
117 GX2SetDepthOnlyControl(GX2_DISABLE, GX2_DISABLE, GX2_COMPARE_LESS);
118 GX2SetCullOnlyControl(GX2_FRONT_FACE_CCW, GX2_DISABLE, GX2_ENABLE);
119 GX2SetBlendControl(
120 GX2_RENDER_TARGET_0,
121
122 GX2_BLEND_ONE,
123 GX2_BLEND_ZERO,
124 GX2_BLEND_COMBINE_ADD,
125
126 GX2_DISABLE,
127
128 GX2_BLEND_ONE,
129 GX2_BLEND_ZERO,
130 GX2_BLEND_COMBINE_ADD
131 );
132 m_CompressShader.Bind();
133 FFLiTempSetContextState(NULL);
134}
135
136template <typename T>
137bool FFLiCompressorImpl<T>::UseUB() const
138{
139 return m_CompressShader.UseUB();
140}
141
142template <typename T>
143void FFLiCompressorImpl<T>::CompressImpl(GX2Texture* pDst, const GX2Texture* pSrc, const FFLiCompressorParam* pParam)
144{
145 m_ContextState.Bind();
146
147 GX2ColorBuffer* pColorBuffer = &m_ColorBuffer;
148
149 m_CompressShader.SetTexture(pSrc);
150 m_CompressDrawer.SetAttributeBuffer(0, 1);
151
152 for (u32 i = 0; i < pDst->surface.numMips; i++)
153 {
154 T::SetupColorBuffer(pColorBuffer, pDst, i);
155
156 GX2SetColorBuffer(pColorBuffer, GX2_RENDER_TARGET_0);
157
158 u32 width = FFLiCalculateUnitTextureResolution(pDst->surface.width, i) >> i;
159 u32 height = FFLiCalculateUnitTextureResolution(pDst->surface.height, i) >> i;
160 GX2SetViewport(0.0f, 0.0f, width, height, 0.0f, 1.0f);
161 GX2SetScissor(0, 0, width, height);
162
163 m_CompressShader.SetUniform(pParam->GetUniform(i));
165 }
166
167 FFLiInvalidateColorBuffer(pColorBuffer);
168 FFLiInvalidateTexture(pDst);
169}
170
171#endif // FFLI_COMPRESSOR_IMPL_H_
NN_STATIC_ASSERT(sizeof(FFLiAllocator)==4)
FFLiCompressorImpl< FFLiCompressorDescBC3 > FFLiCompressorImplBC3
Definition FFLiCompressorImpl.h:43
FFLiCompressorImpl< FFLiCompressorDescBC1 > FFLiCompressorImplBC1
Definition FFLiCompressorImpl.h:42
Definition FFLiBufferAllocator.h:16
Definition FFLiCompressDrawer.h:9
void SetupGPU()
Definition FFLiCompressDrawer.cpp:42
void Draw()
Definition FFLiCompressDrawer.cpp:60
FFLiCompressDrawer()
Definition FFLiCompressDrawer.cpp:8
Definition FFLiCompressShader.h:12
Definition FFLiCompressorImpl.h:19
FFLiCompressDrawer m_CompressDrawer
Definition FFLiCompressorImpl.h:38
static u32 GetBufferSize(const void *pShaderData, u32 shaderIndex)
Definition FFLiCompressorImpl.h:68
~FFLiCompressorImpl()
Definition FFLiCompressorImpl.h:63
bool SetupCPU(const void *pShaderData, u32 shaderIndex)
Definition FFLiCompressorImpl.h:86
static u32 GetTextureBufferSize(u32 width, u32 height, u32 numMips)
Definition FFLiCompressorImpl.h:78
GX2ColorBuffer m_ColorBuffer
Definition FFLiCompressorImpl.h:39
bool UseUB() const
Definition FFLiCompressorImpl.h:137
FFLiCompressorImpl(FFLiBufferAllocator *pAllocator, const void *pShaderData, u32 shaderIndex)
Definition FFLiCompressorImpl.h:49
FFLiContextState m_ContextState
Definition FFLiCompressorImpl.h:36
void SetupGPU()
Definition FFLiCompressorImpl.h:98
void CompressImpl(GX2Texture *pDst, const GX2Texture *pSrc, const FFLiCompressorParam *pParam)
Definition FFLiCompressorImpl.h:143
FFLiCompressShader m_CompressShader
Definition FFLiCompressorImpl.h:37
FFLiBufferAllocator m_BufferAllocator
Definition FFLiCompressorImpl.h:35
Definition FFLiCompressorParam.h:12
Definition FFLiContextState.h:11
Definition FFLiCompressorDesc.h:9
Definition FFLiCompressorDesc.h:19