Skip to content

Commit

Permalink
Add ios libraries + blit shader
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhonnyg committed Oct 28, 2024
1 parent 0d5f0e0 commit 78483a4
Show file tree
Hide file tree
Showing 21 changed files with 113 additions and 58 deletions.
6 changes: 2 additions & 4 deletions defold-rive/assets/rivemodel.material
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
name: "rive-test"
tags: "rive"
vertex_program: "/defold-rive/assets/rivemodel_blit.vp"
fragment_program: "/defold-rive/assets/rivemodel_blit.fp"
vertex_program: "/defold-rive/assets/shader-library/rivemodel_blit.vp"
fragment_program: "/defold-rive/assets/shader-library/rivemodel_blit.fp"
vertex_constants {
name: "world_view_proj"
type: CONSTANT_TYPE_WORLDVIEWPROJ
value {
}
}
fragment_constants {
name: "colors"
Expand Down
4 changes: 2 additions & 2 deletions defold-rive/assets/rivemodel_blit.material
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: "rive-test"
tags: "rive"
vertex_program: "/defold-rive/assets/rivemodel_blit.vp"
fragment_program: "/defold-rive/assets/rivemodel_blit.fp"
vertex_program: "/defold-rive/assets/shader-library/rivemodel_blit.vp"
fragment_program: "/defold-rive/assets/shader-library/rivemodel_blit.fp"
samplers {
name: "texture_sampler"
wrap_u: WRAP_MODE_REPEAT
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions defold-rive/include/private/shaders/rivemodel_blit.fpc.gen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Generated with bob.jar version: 1.9.5 sha1: 585dc52e90385ffed13846003d7aae4be51f2776 built: 2024-10-28 12:43:12
// Generated, do not edit!
#include <stdint.h>
extern "C" uint8_t RIVEMODEL_BLIT_FPC[];
extern "C" uint32_t RIVEMODEL_BLIT_FPC_SIZE;
5 changes: 5 additions & 0 deletions defold-rive/include/private/shaders/rivemodel_blit.vpc.gen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Generated with bob.jar version: 1.9.5 sha1: 585dc52e90385ffed13846003d7aae4be51f2776 built: 2024-10-28 12:43:12
// Generated, do not edit!
#include <stdint.h>
extern "C" uint8_t RIVEMODEL_BLIT_VPC[];
extern "C" uint32_t RIVEMODEL_BLIT_VPC_SIZE;
3 changes: 2 additions & 1 deletion defold-rive/include/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ namespace dmRive
rive::Mat2D GetViewTransform(HRenderContext context, dmRender::HRenderContext render_context);
rive::Mat2D GetViewProjectionTransform(HRenderContext context, dmRender::HRenderContext render_context);
void GetDimensions(HRenderContext context, uint32_t* width, uint32_t* height);
void RenderBegin(HRenderContext context, ShaderResources* shaders, dmResource::HFactory factory);
void RenderBegin(HRenderContext context, dmResource::HFactory factory);
void RenderEnd(HRenderContext context);
dmResource::Result LoadShaders(dmResource::HFactory factory, ShaderResources** resources);
void ReleaseShaders(dmResource::HFactory factory, ShaderResources** resources);

dmRender::HMaterial GetBlitToBackBufferMaterial(HRenderContext context, dmRender::HRenderContext render_context);
dmGraphics::HTexture GetBackingTexture(HRenderContext context);
}

Expand Down
Binary file modified defold-rive/lib/arm64-ios/libharfbuzz.a
Binary file not shown.
Binary file modified defold-rive/lib/arm64-ios/librive.a
Binary file not shown.
Binary file modified defold-rive/lib/arm64-ios/librive_renderer.a
Binary file not shown.
Binary file modified defold-rive/lib/arm64-ios/librivetess.a
Binary file not shown.
Binary file modified defold-rive/lib/arm64-ios/libsheenbidi.a
Binary file not shown.
Binary file modified defold-rive/lib/arm64-ios/libtess2.a
Binary file not shown.
Binary file modified defold-rive/lib/arm64-ios/libyoga.a
Binary file not shown.
Binary file added defold-rive/lib/js-web/libriveshaders.a
Binary file not shown.
Binary file modified defold-rive/lib/wasm-web/libRiveExt_15.a
Binary file not shown.
Binary file added defold-rive/lib/wasm-web/libriveshaders.a
Binary file not shown.
22 changes: 11 additions & 11 deletions defold-rive/src/comp_rive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,9 @@ namespace dmRive
dmObjectPool<RiveComponent*> m_Components;
dmArray<dmRender::RenderObject> m_RenderObjects;
dmArray<dmRender::HNamedConstantBuffer> m_RenderConstants; // 1:1 mapping with the render objects

dmRender::HMaterial m_BlitToBackbufferMaterial;
dmGraphics::HVertexBuffer m_BlitToBackbufferVertexBuffer;

uint8_t m_BlitToBackbuffer : 1;
uint8_t m_FlipY : 1;
dmGraphics::HVertexBuffer m_BlitToBackbufferVertexBuffer;
uint8_t m_BlitToBackbuffer : 1;
uint8_t m_FlipY : 1;
};

