1
0
mirror of https://github.com/blawar/GLideN64.git synced 2024-06-30 08:24:05 +00:00
GLideN64/src/NoiseTexture.cpp

183 lines
4.5 KiB
C++
Raw Normal View History

2017-10-02 17:59:43 +00:00
#ifdef MINGW
#define _CRT_RAND_S
#endif
#include <thread>
#include <algorithm>
#include <random>
#include <functional>
#include <cstdlib>
2017-01-05 12:37:14 +00:00
#include <Graphics/Context.h>
#include <Graphics/Parameters.h>
#include "FrameBuffer.h"
#include "Config.h"
2017-01-05 12:37:14 +00:00
#include "GBI.h"
#include "VI.h"
2017-01-05 12:37:14 +00:00
#include "Textures.h"
#include "NoiseTexture.h"
#include "DisplayWindow.h"
#include "DisplayLoadProgress.h"
2017-01-05 12:37:14 +00:00
using namespace graphics;
#define NOISE_TEX_WIDTH 640
#define NOISE_TEX_HEIGHT 580
2017-01-05 12:37:14 +00:00
NoiseTexture g_noiseTexture;
NoiseTexture::NoiseTexture()
2017-02-01 20:38:02 +00:00
: m_DList(0)
, m_currTex(0)
, m_prevTex(0)
2017-01-05 12:37:14 +00:00
{
for (u32 i = 0; i < NOISE_TEX_NUM; ++i)
m_pTexture[i] = nullptr;
2017-01-05 12:37:14 +00:00
}
2017-10-02 17:59:43 +00:00
static
u32 Rand(u32 rand_value)
{
#ifdef MINGW
rand_s(&rand_value);
#else
rand_value = rand();
#endif
return rand_value;
}
static
void FillTextureData(u32 _seed, NoiseTexturesData * _pData, u32 _start, u32 _stop)
{
srand(_seed);
for (u32 i = _start; i < _stop; ++i) {
auto & vec = _pData->at(i);
const size_t sz = vec.size();
u32 rand_value(0U);
2017-10-02 17:59:43 +00:00
for (size_t t = 0; t < sz; ++t) {
rand_value = Rand(rand_value);
vec[t] = rand_value & 0xFF;
}
}
}
2017-02-19 07:46:47 +00:00
void NoiseTexture::_fillTextureData()
{
displayLoadProgress(L"INIT NOISE TEXTURES. PLEASE WAIT...");
for (auto& vec : m_texData)
vec.resize(NOISE_TEX_WIDTH * NOISE_TEX_HEIGHT);
const u32 concurentThreadsSupported = std::thread::hardware_concurrency();
if (concurentThreadsSupported > 1) {
const u32 numThreads = concurentThreadsSupported;
u32 chunk = NOISE_TEX_NUM / numThreads;
if (NOISE_TEX_NUM % numThreads != 0)
chunk++;
std::uniform_int_distribution<u32> uint_dist;
std::mt19937 engine; // Mersenne twister MT19937
engine.seed(std::mt19937::default_seed);
auto generator = std::bind(uint_dist, engine);
std::vector<std::thread> threads;
u32 start = 0;
do {
threads.emplace_back(
2017-02-21 02:47:33 +00:00
FillTextureData,
generator(),
&m_texData,
2017-02-21 02:47:33 +00:00
start,
std::min(start + chunk, static_cast<u32>(m_texData.size())));
start += chunk;
} while (start < NOISE_TEX_NUM - chunk);
2018-05-06 08:54:12 +00:00
FillTextureData(generator(), &m_texData, start, static_cast<u32>(m_texData.size()));
for (auto& t : threads)
t.join();
} else {
2018-05-06 08:54:12 +00:00
FillTextureData(static_cast<u32>(time(nullptr)), &m_texData, 0, static_cast<u32>(m_texData.size()));
}
2017-02-19 07:46:47 +00:00
displayLoadProgress(L"");
}
void NoiseTexture::init()
{
if (!gfxContext)
return;
if (m_texData[0].empty())
_fillTextureData();
2017-02-01 20:38:02 +00:00
for (u32 i = 0; i < NOISE_TEX_NUM; ++i) {
m_pTexture[i] = textureCache().addFrameBufferTexture(textureTarget::TEXTURE_2D);
2017-02-01 20:38:02 +00:00
m_pTexture[i]->format = G_IM_FMT_RGBA;
m_pTexture[i]->clampS = 1;
m_pTexture[i]->clampT = 1;
m_pTexture[i]->frameBufferTexture = CachedTexture::fbOneSample;
m_pTexture[i]->maskS = 0;
m_pTexture[i]->maskT = 0;
m_pTexture[i]->mirrorS = 0;
m_pTexture[i]->mirrorT = 0;
m_pTexture[i]->width = NOISE_TEX_WIDTH;
m_pTexture[i]->height = NOISE_TEX_HEIGHT;
m_pTexture[i]->textureBytes = m_pTexture[i]->width * m_pTexture[i]->height;
2017-01-05 12:37:14 +00:00
2017-02-05 06:47:36 +00:00
const FramebufferTextureFormats & fbTexFormats = gfxContext.getFramebufferTextureFormats();
2017-02-01 20:38:02 +00:00
{
Context::InitTextureParams params;
params.handle = m_pTexture[i]->name;
params.textureUnitIndex = textureIndices::NoiseTex;
params.width = m_pTexture[i]->width;
params.height = m_pTexture[i]->height;
2017-02-05 06:47:36 +00:00
params.internalFormat = fbTexFormats.noiseInternalFormat;
params.format = fbTexFormats.noiseFormat;
params.dataType = fbTexFormats.noiseType;
params.data = m_texData[i].data();
2017-02-01 20:38:02 +00:00
gfxContext.init2DTexture(params);
}
{
Context::TexParameters params;
params.handle = m_pTexture[i]->name;
params.target = textureTarget::TEXTURE_2D;
params.textureUnitIndex = textureIndices::NoiseTex;
params.minFilter = textureParameters::FILTER_NEAREST;
params.magFilter = textureParameters::FILTER_NEAREST;
gfxContext.setTextureParameters(params);
}
2017-01-05 12:37:14 +00:00
}
}
void NoiseTexture::destroy()
{
2017-02-01 20:38:02 +00:00
for (u32 i = 0; i < NOISE_TEX_NUM; ++i) {
textureCache().removeFrameBufferTexture(m_pTexture[i]);
m_pTexture[i] = nullptr;
}
2017-01-05 12:37:14 +00:00
}
void NoiseTexture::update()
{
if (m_texData[0].empty() || m_DList == dwnd().getBuffersSwapCount())
2017-01-05 12:37:14 +00:00
return;
2017-01-06 07:33:49 +00:00
u32 rand_value(0U);
2017-10-02 17:59:43 +00:00
while (m_currTex == m_prevTex) {
rand_value = Rand(rand_value);
m_currTex = rand_value % NOISE_TEX_NUM;
}
2017-02-01 20:38:02 +00:00
m_prevTex = m_currTex;
if (m_pTexture[m_currTex] == nullptr)
2017-01-05 12:37:14 +00:00
return;
2017-02-01 20:38:02 +00:00
{
Context::BindTextureParameters params;
params.texture = m_pTexture[m_currTex]->name;
params.textureUnitIndex = textureIndices::NoiseTex;
params.target = textureTarget::TEXTURE_2D;
gfxContext.bindTexture(params);
2017-01-06 07:33:49 +00:00
}
m_DList = dwnd().getBuffersSwapCount();
2017-01-05 12:37:14 +00:00
}