Fix up numerous bgfx shader bugs, add auto-uniform u_texsize, nw

This commit is contained in:
therealmogminer@gmail.com 2016-03-08 23:55:54 +01:00
parent 3172371fcd
commit a20459a6a3
18 changed files with 153 additions and 113 deletions

View File

@ -3,9 +3,9 @@
"sliders": [
{ "type": "int_enum", "name": "adjustments", "text": "Enable Adjustments", "default": 0, "max": 1, "min": 0, "step": 1, "scale": 1.0, "format": "%s", "screen": "any", "strings": [ "On", "Off" ] },
{ "type": "float", "name": "ratio_amount", "text": "Ratio Amount", "default": 0, "max": 100, "min": 0, "step": 1, "scale": 0.01, "format": "%1.2f", "screen": "any" },
{ "type": "color", "name": "red_ratios", "text": "Color Matrix, Red from ", "default": [ 200, 0, 0 ], "max": [ 400, 400, 400 ], "min": [ -400, -400, -400 ], "step": 1, "scale": 0.005, "format": "%2.3f", "screen": "any" },
{ "type": "color", "name": "grn_ratios", "text": "Color Matrix, Green from ", "default": [ 0, 200, 0 ], "max": [ 400, 400, 400 ], "min": [ -400, -400, -400 ], "step": 1, "scale": 0.005, "format": "%2.3f", "screen": "any" },
{ "type": "color", "name": "blu_ratios", "text": "Color Matrix, Blue from ", "default": [ 0, 0, 200 ], "max": [ 400, 400, 400 ], "min": [ -400, -400, -400 ], "step": 1, "scale": 0.005, "format": "%2.3f", "screen": "any" },
{ "type": "color", "name": "red_ratios", "text": "Color Matrix, Red from ", "default": [ 200, 0, 0 ], "max": [ 400, 400, 400 ], "min": [ 0, 0, 0 ], "step": 1, "scale": 0.005, "format": "%2.3f", "screen": "any" },
{ "type": "color", "name": "grn_ratios", "text": "Color Matrix, Green from ", "default": [ 0, 200, 0 ], "max": [ 400, 400, 400 ], "min": [ 0, 0, 0 ], "step": 1, "scale": 0.005, "format": "%2.3f", "screen": "any" },
{ "type": "color", "name": "blu_ratios", "text": "Color Matrix, Blue from ", "default": [ 0, 0, 200 ], "max": [ 400, 400, 400 ], "min": [ 0, 0, 0 ], "step": 1, "scale": 0.005, "format": "%2.3f", "screen": "any" },
{ "type": "color", "name": "tint", "text": "Tint ", "default": [ 100, 100, 100 ], "max": [ 100, 100, 100 ], "min": [ 0, 0, 0 ], "step": 1, "scale": 0.01, "format": "%1.2f", "screen": "any" },
{ "type": "color", "name": "phosphor", "text": "Phosphor Life, ", "default": [ 0, 0, 0 ], "max": [ 100, 100, 100 ], "min": [ 0, 0, 0 ], "step": 1, "scale": 0.01, "format": "%1.2f", "screen": "any" },
{ "type": "vec2", "name": "shift", "text": "Frame Shift ", "default": [ 0, 0 ], "max": [ 25, 25 ], "min": [ -25, -25 ], "step": 1, "scale": 1.0, "format": "%2.f", "screen": "raster" }
@ -18,6 +18,10 @@
"mode": "native",
"prescale": 1
},
{ "name": "native2",
"mode": "native",
"prescale": 1
},
{ "name": "previous",
"mode": "native",
"prescale": 1
@ -53,7 +57,7 @@
"input": [
{ "sampler": "s_tex", "texture": "native" }
],
"output": "native"
"output": "native2"
},
{ "effect": "phosphor",
"name": "Phosphor Decay",
@ -66,7 +70,7 @@
{ "uniform": "u_phosphor", "slider": "phosphor" }
],
"input": [
{ "sampler": "s_tex", "texture": "native" },
{ "sampler": "s_tex", "texture": "native2" },
{ "sampler": "s_prev", "texture": "previous" }
],
"output": "native"

