2017-02-19 09:13:25 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <array>
|
|
|
|
#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"
|
2017-01-10 15:22:32 +00:00
|
|
|
#include "Config.h"
|
2017-01-05 12:37:14 +00:00
|
|
|
#include "GBI.h"
|
2017-01-10 15:22:32 +00:00
|
|
|
#include "VI.h"
|
2017-01-05 12:37:14 +00:00
|
|
|
#include "Textures.h"
|
|
|
|
#include "NoiseTexture.h"
|
2017-01-15 07:57:25 +00:00
|
|
|
#include "DisplayWindow.h"
|
2017-01-05 12:37:14 +00:00
|
|
|
|
2017-01-21 12:48:02 +00:00
|
|
|
using namespace graphics;
|
|
|
|
|
2017-02-19 09:13:25 +00:00
|
|
|
#define NOISE_TEX_WIDTH 640
|
2017-02-21 02:49:57 +00:00
|
|
|
#define NOISE_TEX_HEIGHT 580
|
2017-02-19 09:13:25 +00:00
|
|
|
|
2017-01-05 12:37:14 +00:00
|
|
|
NoiseTexture g_noiseTexture;
|
|
|
|
|
|
|
|
NoiseTexture::NoiseTexture()
|
2017-02-01 20:38:02 +00:00
|
|
|
: m_DList(0)
|
2017-01-05 12:37:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-23 08:19:05 +00:00
|
|
|
typedef std::array<std::vector<u8>, NOISE_TEX_NUM> NoiseTexturesData;
|
2017-02-19 09:13:25 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
for (size_t t = 0; t < sz; ++t)
|
2017-03-23 08:19:05 +00:00
|
|
|
vec[t] = rand() & 0xFF;
|
2017-02-19 09:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 12:37:14 +00:00
|
|
|
void NoiseTexture::init()
|
|
|
|
{
|
|
|
|
if (config.generalEmulation.enableNoise == 0)
|
|
|
|
return;
|
2017-02-19 07:46:47 +00:00
|
|
|
|
2017-02-19 09:13:25 +00:00
|
|
|
NoiseTexturesData texData;
|
|
|
|
for (auto& vec : texData)
|
2017-03-23 08:19:05 +00:00
|
|
|
vec.resize(NOISE_TEX_WIDTH * NOISE_TEX_HEIGHT);
|
2017-02-19 09:13:25 +00:00
|
|
|
|
|
|
|
const u32 concurentThreadsSupported = std::thread::hardware_concurrency();
|
2017-03-23 08:35:22 +00:00
|
|
|
if (concurentThreadsSupported > 1) {
|
|
|
|
const u32 numThreads = concurentThreadsSupported;
|
2017-02-19 09:13:25 +00:00
|
|
|
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(),
|
|
|
|
&texData,
|
|
|
|
start,
|
|
|
|
std::min(start + chunk, static_cast<u32>(texData.size())));
|
2017-02-19 09:13:25 +00:00
|
|
|
start += chunk;
|
2017-03-23 08:35:22 +00:00
|
|
|
} while (start < NOISE_TEX_NUM - chunk);
|
|
|
|
|
|
|
|
FillTextureData(generator(), &texData, start, texData.size());
|
2017-02-19 09:13:25 +00:00
|
|
|
|
|
|
|
for (auto& t : threads)
|
|
|
|
t.join();
|
|
|
|
} else {
|
|
|
|
FillTextureData(static_cast<u32>(time(nullptr)), &texData, 0, texData.size());
|
|
|
|
}
|
2017-02-19 07:46:47 +00:00
|
|
|
|
2017-02-01 20:38:02 +00:00
|
|
|
for (u32 i = 0; i < NOISE_TEX_NUM; ++i) {
|
|
|
|
m_pTexture[i] = textureCache().addFrameBufferTexture(false);
|
|
|
|
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;
|
2017-02-19 09:13:25 +00:00
|
|
|
m_pTexture[i]->realWidth = NOISE_TEX_WIDTH;
|
2017-02-21 02:49:57 +00:00
|
|
|
m_pTexture[i]->realHeight = NOISE_TEX_HEIGHT;
|
2017-02-01 20:38:02 +00:00
|
|
|
m_pTexture[i]->textureBytes = m_pTexture[i]->realWidth * m_pTexture[i]->realHeight;
|
|
|
|
textureCache().addFrameBufferTextureSize(m_pTexture[i]->textureBytes);
|
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.width = m_pTexture[i]->realWidth;
|
|
|
|
params.height = m_pTexture[i]->realHeight;
|
2017-02-05 06:47:36 +00:00
|
|
|
params.internalFormat = fbTexFormats.noiseInternalFormat;
|
|
|
|
params.format = fbTexFormats.noiseFormat;
|
|
|
|
params.dataType = fbTexFormats.noiseType;
|
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);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
Context::UpdateTextureDataParams params;
|
|
|
|
params.handle = m_pTexture[i]->name;
|
|
|
|
params.textureUnitIndex = textureIndices::NoiseTex;
|
2017-02-19 07:46:47 +00:00
|
|
|
params.width = m_pTexture[i]->realWidth;
|
|
|
|
params.height = m_pTexture[i]->realHeight;
|
2017-02-05 06:47:36 +00:00
|
|
|
params.format = fbTexFormats.noiseFormat;
|
|
|
|
params.dataType = fbTexFormats.noiseType;
|
2017-02-19 09:13:25 +00:00
|
|
|
params.data = texData[i].data();
|
2017-02-01 20:38:02 +00:00
|
|
|
gfxContext.update2DTexture(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()
|
|
|
|
{
|
2017-01-15 07:57:25 +00:00
|
|
|
if (m_DList == dwnd().getBuffersSwapCount() || config.generalEmulation.enableNoise == 0)
|
2017-01-05 12:37:14 +00:00
|
|
|
return;
|
2017-01-06 07:33:49 +00:00
|
|
|
|
2017-02-01 20:38:02 +00:00
|
|
|
while (m_currTex == m_prevTex)
|
|
|
|
m_currTex = rand() % NOISE_TEX_NUM;
|
|
|
|
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
|
|
|
}
|
2017-01-15 07:57:25 +00:00
|
|
|
m_DList = dwnd().getBuffersSwapCount();
|
2017-01-05 12:37:14 +00:00
|
|
|
}
|