Skip to content

Commit

Permalink
add support for texture that contains the contents of the previous frame
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargaj committed Jan 31, 2021
1 parent 122be00 commit b898afd
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,5 +2,6 @@ Bonzomatic
.obj
tmp
build
build.*
.vs
.DS_Store
8 changes: 4 additions & 4 deletions cmake_all.bat
Expand Up @@ -42,10 +42,10 @@ REM --------------------- BUILD TIME -------------------------------

:build

set BNZ_COMPILER=Visual Studio 10 2010
if not "%BNZ_X64%"=="ON" goto skipme
set BNZ_COMPILER=Visual Studio 10 2010 Win64
:skipme
set BNZ_COMPILER=Visual Studio 14 2015
if not "%BNZ_X64%"=="ON" goto skipvs
set BNZ_COMPILER=Visual Studio 14 2015 Win64
:skipvs

set BNZ_OUT_DIR=x86
set BNZ_PLATFORM=W32
Expand Down
4 changes: 4 additions & 0 deletions src/Renderer.h
Expand Up @@ -56,13 +56,17 @@ namespace Renderer
TEXTURETYPE type;
};

Texture * CreateRGBA8Texture();
Texture * CreateRGBA8TextureFromFile( const char * szFilename );
Texture * CreateA8TextureFromData( int w, int h, const unsigned char * data );
Texture * Create1DR32Texture( int w );
bool UpdateR32Texture( Texture * tex, float * data );
void SetShaderTexture( const char * szTextureName, Texture * tex );
void BindTexture( Texture * tex ); // temporary function until all the quad rendering is moved to the renderer
void ReleaseTexture( Texture * tex );

void CopyBackbufferToTexture( Texture * tex );

struct Vertex
{
Vertex( float _x, float _y, unsigned int _c = 0xFFFFFFFF, float _u = 0.0, float _v = 0.0) :
Expand Down
10 changes: 10 additions & 0 deletions src/main.cpp
Expand Up @@ -304,6 +304,7 @@ int main(int argc, const char *argv[])
return 0;
}