View File

@ -17,8 +17,9 @@
"vertex": "vs_tint",
"fragment": "fs_tint",
"uniforms": [
{ "name": "s_tex", "type": "int", "values": [ 1.0 ] },
{ "name": "u_tint", "type": "vec4", "values": [ 1.0, 1.0, 1.0, 1.0 ] },
{ "name": "u_shift", "type": "vec4", "values": [ 0.1, 0.1, 0.0, 0.0 ] }
{ "name": "s_tex", "type": "int", "values": [ 1.0 ] },
{ "name": "u_tint", "type": "vec4", "values": [ 1.0, 1.0, 1.0, 1.0 ] },
{ "name": "u_shift", "type": "vec4", "values": [ 0.1, 0.1, 0.0, 0.0 ] },
{ "name": "u_texsize", "type": "vec4", "values": [ 1.0, 1.0, 0.0, 0.0 ] }
]
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -48,77 +48,17 @@ bgfx_chain::~bgfx_chain()
void bgfx_chain::process(render_primitive* prim, int view, texture_manager& textures, uint16_t screen_width, uint16_t screen_height, uint64_t blend)
{
for (bgfx_chain_entry* entry : m_entries)
for (int index = 0; index < m_entries.size(); index++)
{
bgfx::TransientVertexBuffer buffer;
if (bgfx::checkAvailTransientVertexBuffer(6, ScreenVertex::ms_decl))
if (index == (m_entries.size() - 1))
{
bgfx::allocTransientVertexBuffer(&buffer, 6, ScreenVertex::ms_decl);
view = 0;
}
else
{
return;
view = 1 + index;
}
ScreenVertex* vertex = reinterpret_cast<ScreenVertex*>(buffer.data);
const uint32_t r = uint32_t(prim->color.r * 255);
const uint32_t g = uint32_t(prim->color.g * 255) << 8;
const uint32_t b = uint32_t(prim->color.b * 255) << 16;
const uint32_t a = uint32_t(prim->color.a * 255) << 24;
UINT32 rgba = r | g | b | a;
vertex[0].m_x = prim->bounds.x0;
vertex[0].m_y = prim->bounds.y0;
vertex[0].m_z = 0;
vertex[0].m_rgba = rgba;
vertex[0].m_u = prim->texcoords.tl.u;
vertex[0].m_v = prim->texcoords.tl.v;
vertex[1].m_x = prim->bounds.x1;
vertex[1].m_y = prim->bounds.y0;
vertex[1].m_z = 0;
vertex[1].m_rgba = rgba;
vertex[1].m_u = prim->texcoords.tr.u;
vertex[1].m_v = prim->texcoords.tr.v;
vertex[2].m_x = prim->bounds.x1;
vertex[2].m_y = prim->bounds.y1;
vertex[2].m_z = 0;
vertex[2].m_rgba = rgba;
vertex[2].m_u = prim->texcoords.br.u;
vertex[2].m_v = prim->texcoords.br.v;
vertex[3].m_x = prim->bounds.x1;
vertex[3].m_y = prim->bounds.y1;
vertex[3].m_z = 0;
vertex[3].m_rgba = rgba;
vertex[3].m_u = prim->texcoords.br.u;
vertex[3].m_v = prim->texcoords.br.v;
vertex[4].m_x = prim->bounds.x0;
vertex[4].m_y = prim->bounds.y1;
vertex[4].m_z = 0;
vertex[4].m_rgba = rgba;
vertex[4].m_u = prim->texcoords.bl.u;
vertex[4].m_v = prim->texcoords.bl.v;
vertex[5].m_x = prim->bounds.x0;
vertex[5].m_y = prim->bounds.y0;
vertex[5].m_z = 0;
vertex[5].m_rgba = rgba;
vertex[5].m_u = prim->texcoords.tl.u;
vertex[5].m_v = prim->texcoords.tl.v;
bgfx::setVertexBuffer(&buffer);
bgfx::setViewClear(view
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
, 0x000000ff
, 1.0f
, 0
);
entry->submit(prim, view, textures, screen_width, screen_height, blend);
m_entries[index]->submit(prim, view, textures, screen_width, screen_height, blend);
}
}

