Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vs49688 committed Sep 28, 2024
1 parent a37311a commit bcc19e3
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cmake/h2inc.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function(make_h2inc TARGET SRC DST)
set(mkdrv "${CMAKE_SOURCE_DIR}/contrib/mkdrv.pl")
set(mkdrv "${CMAKE_CURRENT_LIST_DIR}/../../contrib/mkdrv.pl")
set(target_includes "$<TARGET_PROPERTY:${TARGET},INCLUDE_DIRECTORIES>")

add_custom_command(
Expand Down
93 changes: 88 additions & 5 deletions drivers/glrend/brender.common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#define UV_SOURCE_ENV_I 2

#define DEBUG_DISABLE_LIGHTS 0
#define DEBUG_DISABLE_LIGHT_AMBIENT 0
#define DEBUG_DISABLE_LIGHT_AMBIENT 1
#define DEBUG_DISABLE_LIGHT_DIRECTIONAL 0
#define DEBUG_DISABLE_LIGHT_POINT 0
#define DEBUG_DISABLE_LIGHT_POINTATTEN 0
#define DEBUG_DISABLE_LIGHT_SPOT 1
#define DEBUG_DISABLE_LIGHT_SPECULAR 0
#define ENABLE_GOURAUD 1
#define ENABLE_PHONG 0
#define DEBUG_DISABLE_LIGHT_SPECULAR 1
#define ENABLE_GOURAUD 0
#define ENABLE_PHONG 1
#define ENABLE_PSX_SIMULATION 0

struct br_light
Expand All @@ -29,13 +29,22 @@ struct br_light
vec4 colour; /* (R, G, B, 0), normalised */
vec4 iclq; /* (intensity, constant, linear, attenutation) */
vec2 spot_angles; /* (inner, outer), if (0.0, 0.0), then this is a point light. */
float falloff;
float cutoff;
float spot_falloff;
};

layout(std140) uniform br_scene_state
{
vec4 eye_view; /* Eye position in view-space */
br_light lights[MAX_LIGHTS];
uint num_lights;
uint use_ambient_colour;
float ambient_red;
float ambient_green;
float ambient_blue;
uint use_ambient_intensity;
float ambient_intensity;
br_light lights[MAX_LIGHTS];
};

layout(std140) uniform br_model_state
Expand Down Expand Up @@ -81,8 +90,18 @@ float shadingFilter(in float i)
return i;
}



vec4 _SpecColour = vec4(1.0);
float _Shininess = 64.0;

float calculateAttenuation(in br_light alp, in float dist)
{
float c = alp.iclq.y;
float l = alp.iclq.z;
float q = alp.iclq.w;

return 1.0 / (c + (l * dist) + (q * dist * dist));
if (dist > alp.iclq.w)
return 0.0;

Expand All @@ -96,14 +115,78 @@ float calculateAttenuation(in br_light alp, in float dist)
return 1.0 - attn;
}

float calculateSpecularPhong(in vec3 lightDir, in vec3 normal)
{
vec3 viewDir = normalize(-vFragPos);
vec3 reflectDir = reflect(-lightDir, normal);

return pow(max(dot(viewDir, reflectDir), 0.0), _Shininess);
}

float calculateSpecularBlinnPhong(in vec3 lightDir, in vec3 normal)
{
vec3 viewDir = normalize(-vFragPos);
vec3 halfwayDir = normalize(lightDir + viewDir);

return pow(max(dot(normal, halfwayDir), 0.0), _Shininess * 4);
}

float calculateSpecular(in vec3 lightDir, in vec3 normal)
{
return calculateSpecularBlinnPhong(lightDir, normal);
}


vec3 lightingColourAmbient(in vec4 p, in vec4 n, in br_light alp)
{
return ka * alp.iclq.x * alp.colour.xyz;
}


