2017-10-02 17:59:43 +00:00
|
|
|
#ifdef MINGW
|
|
|
|
#define _CRT_RAND_S
|
|
|
|
#endif
|
|
|
|
|
2017-02-19 09:13:25 +00:00
|
|
|
#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"
|
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-10-25 17:20:53 +00:00
|
|
|
#include "DisplayLoadProgress.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-11-18 15:07:27 +00:00
|
|
|
, m_currTex(0)
|
|
|
|
, m_prevTex(0)
|
2017-01-05 12:37:14 +00:00
|
|
|
{
|
2018-05-06 08:28:07 +00:00
|
|
|
for (u32 i = 0; i < NOISE_TEX_NUM; ++i)
|
2017-11-18 15:07:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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();
|
2017-10-04 09:29:09 +00:00
|
|
|
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 09:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-19 07:46:47 +00:00
|
|
|
|
2017-10-27 03:43:14 +00:00
|
|
|
void NoiseTexture::_fillTextureData()
|
|
|
|
{
|
2017-10-25 17:20:53 +00:00
|
|
|
displayLoadProgress(L"INIT NOISE TEXTURES. PLEASE WAIT...");
|
|
|
|
|
2017-10-27 03:43:14 +00:00
|
|
|
for (auto& vec : m_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(),
|
2017-10-27 03:43:14 +00:00
|
|
|
&m_texData,
|
2017-02-21 02:47:33 +00:00
|
|
|
start,
|
2017-10-27 03:43:14 +00:00
|
|
|
std::min(start + chunk, static_cast<u32>(m_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);
|
|
|
|
|
2018-05-06 08:54:12 +00:00
|
|
|
FillTextureData(generator(), &m_texData, start, static_cast<u32>(m_texData.size()));
|
2017-02-19 09:13:25 +00:00
|
|
|
|
|
|
|
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 09:13:25 +00:00
|
|
|
}
|
2017-02-19 07:46:47 +00:00
|
|
|
|
2017-10-27 03:43:14 +00:00
|
|
|
displayLoadProgress(L"");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void NoiseTexture::init()
|
|
|
|
{
|
2022-02-15 21:50:30 +00:00
|
|
|
if (!gfxContext)
|
|
|
|
return;
|
2017-10-27 03:43:14 +00:00
|
|
|
if (m_texData[0].empty())
|
|
|
|
_fillTextureData();
|
|
|
|
|
2017-02-01 20:38:02 +00:00
|
|
|
for (u32 i = 0; i < NOISE_TEX_NUM; ++i) {
|
2019-11-29 22:09:09 +00:00
|
|
|
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;
|
2019-05-08 14:04:24 +00:00
|
|
|
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;
|
2018-03-08 02:45:28 +00:00
|
|
|
params.textureUnitIndex = textureIndices::NoiseTex;
|
2019-05-08 14:04:24 +00:00
|
|
|
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;
|
2018-03-08 02:45:28 +00:00
|
|
|
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()
|
|
|
|
{
|
2020-03-12 10:29:53 +00:00
|
|
|
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
|
|
|
|
2017-10-04 09:29:09 +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
|
|
|
}
|
2017-01-15 07:57:25 +00:00
|
|
|
m_DList = dwnd().getBuffersSwapCount();
|
2017-01-05 12:37:14 +00:00
|
|
|
}
|