Renderer::Texture * texPreviousFrame = Renderer::CreateRGBA8Texture();
Renderer::Texture * texFFT = Renderer::Create1DR32Texture( FFT_SIZE );
Renderer::Texture * texFFTSmoothed = Renderer::Create1DR32Texture( FFT_SIZE );
Renderer::Texture * texFFTIntegrated = Renderer::Create1DR32Texture( FFT_SIZE );
Expand Down Expand Up @@ -390,6 +391,7 @@ int main(int argc, const char *argv[])
bool bShowGui = true;
Timer::Start();
float fNextTick = 0.1f;
float fLastTimeMS = Timer::GetTime();
while (!Renderer::WantsToQuit())
{
bool newShader = false;
Expand Down Expand Up @@ -481,6 +483,10 @@ int main(int argc, const char *argv[])
Renderer::SetShaderConstant( "fGlobalTime", time );
Renderer::SetShaderConstant( "v2Resolution", settings.sRenderer.nWidth, settings.sRenderer.nHeight );

float fTime = Timer::GetTime();
Renderer::SetShaderConstant( "fFrameTime", ( fTime - fLastTimeMS ) / 1000.0f );
fLastTimeMS = fTime;

for (std::map<int,std::string>::iterator it = midiRoutes.begin(); it != midiRoutes.end(); it++)
{
Renderer::SetShaderConstant( it->second.c_str(), MIDI::GetCCValue( it->first ) );
Expand Down Expand Up @@ -510,6 +516,7 @@ int main(int argc, const char *argv[])
Renderer::SetShaderTexture( "texFFT", texFFT );
Renderer::SetShaderTexture( "texFFTSmoothed", texFFTSmoothed );
Renderer::SetShaderTexture( "texFFTIntegrated", texFFTIntegrated );
Renderer::SetShaderTexture( "texPreviousFrame", texPreviousFrame );

for (std::map<std::string, Renderer::Texture*>::iterator it = textures.begin(); it != textures.end(); it++)
{
Expand All @@ -518,6 +525,8 @@ int main(int argc, const char *argv[])

Renderer::RenderFullscreenQuad();

Renderer::CopyBackbufferToTexture( texPreviousFrame );

Renderer::StartTextRendering();

if (bShowGui)
Expand Down Expand Up @@ -591,6 +600,7 @@ int main(int argc, const char *argv[])
MIDI::Close();
FFT::Close();

Renderer::ReleaseTexture( texPreviousFrame );
Renderer::ReleaseTexture( texFFT );
Renderer::ReleaseTexture( texFFTSmoothed );
for (std::map<std::string, Renderer::Texture*>::iterator it = textures.begin(); it != textures.end(); it++)
Expand Down
35 changes: 35 additions & 0 deletions src/platform_glfw/Renderer.cpp
Expand Up @@ -123,10 +123,12 @@ namespace Renderer
"\n"
"uniform float fGlobalTime; // in seconds\n"
"uniform vec2 v2Resolution; // viewport resolution (in pixels)\n"
"uniform float fFrameTime; // duration of the last frame, in seconds\n"
"\n"
"uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq\n"
"uniform sampler1D texFFTSmoothed; // this one has longer falloff and less harsh transients\n"
"uniform sampler1D texFFTIntegrated; // this is continually increasing\n"
"uniform sampler2D texPreviousFrame; // screenshot of the previous frame\n"
"{%textures:begin%}" // leave off \n here
"uniform sampler2D {%textures:name%};\n"
"{%textures:end%}" // leave off \n here
Expand Down Expand Up @@ -690,6 +692,32 @@ namespace Renderer
};

int textureUnit = 0;

Texture * CreateRGBA8Texture()
{
void * data = NULL;
GLenum internalFormat = GL_SRGB8_ALPHA8;
GLenum srcFormat = GL_FLOAT;
GLenum format = GL_UNSIGNED_BYTE;

GLuint glTexId = 0;
glGenTextures( 1, &glTexId );
glBindTexture( GL_TEXTURE_2D, glTexId );

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

GLTexture * tex = new GLTexture();
tex->width = nWidth;
tex->height = nHeight;
tex->ID = glTexId;
tex->type = TEXTURETYPE_2D;
tex->unit = textureUnit++;
return tex;
}

Texture * CreateRGBA8TextureFromFile( const char * szFilename )
{
int comp = 0;
Expand Down Expand Up @@ -815,6 +843,13 @@ namespace Renderer
glDeleteTextures(1, &((GLTexture*)tex)->ID );
}

void CopyBackbufferToTexture( Texture * tex )
{
glActiveTexture( GL_TEXTURE0 + ( (GLTexture *) tex )->unit );
glBindTexture( GL_TEXTURE_2D, ( (GLTexture *) tex )->ID );
glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, nWidth, nHeight, 0 );
}

//////////////////////////////////////////////////////////////////////////
// text rendering

Expand Down
38 changes: 38 additions & 0 deletions src/platform_w32_dx11/Renderer.cpp
Expand Up @@ -112,12 +112,14 @@ namespace Renderer
"Texture1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq\n"
"Texture1D texFFTSmoothed; // this one has longer falloff and less harsh transients\n"
"Texture1D texFFTIntegrated; // this is continually increasing\n"
"Texture2D texPreviousFrame; // screenshot of the previous frame\n"
"SamplerState smp;\n"
"\n"
"cbuffer constants\n"
"{\n"
"\tfloat fGlobalTime; // in seconds\n"
"\tfloat2 v2Resolution; // viewport resolution (in pixels)\n"
"\tfloat fFrameTime; // duration of the last frame, in seconds\n"
"{%midi:begin%}"
"\tfloat {%midi:name%};\n"
"{%midi:end%}"
Expand Down Expand Up @@ -912,6 +914,35 @@ namespace Renderer
}

int textureUnit = 0;

Renderer::Texture * CreateRGBA8Texture()
{
D3D11_TEXTURE2D_DESC desc;
ZeroMemory( &desc, sizeof( D3D11_TEXTURE2D_DESC ) );
desc.Width = nWidth;
desc.Height = nHeight;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.SampleDesc.Count = 1;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

ID3D11Texture2D * pTex = NULL;

if ( pDevice->CreateTexture2D( &desc, NULL, &pTex ) != S_OK )
return NULL;

DX11Texture * tex = new DX11Texture();
tex->width = nWidth;
tex->height = nHeight;
tex->pTexture = pTex;
tex->type = TEXTURETYPE_2D;
tex->format = desc.Format;
CreateResourceView( tex );
return tex;

}

Texture * CreateRGBA8TextureFromFile( const char * szFilename )
{
int comp = 0;
Expand Down Expand Up @@ -1057,6 +1088,13 @@ namespace Renderer
delete tex;
}

void CopyBackbufferToTexture( Texture * tex )
{
ID3D11Resource * pTex = ( (DX11Texture *) tex )->pTexture;

pContext->CopySubresourceRegion( pTex, 0, 0, 0, 0, pBackBuffer, 0, NULL );
}

//////////////////////////////////////////////////////////////////////////
// text rendering