vec3 calculateASD(in br_light alp, in vec3 lightDir, in vec3 normal, in vec3 surfaceColour)
{
vec3 lightColour = alp.colour.xyz;
float intensity = alp.iclq.x;

vec3 ambient = ka * lightColour * surfaceColour;

/* Do diffuse. */
float _dot = max(dot(normal, lightDir), 0.0);
vec3 diffuse = kd * _dot * lightColour * surfaceColour;

/* Only do specular if the light's actually hitting the surface. */
vec3 specular = vec3(0.0);
if(_dot > 0)
specular = ks * calculateSpecular(lightDir, normal) * lightColour * _SpecColour.xyz;

return (ambient + diffuse + specular) * intensity;
}

vec3 lightDirect(in br_light alp, in vec3 normal, in vec3 surfaceColour)
{
vec3 lightDir = -alp.direction.xyz;
return calculateASD(alp, lightDir, normal, surfaceColour);
}


vec3 lightPoint(in br_light alp, in vec3 normal, in vec3 surfaceColour)
{
vec3 lightDir = alp.position.xyz - vFragPos;
float dist = length(lightDir);
lightDir = normalize(lightDir);

vec3 colour = calculateASD(i, lightDir, normal, surfaceColour);
float atten = calculateAttenuation(alp.iclq.yzw, dist);

return colour * atten * alp.iclq.x;
}