View File

@ -12,6 +12,7 @@
#include "emu.h"
#include <bgfx/bgfx.h>
#include <bx/fpumath.h>
#include "chainentry.h"
@ -20,6 +21,7 @@
#include "target.h"
#include "entryuniform.h"
#include "texturemanager.h"
#include "vertex.h"
bgfx_chain_entry::bgfx_chain_entry(std::string name, bgfx_effect* effect, std::vector<bgfx_input_pair>& inputs, std::vector<bgfx_entry_uniform*> uniforms, bgfx_target* output)
: m_name(name)
@ -47,25 +49,102 @@ bgfx_chain_entry::~bgfx_chain_entry()
void bgfx_chain_entry::submit(render_primitive* prim, int view, texture_manager& textures, uint16_t screen_width, uint16_t screen_height, uint64_t blend)
{
for (bgfx_input_pair input : m_inputs)
{
input.bind(m_effect, textures);
}
if (m_output != nullptr)
{
printf("Setting view to %s, %dx%d\n", m_output->name().c_str(), m_outptu->width, m_output->height());
bgfx::setViewFrameBuffer(view, m_output->target());
bgfx::setViewRect(view, 0, 0, m_output->width(), m_output->height());
}
else
{
printf("Setting view to backbuffer, %dx%d\n", screen_width, screen_height);
bgfx::setViewFrameBuffer(view, BGFX_INVALID_HANDLE);
bgfx::setViewRect(view, 0, 0, screen_width, screen_height);
bgfx::setViewSeq(view, true);
setup_view(view, screen_width, screen_height);
for (bgfx_input_pair input : m_inputs) {
input.bind(m_effect, textures);
}
bgfx::TransientVertexBuffer buffer;
put_screen_buffer(prim, &buffer);
bgfx::setVertexBuffer(&buffer);
for (bgfx_entry_uniform* uniform : m_uniforms)
{
uniform->bind();
}
m_effect->submit(view, blend);
}
void bgfx_chain_entry::setup_view(int view, uint16_t screen_width, uint16_t screen_height)
{
bgfx::FrameBufferHandle handle = BGFX_INVALID_HANDLE;
uint16_t width = screen_width;
uint16_t height = screen_height;
if (m_output != nullptr)
{
handle = m_output->target();
width = m_output->width();
height = m_output->height();
}
bgfx::setViewFrameBuffer(view, handle);
bgfx::setViewRect(view, 0, 0, width, height);
{
float viewMat[16];
bx::mtxIdentity(viewMat);
float projMat[16];
bx::mtxOrtho(projMat, 0.0f, width, height, 0.0f, 0.0f, 100.0f);
bgfx::setViewTransform(view, viewMat, projMat);
}
bgfx::setViewClear(view, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x000000ff, 1.0f, 0);
}
void bgfx_chain_entry::put_screen_buffer(render_primitive* prim, bgfx::TransientVertexBuffer* buffer)
{
if (bgfx::checkAvailTransientVertexBuffer(6, ScreenVertex::ms_decl)) {
bgfx::allocTransientVertexBuffer(buffer, 6, ScreenVertex::ms_decl);
} else {
return;
}
ScreenVertex* vertex = reinterpret_cast<ScreenVertex*>(buffer->data);
vertex[0].m_x = prim->bounds.x0;
vertex[0].m_y = prim->bounds.y0;
vertex[0].m_z = 0;
vertex[0].m_rgba = 0xffffffff;
vertex[0].m_u = prim->texcoords.tl.u;
vertex[0].m_v = prim->texcoords.tl.v;
vertex[1].m_x = prim->bounds.x1;
vertex[1].m_y = prim->bounds.y0;
vertex[1].m_z = 0;
vertex[1].m_rgba = 0xffffffff;
vertex[1].m_u = prim->texcoords.tr.u;
vertex[1].m_v = prim->texcoords.tr.v;
vertex[2].m_x = prim->bounds.x1;
vertex[2].m_y = prim->bounds.y1;
vertex[2].m_z = 0;
vertex[2].m_rgba = 0xffffffff;
vertex[2].m_u = prim->texcoords.br.u;
vertex[2].m_v = prim->texcoords.br.v;
vertex[3].m_x = prim->bounds.x1;
vertex[3].m_y = prim->bounds.y1;
vertex[3].m_z = 0;
vertex[3].m_rgba = 0xffffffff;
vertex[3].m_u = prim->texcoords.br.u;
vertex[3].m_v = prim->texcoords.br.v;
vertex[4].m_x = prim->bounds.x0;
vertex[4].m_y = prim->bounds.y1;
vertex[4].m_z = 0;
vertex[4].m_rgba = 0xffffffff;
vertex[4].m_u = prim->texcoords.bl.u;
vertex[4].m_v = prim->texcoords.bl.v;
vertex[5].m_x = prim->bounds.x0;
vertex[5].m_y = prim->bounds.y0;
vertex[5].m_z = 0;
vertex[5].m_rgba = 0xffffffff;
vertex[5].m_u = prim->texcoords.tl.u;
vertex[5].m_v = prim->texcoords.tl.v;
}

View File

@ -14,6 +14,8 @@
#ifndef __DRAWBGFX_CHAIN_ENTRY__
#define __DRAWBGFX_CHAIN_ENTRY__
#include <bgfx/bgfx.h>
#include <string>
#include <vector>
@ -31,12 +33,15 @@ public:
bgfx_chain_entry(std::string name, bgfx_effect* effect, std::vector<bgfx_input_pair>& inputs, std::vector<bgfx_entry_uniform*> uniforms, bgfx_target* output);
~bgfx_chain_entry();
void submit(render_primitive* prim, int view, texture_manager& textures, uint16_t screen_width, uint16_t screen_height, uint64_t blend = 0L);
void submit(render_primitive* prim, int view, texture_manager& textures, uint16_t screen_width, uint16_t screen_height, uint64_t blend);
// Getters
std::string name() const { return m_name; }
private:
void setup_view(int view, uint16_t screen_width, uint16_t screen_height);
void put_screen_buffer(render_primitive* prim, bgfx::TransientVertexBuffer* buffer);
std::string m_name;
bgfx_effect* m_effect;
std::vector<bgfx_input_pair> m_inputs;

View File

@ -26,6 +26,7 @@ public:
void submit(int view, uint64_t blend = 0L);
bgfx_uniform* uniform(std::string name);
bool has_uniform(std::string name);
private:
uint64_t m_state;

View File

@ -9,8 +9,9 @@ uniform vec4 u_tint;
uniform vec4 u_shift;
SAMPLER2D(s_tex, 0);
uniform vec4 u_texsize;
void main()
{
gl_FragColor = texture2D(s_tex, v_texcoord0 + u_shift.xy) * (u_tint + vec4(0.0, 0.5, 0.0, 0.0)) * v_color0;
gl_FragColor = texture2D(s_tex, v_texcoord0.xy + u_shift.xy * u_texsize.xy) * u_tint * v_color0 + vec4(0.0, 0.0, 0.0, 1.0);
}

View File

@ -11,6 +11,7 @@
#include "inputpair.h"
#include "texture.h"
#include "target.h"
#include "effect.h"
#include "uniform.h"
#include "texturemanager.h"
@ -24,9 +25,13 @@ bgfx_input_pair::bgfx_input_pair(int index, std::string sampler, std::string tex
void bgfx_input_pair::bind(bgfx_effect *effect, texture_manager& textures)
{
printf("Binding texture %s to uniform %s\n", m_texture.c_str(), m_sampler.c_str());
bgfx_uniform *uniform = effect->uniform(m_sampler);
bgfx::UniformHandle u_handle = uniform->handle();
bgfx::TextureHandle t_handle = textures.texture(m_texture)->handle();
bgfx::setTexture(m_index, u_handle, t_handle);
bgfx::setTexture(m_index, effect->uniform(m_sampler)->handle(), textures.texture(m_texture)->handle());
bgfx_uniform *size_uniform = effect->uniform("u_texsize");
if (size_uniform != nullptr)
{
float width = float(textures.texture(m_texture)->width());
float height = float(textures.texture(m_texture)->height());
float size[4] = { 1.0f / width, 1.0f / height, 0.0f, 0.0f };
size_uniform->set(reinterpret_cast<void *>(size), sizeof(float) * 4);
}
}

View File

@ -29,6 +29,5 @@ void bgfx_slider_uniform::bind()
{
values[i] = m_sliders[i]->uniform_value();
}
printf("Setting slider uniform %s to %f, %f, %f, %f (%s)\n", m_uniform->name().c_str(), values[0], values[1], values[2], values[3], m_sliders[0]->name().c_str());
m_uniform->set(values, sizeof(float) * m_sliders.size());
}

View File

@ -9,14 +9,14 @@
#include "target.h"
bgfx_target::bgfx_target(std::string name, bgfx::TextureFormat::Enum format, uint32_t width, uint32_t height, bool filter, uint32_t style)
: bgfx_texture(name, format, width, height, BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP | (filter ? 0 : (BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT | BGFX_TEXTURE_MIP_POINT)), nullptr)
: bgfx_texture(name, format, width, height, BGFX_TEXTURE_RT | BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP | (filter ? 0 : (BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT | BGFX_TEXTURE_MIP_POINT)), nullptr)
, m_style(style)
{
m_target = bgfx::createFrameBuffer(1, &m_handle, false);
}
bgfx_target::bgfx_target(std::string name, uint32_t width, uint32_t height, uint32_t style, void *handle)
: bgfx_texture(name, bgfx::TextureFormat::RGBA8, width, height, BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP | BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT | BGFX_TEXTURE_MIP_POINT, nullptr)
: bgfx_texture(name, bgfx::TextureFormat::RGBA8, width, height, BGFX_TEXTURE_RT | BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP | BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT | BGFX_TEXTURE_MIP_POINT, nullptr)
, m_style(style)
{
m_target = bgfx::createFrameBuffer(handle, width, height);

View File

@ -29,8 +29,7 @@ bgfx_uniform::~bgfx_uniform()
void bgfx_uniform::upload()
{
//if (m_type != bgfx::UniformType::Int1) // Avoid samplers; set separately!
bgfx::setUniform(m_handle, m_data);
bgfx::setUniform(m_handle, m_data);
}
bgfx_uniform* bgfx_uniform::set(float* value)

View File

@ -20,6 +20,5 @@ bgfx_value_uniform::bgfx_value_uniform(bgfx_uniform* uniform, float* values, con
void bgfx_value_uniform::bind()
{
printf("Setting value uniform %s to %f, %f, %f, %f (%s)\n", m_uniform->name().c_str(), m_values[0], m_values[1], m_values[2], m_values[3]);
m_uniform->set(m_values, sizeof(float) * m_count);
}

View File

@ -61,7 +61,7 @@ const uint32_t renderer_bgfx::WHITE_HASH = 0x87654321;
//============================================================
#define GIBBERISH (0)
#define USE_NEW_SHADERS (0)
#define USE_NEW_SHADERS (1)
//============================================================
// INLINES
@ -139,7 +139,6 @@ int renderer_bgfx::create()
m_targets = new target_manager(*m_textures);
m_shaders = new shader_manager();
m_effects = new effect_manager(*m_shaders);
//m_chains = new chain_manager(*m_textures, *m_targets, *m_effects, m_width[window().m_index], m_height[window().m_index]);
if (window().m_index != 0)
{
@ -162,8 +161,11 @@ int renderer_bgfx::create()
m_screen_effect[2] = m_effects->effect("screen_multiply");
m_screen_effect[3] = m_effects->effect("screen_add");
//m_screen_chain = m_chains->chain("test", window().machine());
#if USE_NEW_SHADERS
m_chains = new chain_manager(*m_textures, *m_targets, *m_effects, m_width[window().m_index], m_height[window().m_index]);
m_screen_chain = m_chains->chain("test", window().machine());
m_sliders_dirty = true;
#endif
uint32_t flags = BGFX_TEXTURE_U_CLAMP | BGFX_TEXTURE_V_CLAMP | BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT | BGFX_TEXTURE_MIP_POINT;
m_texture_cache = m_textures->create_texture("#cache", bgfx::TextureFormat::RGBA8, CACHE_SIZE, CACHE_SIZE, nullptr, flags);
@ -181,7 +183,9 @@ int renderer_bgfx::create()
renderer_bgfx::~renderer_bgfx()
{
// Cleanup.
//delete m_chains;
#if USE_NEW_SHADERS
delete m_chains;
#endif
delete m_effects;
delete m_shaders;
delete m_textures;
@ -307,13 +311,13 @@ void renderer_bgfx::render_screen_quad(int view, render_primitive* prim)
texture_flags |= BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT | BGFX_TEXTURE_MIP_POINT;
}
const bgfx::Memory* mem = mame_texture_data_to_bgfx_texture_data(prim->flags & PRIMFLAG_TEXFORMAT_MASK,
prim->texture.width, prim->texture.height, prim->texture.rowpixels, prim->texture.palette, prim->texture.base);
uint16_t tex_width(prim->texture.width);
uint16_t tex_height(prim->texture.height);
bgfx_texture *texture = new bgfx_texture("screen", bgfx::TextureFormat::RGBA8, uint16_t(prim->texture.width), uint16_t(prim->texture.height), mem);
const bgfx::Memory* mem = mame_texture_data_to_bgfx_texture_data(prim->flags & PRIMFLAG_TEXFORMAT_MASK,
tex_width, tex_height, prim->texture.rowpixels, prim->texture.palette, prim->texture.base);
bgfx_texture *texture = new bgfx_texture("screen", bgfx::TextureFormat::RGBA8, tex_width, tex_height, mem);
m_textures->add_texture("screen", texture);
m_targets->update_guest_targets(tex_width, tex_height);
@ -377,10 +381,13 @@ void renderer_bgfx::render_textured_quad(int view, render_primitive* prim, bgfx:
texture_flags |= BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT | BGFX_TEXTURE_MIP_POINT;
}
const bgfx::Memory* mem = mame_texture_data_to_bgfx_texture_data(prim->flags & PRIMFLAG_TEXFORMAT_MASK,
prim->texture.width, prim->texture.height, prim->texture.rowpixels, prim->texture.palette, prim->texture.base);
uint16_t tex_width(prim->texture.width);
uint16_t tex_height(prim->texture.height);
bgfx::TextureHandle texture = bgfx::createTexture2D(uint16_t(prim->texture.width), uint16_t(prim->texture.height), 1, bgfx::TextureFormat::RGBA8, texture_flags, mem);
const bgfx::Memory* mem = mame_texture_data_to_bgfx_texture_data(prim->flags & PRIMFLAG_TEXFORMAT_MASK,
tex_width, tex_height, prim->texture.rowpixels, prim->texture.palette, prim->texture.base);
bgfx::TextureHandle texture = bgfx::createTexture2D(tex_width, tex_height, 1, bgfx::TextureFormat::RGBA8, texture_flags, mem);
bgfx_effect** effects = PRIMFLAG_GET_SCREENTEX(prim->flags) ? m_screen_effect : m_gui_effect;
@ -928,7 +935,7 @@ renderer_bgfx::buffer_status renderer_bgfx::buffer_primitives(int view, bool atl
render_textured_quad(view, *prim, buffer);
}
#else
render_textured_quad(view, *prim, buffer);
render_textured_quad(*view, *prim, buffer);
#endif
return BUFFER_EMPTY;
}