dmGameObject::CreateResult CompRiveNewWorld(const dmGameObject::ComponentNewWorldParams& params)
Expand Down Expand Up @@ -193,6 +190,8 @@ namespace dmRive
{
RiveWorld* world = (RiveWorld*)params.m_World;

dmGraphics::DeleteVertexBuffer(world->m_BlitToBackbufferVertexBuffer);

dmResource::UnregisterResourceReloadedCallback(((CompRiveContext*)params.m_Context)->m_Factory, ResourceReloadedCallback, world);

delete world;
Expand Down Expand Up @@ -432,7 +431,7 @@ namespace dmRive
dmRender::RenderObject& ro = *world->m_RenderObjects.End();
world->m_RenderObjects.SetSize(world->m_RenderObjects.Size()+1);
ro.Init();
ro.m_Material = world->m_BlitToBackbufferMaterial;
ro.m_Material = GetBlitToBackBufferMaterial(world->m_RiveRenderContext, render_context);
ro.m_VertexDeclaration = dmRender::GetVertexDeclaration(ro.m_Material);
ro.m_VertexBuffer = world->m_BlitToBackbufferVertexBuffer;
ro.m_PrimitiveType = dmGraphics::PRIMITIVE_TRIANGLES;
Expand All @@ -451,7 +450,7 @@ namespace dmRive
dmRive::RiveSceneData* data = (dmRive::RiveSceneData*) scene_res->m_Scene;
world->m_RiveRenderContext = data->m_RiveRenderContext;

RenderBegin(world->m_RiveRenderContext, scene_res->m_Shaders, world->m_Ctx->m_Factory);
RenderBegin(world->m_RiveRenderContext, world->m_Ctx->m_Factory);

uint32_t width, height;
GetDimensions(world->m_RiveRenderContext, &width, &height);
Expand All @@ -466,8 +465,6 @@ namespace dmRive
if (!c->m_Enabled || !c->m_AddedToUpdate)
continue;

world->m_BlitToBackbufferMaterial = GetMaterial(c, c->m_Resource);

rive::Mat2D transform;
Mat4ToMat2D(c->m_World, transform);

Expand Down Expand Up @@ -1289,7 +1286,10 @@ namespace dmRive
rivectx->m_RenderContext = *(dmRender::HRenderContext*)ctx->m_Contexts.Get(dmHashString64("render"));
rivectx->m_MaxInstanceCount = dmConfigFile::GetInt(ctx->m_Config, "rive.max_instance_count", 128);

g_DisplayFactor = dmGraphics::GetWindowWidth(rivectx->m_GraphicsContext) / dmGraphics::GetWidth(rivectx->m_GraphicsContext);
float scale_factor_width = (float) dmGraphics::GetWindowWidth(rivectx->m_GraphicsContext) / (float) dmGraphics::GetWidth(rivectx->m_GraphicsContext);
float scale_factor_height = (float) dmGraphics::GetWindowHeight(rivectx->m_GraphicsContext) / (float) dmGraphics::GetHeight(rivectx->m_GraphicsContext);
float scale_factor_engine = dmGraphics::GetDisplayScaleFactor(rivectx->m_GraphicsContext);
g_DisplayFactor = dmMath::Max(scale_factor_engine, dmMath::Max(scale_factor_width, scale_factor_height));

dmLogInfo("Display Factor: %g", g_DisplayFactor);

Expand Down
79 changes: 60 additions & 19 deletions defold-rive/src/private/renderer_private.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <rive/renderer/rive_render_image.hpp>

#include <private/defold_graphics.h>
#include <private/shaders/rivemodel_blit.vpc.gen.h>
#include <private/shaders/rivemodel_blit.fpc.gen.h>

#include <common/vertices.h>
#include <renderer.h>
Expand All @@ -32,6 +34,8 @@ namespace dmRender
{
const dmVMath::Matrix4& GetViewMatrix(HRenderContext render_context);
const dmVMath::Matrix4& GetViewProjectionMatrix(HRenderContext render_context);
HMaterial NewMaterial(HRenderContext render_context, dmGraphics::HVertexProgram vertex_program, dmGraphics::HFragmentProgram fragment_program);
void SetMaterialTags(HMaterial material, uint32_t tag_count, const dmhash_t* tags);
}

// DM_RIVE_USE_OPENGL
Expand Down Expand Up @@ -62,6 +66,11 @@ namespace dmRive
dmResource::HFactory m_Factory;
rive::Renderer* m_RiveRenderer;
dmGraphics::HContext m_GraphicsContext;

dmGraphics::HVertexProgram m_BlitVs;
dmGraphics::HFragmentProgram m_BlitFs;
dmRender::HMaterial m_BlitMaterial;

uint32_t m_LastWidth;
uint32_t m_LastHeight;
uint8_t m_FrameBegin : 1;
Expand All @@ -86,23 +95,53 @@ namespace dmRive

static void AddShaderResources(dmResource::HFactory factory)
{
dmResource::AddFile(factory, "/defold-rive/assets/pls-shaders/rivemodel_blit.vpc", RIVEMODEL_BLIT_VPC_SIZE, RIVEMODEL_BLIT_VPC);
dmResource::AddFile(factory, "/defold-rive/assets/pls-shaders/rivemodel_blit.fpc", RIVEMODEL_BLIT_FPC_SIZE, RIVEMODEL_BLIT_FPC);
}

static void RemoveShaderResources(dmResource::HFactory factory)
{
dmResource::RemoveFile(factory, "/defold-rive/assets/pls-shaders/rivemodel_blit.vpc");
dmResource::RemoveFile(factory, "/defold-rive/assets/pls-shaders/rivemodel_blit.fpc");
}

dmResource::Result LoadShaders(dmResource::HFactory factory, ShaderResources** resources)
{
return dmResource::RESULT_OK;
dmResource::Result result = dmResource::RESULT_OK;

AddShaderResources(factory);

#define GET_SHADER(path, storage) \
result = dmResource::Get(factory, path, (void**) &storage); \
if (result != dmResource::RESULT_OK) \
{ \
dmLogError("Failed to load resource '%s'", path); \
return result; \
}

GET_SHADER("/defold-rive/assets/pls-shaders/rivemodel_blit.vpc", g_RiveRenderer->m_BlitVs);
GET_SHADER("/defold-rive/assets/pls-shaders/rivemodel_blit.fpc", g_RiveRenderer->m_BlitFs);

#undef GET_SHADER

RemoveShaderResources(factory);

return result;
}

static void ReleaseShadersInternal(dmResource::HFactory factory, ShaderResources* shaders)
static void ReleaseShadersInternal(dmResource::HFactory factory)
{
#define RELEASE_SHADER(res) \
if (res) dmResource::Release(factory, (void*) res);
RELEASE_SHADER(g_RiveRenderer->m_BlitVs);
RELEASE_SHADER(g_RiveRenderer->m_BlitFs);
#undef RELEASE_SHADER
}

void ReleaseShaders(dmResource::HFactory factory, ShaderResources** resources)
{
ReleaseShadersInternal(factory);
*resources = 0;
}

void DeleteRenderContext(HRenderContext context)
Expand Down Expand Up @@ -132,7 +171,7 @@ namespace dmRive
return renderer->m_RenderContext->GetBackingTexture();
}