vec3 lightingColourDirect(in vec4 p, in vec4 n, in br_light alp)
{
return lightDirect(alp, n.xyz, vec3(1, 1, 1));
//vec3 lightDir = -alp.direction.xyz;


/* Notes: '_dot' is 'intensity' */
float _dot = max(dot(n, alp.direction), 0.0) * kd;

Expand Down
7 changes: 5 additions & 2 deletions drivers/glrend/brender.frag.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#version 150

#include "brender.common.glsl"
#include "common.glsl"
#define vFragPos (position.xyz)

in vec4 position;
in vec2 uv;
Expand All @@ -16,6 +15,10 @@ out vec4 mainColour;
uniform sampler2D main_texture;
uniform usampler2D index_texture;


#include "brender.common.glsl"
#include "common.glsl"

vec3 adjustBrightness(in vec3 colour, in float brightness)
{
return colour + brightness;
Expand Down
6 changes: 5 additions & 1 deletion drivers/glrend/brender.vert.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#version 150

#include "brender.common.glsl"
#define vFragPos (position.xyz)


in vec3 aPosition;
in vec2 aUV;
Expand All @@ -15,6 +16,9 @@ out vec4 colour;
out vec3 rawPosition;
out vec3 rawNormal;

#include "brender.common.glsl"


#if ENABLE_PSX_SIMULATION
vec4 PSXify_pos(in vec4 vertex, in vec2 resolution)
{
Expand Down
96 changes: 86 additions & 10 deletions drivers/glrend/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,98 @@
#include "shortcut.h"
#include "vecifns.h"

/*
** Process each light, doing as much once-per-frame work as possible.
** - For work that cannot be done here, see GLSTATE_ProcessActiveLights()
*/
/* softrend/setup.c, from ActiveLightsUpdate() */
static void accumulate_ambient(shader_data_scene *scache, const state_light *lights)
{
br_boolean found_ambient = BR_FALSE;

scache->ambient_red = BR_SCALAR(0.0);
scache->ambient_green = BR_SCALAR(0.0);
scache->ambient_blue = BR_SCALAR(0.0);
scache->ambient_intensity = BR_SCALAR(0.0);

for(int i = 0; i < MAX_STATE_LIGHTS; ++i) {
const state_light *lp = lights + i;
br_scalar intensity;

if(lp->type != BRT_AMBIENT || lp->attenuation_type == BRT_RADII)
continue;

found_ambient = BR_TRUE;

if(lp->culled)
continue;

intensity = BR_RCP(lp->attenuation_c);

scache->ambient_red += BR_MUL(BrFixedToScalar(BR_RED(lp->colour) << 8), intensity);
scache->ambient_green += BR_MUL(BrFixedToScalar(BR_GRN(lp->colour) << 8), intensity);
scache->ambient_blue += BR_MUL(BrFixedToScalar(BR_BLU(lp->colour) << 8), intensity);
scache->ambient_intensity += intensity;
}

if(found_ambient) {
if(scache->ambient_red > BR_SCALAR(1.0))
scache->ambient_red = BR_SCALAR(1.0);

if(scache->ambient_green > BR_SCALAR(1.0))
scache->ambient_green = BR_SCALAR(1.0);

if(scache->ambient_blue > BR_SCALAR(1.0))
scache->ambient_blue = BR_SCALAR(1.0);

scache->use_ambient_colour = scache->ambient_red != BR_SCALAR(1.0) || scache->ambient_green != BR_SCALAR(1.0) ||
scache->ambient_blue != BR_SCALAR(1.0);

if(scache->ambient_intensity > BR_SCALAR(1.0))
scache->ambient_intensity = BR_SCALAR(1.0);

scache->use_ambient_intensity = scache->ambient_intensity != BR_SCALAR(1.0);

} else {
scache->use_ambient_colour = BR_FALSE;
scache->use_ambient_intensity = BR_FALSE;
}
}

/**
* \brief Process each light, doing as much once-per-frame work as possible.
*
* \remark BRender does support model/view-space lights. We only support view-space because
* there's no benefit to model-space. Luckily, the position/direction is always transformed
* in to view-space anyway, so we can just (ab)use that.
*
* \remark See BrSetupLighs() in `core/v1db/enables.c` for reference.
*/
static void ProcessSceneLights(state_cache *cache, const state_light *lights)
{
accumulate_ambient(&cache->scene, lights);

cache->scene.num_lights = 0;
for(uint32_t i = 0; i < MAX_STATE_LIGHTS; ++i) {
const state_light *light = lights + i;
const state_light *light = lights + i;
shader_data_light *alp = cache->scene.lights + cache->scene.num_lights;
br_token type = light->type & BR_LIGHT_TYPE;
float intensity = 16384.0f;

if(light->type == BRT_NONE)
if(type == BRT_NONE || light->culled)
continue;

shader_data_light *alp = cache->scene.lights + cache->scene.num_lights;

/* See enables.c:194, BrSetupLights(). All the lights are already converted into view space. */
BrVector4Set(&alp->position, light->position.v[0], light->position.v[1], light->position.v[2],
light->type == BRT_DIRECT ? 0.0f : 1.0f);

BrVector4Set(&alp->direction, light->direction.v[0], light->direction.v[1], light->direction.v[2], 0.0f);

float intensity = 16384.0f; /* Effectively infinite */
if(light->attenuation_c != 0)
if(light->attenuation_c != BR_SCALAR(0))
intensity = BR_RCP(light->attenuation_c);

if(light->type == BRT_DIRECT) {
/*
* Work out a unit half vector:
* eye = (0,0,1)
* half = normalise(light_direection + eye)
*/
BrVector4Copy(&alp->half, &alp->direction);
alp->half.v[2] += 1.0f;
BrVector4Normalise(&alp->half, &alp->half);
Expand All @@ -41,11 +107,21 @@ static void ProcessSceneLights(state_cache *cache, const state_light *lights)
BrVector4Set(&alp->colour, BR_RED(light->colour) / 255.0f, BR_GRN(light->colour) / 255.0f,
BR_BLU(light->colour) / 255.0f, light->type == BRT_AMBIENT ? 1.0f : 0.0f);

if(light->attenuation_type == BRT_RADII) {
if(light->radius_inner != light->radius_outer)
alp->falloff = BR_DIV(alp->iclq.v[0], light->radius_inner - light->radius_outer);

alp->cutoff = BR_SQR(light->radius_outer);
}

if(light->type == BRT_SPOT) {
BrVector2Set(&alp->spot_angles, BrAngleToRadian(light->spot_inner), BrAngleToRadian(light->spot_outer));
alp->spot_falloff = BR_RCP(light->spot_outer - light->spot_inner);
} else {
BrVector2Set(&alp->spot_angles, 0, 0);
alp->spot_falloff = 0.0f;
}

++alp;
++cache->scene.num_lights;
}
Expand Down
16 changes: 14 additions & 2 deletions drivers/glrend/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,28 @@ typedef struct shader_data_light {
alignas(16) br_vector4 iclq;
/* (inner, outer), if (0.0, 0.0), then this is a point light. */
alignas(16) br_vector2 spot_angles;
alignas(4) br_float _pad0, _pad1;

alignas(4) br_float falloff;
alignas(4) br_float cutoff;
alignas(4) br_float spot_falloff;

/* Pad out the structure to maintain alignment. */
alignas(4) float _pad0, _pad1;
alignas(4) float _pad2;
} shader_data_light;
BR_STATIC_ASSERT(sizeof(shader_data_light) % 16 == 0, "shader_data_light is not aligned");

typedef struct shader_data_scene {
alignas(16) br_vector4 eye_view;
alignas(16) shader_data_light lights[BR_MAX_LIGHTS];
alignas(4) uint32_t num_lights;
alignas(4) uint32_t use_ambient_colour;
alignas(4) float ambient_red;
alignas(4) float ambient_green;
alignas(4) float ambient_blue;
alignas(4) br_boolean use_ambient_intensity;
alignas(4) float ambient_intensity;
alignas(4) float _pad0;
alignas(16) shader_data_light lights[BR_MAX_LIGHTS];
} shader_data_scene;
BR_STATIC_ASSERT(sizeof(((shader_data_scene *)NULL)->lights) == sizeof(shader_data_light) * BR_MAX_LIGHTS,
"std::array<shader_data_light> fucked up");
Expand Down
2 changes: 1 addition & 1 deletion examples/brdemo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ add_library(brdemo STATIC
brdemo.c
)

target_link_libraries(brdemo PUBLIC BRender::Full)
target_link_libraries(brdemo PUBLIC BRender::Full BRender::DDI)
target_include_directories(brdemo PUBLIC .)
set_property(TARGET brdemo PROPERTY FOLDER "BRender/Examples")
7 changes: 7 additions & 0 deletions examples/brdemo/brdemo.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@

#include "brdemo.h"

struct br_device * BR_EXPORT BrDrv1SoftPrimBegin(const char *arguments);
struct br_device * BR_EXPORT BrDrv1SoftRendBegin(const char *arguments);


/* begin hook */
void _BrBeginHook(void) // NOLINT(*-reserved-identifier)
{
BrDevAddStatic(NULL, BrDrv1SDL2Begin, NULL);
BrDevAddStatic(NULL, BrDrv1GLBegin, NULL);

BrDevAddStatic(NULL, BrDrv1SoftPrimBegin, NULL);
BrDevAddStatic(NULL, BrDrv1SoftRendBegin, NULL);
}

/* end hook */
Expand Down
5 changes: 5 additions & 0 deletions examples/brintro/intro.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ br_intro *BrIntroCreate(void)
BrMatrix34Translate(&state->camera->t.t.mat, BR_SCALAR(0), BR_SCALAR(0), BR_SCALAR(2));
BrMatrix34RotateY(&state->light->t.t.mat, BR_ANGLE_DEG(-45));
BrMatrix34PostRotateZ(&state->light->t.t.mat, BR_ANGLE_DEG(45));

{
br_light *lightdata = state->light->type_data;
lightdata->type = BR_LIGHT_DIRECT | BR_LIGHT_VIEW;
}
BrLightEnable(state->light);

BrIntroReset(state);
Expand Down
Binary file added examples/dat/shit.pix
Binary file not shown.
Loading

0 comments on commit bcc19e3

Please sign in to comment.