This commit is contained in:
Михаил Капелько committed 2026-10-22 16:25:06 +03:00
1 parent 01594353d8
commit 96a5684709
12 files changed
+262 -250

No files matched your search

+24
View File
@@ -26,6 +26,30 @@ enum RenderIOS {
static let renderVM = RenderVM()
}
// The GLSL sources live in renderConstShader.swift (shared with the macOS
// build, with the platform #version already substituted). Expose them to the
// ObjC++ bridge as C strings; each call returns a malloc'd copy the caller
// must free().
@_cdecl("rcios_shader_main_vs")
public func rciosShaderMainVS() -> UnsafeMutablePointer<CChar>? {
strdup(RENDER_SHADER_MAIN_VS)
}
@_cdecl("rcios_shader_main_fs")
public func rciosShaderMainFS() -> UnsafeMutablePointer<CChar>? {
strdup(RENDER_SHADER_MAIN_FS)
}
@_cdecl("rcios_shader_shadow_vs")
public func rciosShaderShadowVS() -> UnsafeMutablePointer<CChar>? {
strdup(RENDER_SHADER_SHADOW_VS)
}
@_cdecl("rcios_shader_shadow_fs")
public func rciosShaderShadowFS() -> UnsafeMutablePointer<CChar>? {
strdup(RENDER_SHADER_SHADOW_FS)
}
@_cdecl("rcios_present_overlay")
public func rciosPresentOverlay() {
otherSetupConsoleLogging(RenderIOS.chkgfx.ctrl, "Chkgfx")
+24 -12
View File
@@ -1,11 +1,11 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <stdlib.h>
#include <string>
#include "raylib.h"
#include "raybri.h"
#include "renderShaders.h"
// iOS entry points. The prebuilt raylib.framework (ghera/raylib-iOS) owns
// main()/AppDelegate/SceneDelegate and a full-screen EGL/ANGLE window; it
@@ -22,14 +22,26 @@ extern "C" void rcios_present_overlay(void); // implemented in Swift (RenderIOS.
void rcios_present_overlay(void);
#endif
// The shader templates in renderShaders.c start with %VERSION%; substitute the
// GLSL ES version directive (which must be the first token of the shader).
static std::string sr_shader(const char *template_source)
// The GLSL sources live in renderConstShader.swift (shared with the macOS
// build, with the platform #version already substituted there). These Swift
// accessors return malloc'd copies that sr_shader() releases.
#if defined(__cplusplus)
extern "C" {
#endif
char *rcios_shader_main_vs(void);
char *rcios_shader_main_fs(void);
char *rcios_shader_shadow_vs(void);
char *rcios_shader_shadow_fs(void);
#if defined(__cplusplus)
}
#endif
static std::string sr_shader(char *(*accessor)(void))
{
NSString *replaced = [[NSString stringWithUTF8String:template_source]
stringByReplacingOccurrencesOfString:[NSString stringWithUTF8String:RENDER_SHADER_VERSION_TOKEN]
withString:[NSString stringWithUTF8String:RENDER_SHADER_VERSION_ES]];
return std::string([replaced UTF8String]);
char *source = accessor();
std::string shader = source ? source : "";
free(source);
return shader;
}
static double sr_start_time = 0.0;
@@ -49,10 +61,10 @@ extern "C" void ios_ready(void)
// On iOS raylib's InitWindow loaded the GL functions and called rlglInit,
// so raybri_init() must not repeat it (see raybri.c, RAYBRI_PLATFORM_IOS).
std::string main_vs = sr_shader(RENDER_SHADER_MAIN_VS_T);
std::string main_fs = sr_shader(RENDER_SHADER_MAIN_FS_T);
std::string shadow_vs = sr_shader(RENDER_SHADER_SHADOW_VS_T);
std::string shadow_fs = sr_shader(RENDER_SHADER_SHADOW_FS_T);
std::string main_vs = sr_shader(rcios_shader_main_vs);
std::string main_fs = sr_shader(rcios_shader_main_fs);
std::string shadow_vs = sr_shader(rcios_shader_shadow_vs);
std::string shadow_fs = sr_shader(rcios_shader_shadow_fs);
raybri_init(
main_vs.c_str(),
main_fs.c_str(),
+201 -17
View File
@@ -1,21 +1,205 @@
import RaylibBridge
// GLSL sources shared by the macOS (GLSL 150) and iOS (GLSL ES 300)
// builds. A shader's #version directive must precede everything else (even
// "precision"), so it cannot be chosen with an #ifdef inside the GLSL; the
// templates below start with RENDER_SHADER_VERSION_TOKEN and the platform
// version is substituted with replacingOccurrences.
//
// The fragment templates carry "precision highp float;" because GLSL ES has
// no default float precision in the fragment stage; desktop GLSL 1.30+
// accepts and ignores it. GLSL ES vertex shaders default to highp, so the
// vertex templates need no precision statement.
// The GLSL bodies live in components/raybri/renderShaders.c so the macOS and
// iOS builds share a single copy. Only the #version directive differs, and it
// is substituted here (it must be the first token, so it cannot be selected by
// a preprocessor conditional inside the shader).
let RENDER_SHADER_VERSION_TOKEN = "%VERSION%"
let RENDER_SHADER_VERSION_GL = "#version 150"
let RENDER_SHADER_VERSION_ES = "#version 300 es"
let RENDER_SHADER_MAIN_FS = renderShaderSource(RENDER_SHADER_MAIN_FS_T, RENDER_SHADER_VERSION_GL)
let RENDER_SHADER_MAIN_VS = renderShaderSource(RENDER_SHADER_MAIN_VS_T, RENDER_SHADER_VERSION_GL)
let RENDER_SHADER_SHADOW_FS = renderShaderSource(RENDER_SHADER_SHADOW_FS_T, RENDER_SHADER_VERSION_GL)
let RENDER_SHADER_SHADOW_VS = renderShaderSource(RENDER_SHADER_SHADOW_VS_T, RENDER_SHADER_VERSION_GL)
#if os(iOS)
let RENDER_SHADER_VERSION = RENDER_SHADER_VERSION_ES
#else
let RENDER_SHADER_VERSION = RENDER_SHADER_VERSION_GL
#endif
private func renderShaderSource(
_ template: UnsafePointer<CChar>,
_ version: UnsafePointer<CChar>
) -> String {
String(cString: template).replacingOccurrences(
of: RENDER_SHADER_VERSION_TOKEN,
with: String(cString: version)
)
let RENDER_SHADER_MAIN_FS_T = """
%VERSION%
precision highp float;
in vec3 vWorldPos;
in vec3 vNormal;
in vec2 vTexCoord;
in vec4 vLightClip;
uniform sampler2D texture0;
uniform sampler2D u_normalMap;
uniform float u_normalStrength;
uniform sampler2D u_shadowMap;
uniform vec3 u_lightPos;
uniform vec3 u_lightColor;
uniform vec3 u_ambient;
uniform vec3 u_albedoColor;
uniform float u_lightEnabled;
uniform float u_emissive;
uniform float u_shadowBias;
uniform float u_shadowSoftness;
uniform float u_isGround;
uniform vec3 u_gridColor;
uniform vec3 u_cameraPos;
uniform vec3 u_fogColor;
uniform float u_fogStart;
uniform float u_fogEnd;
out vec4 finalColor;
bool isPixelInShadowMap(vec3 proj)
{
return proj.x >= 0.0 && proj.x <= 1.0 &&
proj.y >= 0.0 && proj.y <= 1.0 &&
proj.z >= 0.0 && proj.z <= 1.0;
}
float shadowFactor()
{
vec3 proj = vLightClip.xyz / vLightClip.w;
proj = proj * 0.5 + 0.5;
if (!isPixelInShadowMap(proj)) return 0.0;
int radius = int(u_shadowSoftness);
vec2 texel = 1.0 / vec2(textureSize(u_shadowMap, 0));
float sum = 0.0;
float count = 0.0;
for (int x = -radius; x <= radius; x++)
{
for (int y = -radius; y <= radius; y++)
{
float d = texture(u_shadowMap, proj.xy + vec2(float(x), float(y)) * texel).r;
sum += (proj.z - u_shadowBias > d) ? 1.0 : 0.0;
count += 1.0;
}
}
return sum / count;
}
mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)
{
vec3 dp1 = dFdx(p);
vec3 dp2 = dFdy(p);
vec2 duv1 = dFdx(uv);
vec2 duv2 = dFdy(uv);
vec3 dp2perp = cross(dp2, N);
vec3 dp1perp = cross(N, dp1);
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
float invmax = inversesqrt(max(dot(T, T), dot(B, B)));
return mat3(T * invmax, B * invmax, N);
}
void main()
{
vec4 texel = texture(texture0, vTexCoord);
vec3 albedo = texel.rgb * u_albedoColor;
vec3 N = normalize(vNormal);
if (u_normalStrength > 0.0)
{
vec3 nm = texture(u_normalMap, vTexCoord).rgb * 2.0 - 1.0;
nm.xy *= u_normalStrength;
N = normalize(cotangentFrame(N, vWorldPos, vTexCoord) * nm);
}
if (u_isGround > 0.5)
{
vec2 uv = vWorldPos.xz;
vec2 gg = abs(fract(uv - 0.5) - 0.5) / fwidth(uv);
float line = min(gg.x, gg.y);
float grid = 1.0 - min(line, 1.0);
albedo = mix(albedo, u_gridColor, grid * 0.7);
}
vec3 color = albedo * u_ambient;
if (u_lightEnabled > 0.5)
{
vec3 toLight = u_lightPos - vWorldPos;
float dist = length(toLight);
vec3 L = toLight / max(dist, 0.0001);
float diff = max(dot(N, L), 0.0);
float attenuation = 1.0 / (1.0 + 0.14 * dist + 0.07 * dist * dist);
float shadow = shadowFactor();
color += albedo * u_lightColor * diff * attenuation * (1.0 - shadow);
}
color += albedo * u_emissive;
float fogDist = length(u_cameraPos - vWorldPos);
float fog = clamp((fogDist - u_fogStart) / (u_fogEnd - u_fogStart), 0.0, 1.0);
color = mix(color, u_fogColor, fog);
finalColor = vec4(color, 1.0);
}
"""
let RENDER_SHADER_MAIN_FS = RENDER_SHADER_MAIN_FS_T.replacingOccurrences(
of: RENDER_SHADER_VERSION_TOKEN,
with: RENDER_SHADER_VERSION
)
let RENDER_SHADER_MAIN_VS_T = """
%VERSION%
in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
uniform mat4 mvp;
uniform mat4 matModel;
uniform mat4 u_lightMVP;
out vec3 vWorldPos;
out vec3 vNormal;
out vec2 vTexCoord;
out vec4 vLightClip;
void main()
{
vec4 worldPos = matModel * vec4(vertexPosition, 1.0);
vWorldPos = worldPos.xyz;
vNormal = normalize(mat3(matModel) * vertexNormal);
vTexCoord = vertexTexCoord;
vLightClip = u_lightMVP * vec4(vertexPosition, 1.0);
gl_Position = mvp * vec4(vertexPosition, 1.0);
}
"""
let RENDER_SHADER_MAIN_VS = RENDER_SHADER_MAIN_VS_T.replacingOccurrences(
of: RENDER_SHADER_VERSION_TOKEN,
with: RENDER_SHADER_VERSION
)
let RENDER_SHADER_SHADOW_FS_T = """
%VERSION%
precision highp float;
out vec4 finalColor;
void main()
{
finalColor = vec4(1.0);
}
"""
let RENDER_SHADER_SHADOW_FS = RENDER_SHADER_SHADOW_FS_T.replacingOccurrences(
of: RENDER_SHADER_VERSION_TOKEN,
with: RENDER_SHADER_VERSION
)
let RENDER_SHADER_SHADOW_VS_T = """
%VERSION%
in vec3 vertexPosition;
uniform mat4 mvp;
void main()
{
gl_Position = mvp * vec4(vertexPosition, 1.0);
}
"""
let RENDER_SHADER_SHADOW_VS = RENDER_SHADER_SHADOW_VS_T.replacingOccurrences(
of: RENDER_SHADER_VERSION_TOKEN,
with: RENDER_SHADER_VERSION
)