void RenderBegin(HRenderContext context, ShaderResources* shaders, dmResource::HFactory factory)
void RenderBegin(HRenderContext context, dmResource::HFactory factory)
{
DefoldRiveRenderer* renderer = (DefoldRiveRenderer*) context;

Expand All @@ -148,22 +187,10 @@ namespace dmRive
uint32_t width = dmGraphics::GetWindowWidth(renderer->m_GraphicsContext);
uint32_t height = dmGraphics::GetWindowHeight(renderer->m_GraphicsContext);

uint32_t window_width = width;
uint32_t window_height = height;

// uint32_t fb_width = dmGraphics::GetWindowWidth(renderer->m_GraphicsContext);
// uint32_t fb_height = dmGraphics::GetWindowHeight(renderer->m_GraphicsContext);

// dmLogInfo("Window=(%d,%d), FB=(%d,%d)", fb_width, fb_height, width, height);
// width = window_width;
// height = window_height;

int32_t msaa_samples = 0;

if (width != renderer->m_LastWidth || height != renderer->m_LastHeight)
{
dmLogInfo("Change size to %d, %d", width, height);
renderer->m_RenderContext->OnSizeChanged(window_width, window_height, msaa_samples);
renderer->m_RenderContext->OnSizeChanged(width, height, 0);
renderer->m_LastWidth = width;
renderer->m_LastHeight = height;
}
Expand All @@ -174,10 +201,10 @@ namespace dmRive
#endif

renderer->m_RenderContext->BeginFrame({
.renderTargetWidth = window_width,
.renderTargetHeight = window_height,
.renderTargetWidth = width,
.renderTargetHeight = height,
.clearColor = 0x00000000,
.msaaSampleCount = msaa_samples,
.msaaSampleCount = 0,
// .disableRasterOrdering = s_forceAtomicMode,
// .wireframe = s_wireframe,
// .fillsDisabled = s_disableFill,
Expand Down Expand Up @@ -302,6 +329,20 @@ namespace dmRive
return texture != nullptr ? rive::make_rcp<rive::RiveRenderImage>(std::move(texture)) : nullptr;
}

dmRender::HMaterial GetBlitToBackBufferMaterial(HRenderContext context, dmRender::HRenderContext render_context)
{
DefoldRiveRenderer* renderer = (DefoldRiveRenderer*) context;
if (!renderer->m_BlitMaterial)
{
renderer->m_BlitMaterial = dmRender::NewMaterial(render_context, renderer->m_BlitVs, renderer->m_BlitFs);

dmhash_t dummy_tag = dmHashString64("rive");
dmRender::SetMaterialTags(renderer->m_BlitMaterial, 1, &dummy_tag);
}
assert(renderer->m_BlitMaterial);
return renderer->m_BlitMaterial;
}

rive::Mat2D GetViewTransform(HRenderContext context, dmRender::HRenderContext render_context)
{
const dmVMath::Matrix4& view_matrix = dmRender::GetViewMatrix(render_context);
Expand Down
26 changes: 25 additions & 1 deletion utils/build_pls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,18 @@ cp -r -v ${RIVECPP_ORIGINAL_DIR}/renderer/src/*.cpp ${RIVECPP_RENDERER_SOURCE_DI
cp -r -v ${RIVECPP_ORIGINAL_DIR}/renderer/src/*.hpp ${RIVECPP_RENDERER_SOURCE_DIR}/src
cp -r -v ${RIVECPP_ORIGINAL_DIR}/renderer/src/shaders ${RIVECPP_RENDERER_SHADER_DIR}

echo "*************************************************"
echo "Setup shader source variables"

source ${SCRIPT_DIR}/gen_embedded_shaders.sh

DEFOLDSHADERS_INPUT_DIR=${SCRIPT_DIR}/../defold-rive/assets/shader-library
DEFOLDSHADERS_INCLUDE_DIR=${SCRIPT_DIR}/../defold-rive/include/private/shaders
DEFOLDSHADERS_SOURCE_DIR=${SOURCE_DIR}/defoldshaders/src

mkdir -p ${DEFOLDSHADERS_INCLUDE_DIR}
mkdir -p ${DEFOLDSHADERS_SOURCE_DIR}

echo "*************************************************"

for platform in $PLATFORMS; do
Expand Down Expand Up @@ -526,8 +538,20 @@ for platform in $PLATFORMS; do
export DEFINES="${RIVE_RENDERER_DEFINES}"
fi

build_library rive_renderer $platform $platform_ne ${RIVECPP_RENDERER_SOURCE_DIR} ${BUILD}
#build_library rive_renderer $platform $platform_ne ${RIVECPP_RENDERER_SOURCE_DIR} ${BUILD}

echo "************************************************************"
echo "RIVE SHADERS ${platform}"
echo "************************************************************"
unset DEFINES
unset INCLUDES
export CXXFLAGS="-x c"
generate_cpp_sources ${platform} ${DEFOLDSHADERS_INPUT_DIR} ${DEFOLDSHADERS_SOURCE_DIR}
build_library riveshaders $platform $platform_ne ${DEFOLDSHADERS_SOURCE_DIR} ${BUILD}

mkdir -p ${DEFOLDSHADERS_INCLUDE_DIR}
rm -v ${DEFOLDSHADERS_INCLUDE_DIR}/*.gen.h
cp -v ${DEFOLDSHADERS_SOURCE_DIR}/*.gen.h ${DEFOLDSHADERS_INCLUDE_DIR}
done


Expand Down
21 changes: 1 addition & 20 deletions utils/gen_embedded_shaders.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function build_shader() {
echo "Compiling shader ${src} for platform ${platform} with ${shader_type}"
echo " ${output}"

java -cp ${BOB} ${class_name} ${src} ${dst} --platform=${platform} --variant=${variant} ${OUTPUT_SPIRV}
java -cp ${BOB} ${class_name} ${src} ${dst} ${platform} --platform=${platform} --variant=${variant} ${OUTPUT_SPIRV}
}

function generate_cpp_source() {
Expand Down Expand Up @@ -85,16 +85,9 @@ function generate_cpp_sources() {
local input_dir=$2
local target_dir=$3

local pls_impl_dir=${input_dir}/pls_subpass_load

# Allow for file patterns not returning any files
shopt -s nullglob

case ${platform} in
x86_64-win32)
pls_impl_dir=${input_dir}/pls_rw_texture;;
esac

for name in ${input_dir}/*.vp; do
local short_name=$(basename $name)
local shader=${target_dir}/${short_name}c
Expand All @@ -107,18 +100,6 @@ function generate_cpp_sources() {
build_shader FragmentProgramBuilder ${platform} ${name} ${shader}
done

for name in ${pls_impl_dir}/*.vp; do
local short_name=$(basename $name)
local shader=${target_dir}/${short_name}c
build_shader VertexProgramBuilder ${platform} ${name} ${shader}
done

for name in ${pls_impl_dir}/*.fp; do
local short_name=$(basename $name)
local shader=${target_dir}/${short_name}c
build_shader FragmentProgramBuilder ${platform} ${name} ${shader}
done

for name in ${target_dir}/*.vpc ${target_dir}/*.fpc; do
local short_name=$(basename $name)
local shader=${target_dir}/${short_name}
Expand Down

0 comments on commit 78483a4

Please sign in to comment.