Expand Down
61 changes: 47 additions & 14 deletions src/platform_w32_dx9/Renderer.cpp
Expand Up @@ -116,6 +116,8 @@ namespace Renderer
"// this one has longer falloff and less harsh transients\n"
"texture texFFTIntegratedT; sampler1D texFFTIntegrated = sampler_state { Texture = <texFFTIntegratedT>; }; \n"
"// this is continually increasing\n"
"texture texPreviousFrameT; sampler2D texPreviousFrame = sampler_state { Texture = <texPreviousFrameT>; }; \n"
"// screenshot of the previous frame\n"
"\n"
"{%textures:begin%}" // leave off \n here
"texture raw{%textures:name%}; sampler2D {%textures:name%} = sampler_state { Texture = <raw{%textures:name%}>; };\n"
Expand All @@ -125,6 +127,7 @@ namespace Renderer
"float {%midi:name%};\n"
"{%midi:end%}"
"float fGlobalTime; // in seconds\n"
"float fFrameTime; // duration of the last frame, in seconds\n"
"float2 v2Resolution; // viewport resolution (in pixels)\n"
"\n"
"float4 plas( float2 v, float time )\n"
Expand Down Expand Up @@ -480,11 +483,12 @@ namespace Renderer
D3DDECL_END()
};

static float pQuad[] = {
-1.0, -1.0, 0.0, 0.0, 0.0,
-1.0, 1.0, 0.0, 0.0, 1.0,
1.0, -1.0, 0.0, 1.0, 0.0,
1.0, 1.0, 0.0, 1.0, 1.0,
static float pQuad[] =
{
-1.0, -1.0, 0.0, 0.0 + 0.5 / (float)nWidth, 0.0 + 0.5 / (float)nHeight,
-1.0, 1.0, 0.0, 0.0 + 0.5 / (float)nWidth, 1.0 + 0.5 / (float)nHeight,
1.0, -1.0, 0.0, 1.0 + 0.5 / (float)nWidth, 0.0 + 0.5 / (float)nHeight,
1.0, 1.0, 0.0, 1.0 + 0.5 / (float)nWidth, 1.0 + 0.5 / (float)nHeight,
};

pDevice->CreateVertexBuffer( 4 * 5 * sizeof(float), D3DUSAGE_WRITEONLY, D3DFVF_XYZ | D3DFVF_TEX1, D3DPOOL_DEFAULT, &pFullscreenQuadVB, NULL);
Expand Down Expand Up @@ -623,15 +627,33 @@ namespace Renderer
};

int textureUnit = 0;

Renderer::Texture * CreateRGBA8Texture()
{
LPDIRECT3DTEXTURE9 pTex = NULL;
pDevice->CreateTexture( nWidth, nHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pTex, NULL );

if ( !pTex )
return NULL;

DX9Texture * tex = new DX9Texture();
tex->pTexture = pTex;
tex->width = nWidth;
tex->height = nHeight;
tex->type = TEXTURETYPE_2D;
return tex;

}

Texture * CreateRGBA8TextureFromFile( const char * szFilename )
{
LPDIRECT3DTEXTURE9 pTex = NULL;
D3DXIMAGE_INFO info;
HRESULT h = D3DXCreateTextureFromFileExA(
pDevice,
szFilename,
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DX_DEFAULT_NONPOW2,
D3DX_DEFAULT_NONPOW2,
0,
NULL,
D3DFMT_FROM_FILE,
Expand Down Expand Up @@ -734,10 +756,27 @@ namespace Renderer
return tex;
}

void ReleaseTexture( Texture * tex )
{
( (DX9Texture *) tex )->pTexture->Release();
delete tex;
}

void CopyBackbufferToTexture( Texture * tex )
{
LPDIRECT3DTEXTURE9 pTex = ( (DX9Texture *) tex )->pTexture;
LPDIRECT3DSURFACE9 pSurf = NULL;
pTex->GetSurfaceLevel( 0, &pSurf );
if ( pSurf )
{
HRESULT res = pDevice->StretchRect( pBackBuffer, NULL, pSurf, NULL, D3DTEXF_LINEAR );
pSurf->Release();
}
}

//////////////////////////////////////////////////////////////////////////
// text rendering


void StartTextRendering()
{
pDevice->SetVertexShader( NULL );
Expand All @@ -764,12 +803,6 @@ namespace Renderer
pDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_TEXTURE );
}

void ReleaseTexture( Texture * tex )
{
((DX9Texture *)tex)->pTexture->Release();
delete tex;
}

int bufferPointer = 0;
unsigned char buffer[GUIQUADVB_SIZE * sizeof(float) * 6];
bool lastModeIsQuad = true;
Expand Down

0 comments on commit b898afd

Please sign in to comment.