Skip to content

Commit

Permalink
feat: run sksl
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed Dec 17, 2023
1 parent a9735e8 commit 587887f
Show file tree
Hide file tree
Showing 23 changed files with 331 additions and 29 deletions.
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Wasm Test",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/packages/wasm/c++/build_test/src/sksl-wasm-lib-test",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/packages/wasm/c++/build_test/src",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
27 changes: 21 additions & 6 deletions packages/extension/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
GetUniformsResponse,
RunSkSLRequest,
DebugSkSLRequest,
kRunUrl,
RunResult,
RunParams,
QueryParams,
} from '@workspace/runner-data'

export class Runner {
Expand Down Expand Up @@ -71,14 +75,17 @@ export class Runner {
panel.webview.postMessage(
pipe<SelectSkSLResponse>({
type: MessageType.kSelectSkSL,
path: uri.toString(),
path: uri.fsPath,
}),
)

const buffer = fs.readFileSync(uri.fsPath)
const result: QueryResult = await this.client.sendRequest(kQueryUrl, {
source: buffer.toString(),
})
const result: QueryResult = await this.client.sendRequest(
kQueryUrl,
pipe<QueryParams>({
source: buffer.toString(),
}),
)
panel.webview.postMessage(
pipe<GetUniformsResponse>({
type: MessageType.kGetUniforms,
Expand All @@ -88,8 +95,16 @@ export class Runner {
}

private async onRunSkSLRequest(panel: vscode.WebviewPanel, request: RunSkSLRequest) {
console.log(panel)
console.log(request)
const buffer = fs.readFileSync(request.path)
const result: RunResult = await this.client.sendRequest(
kRunUrl,
pipe<RunParams>({
source: buffer.toString(),
values: request.values,
}),
)
// TODO:
console.log(result.color)
}

private async onDebugSkSLRequest(panel: vscode.WebviewPanel, request: DebugSkSLRequest) {
Expand Down
6 changes: 5 additions & 1 deletion packages/extension/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ls from 'vscode-languageserver/node'
import * as lstd from 'vscode-languageserver-textdocument'
import { SkSLServer } from '@workspace/lsp'
import { QueryParams, kQueryUrl } from '@workspace/runner-data'
import { QueryParams, RunParams, kQueryUrl, kRunUrl } from '@workspace/runner-data'

const connection = ls.createConnection(ls.ProposedFeatures.all)
const documents = new ls.TextDocuments(lstd.TextDocument)
Expand Down Expand Up @@ -95,5 +95,9 @@ connection.onRequest(kQueryUrl, (params: QueryParams) => {
return server?.query(params)
})

connection.onRequest(kRunUrl, (params: RunParams) => {
return server?.run(params)
})

documents.listen(connection)
connection.listen()
7 changes: 7 additions & 0 deletions packages/float/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@workspace/float",
"version": "1.0.0",
"author": "seven332",
"license": "MIT",
"main": "src/float.ts"
}
File renamed without changes.
15 changes: 14 additions & 1 deletion packages/lsp/src/sksl-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import * as ls from 'vscode-languageserver/node'
import { FilePosition } from './file-position'
import { UTFOffset, dummyUTFOffset } from './utf-offset-converter'
import { decode, encode } from '@workspace/util'
import { QueryParams, QueryResult, dummyQueryResult } from '@workspace/runner-data'
import {
QueryParams,
QueryResult,
RunParams,
RunResult,
dummyQueryResult,
dummyRunResult,
} from '@workspace/runner-data'

export const kTokenTypes = [
'class',
Expand Down Expand Up @@ -266,6 +273,12 @@ export class SkSLServer {
return this.getResult<QueryResult>(dummyQueryResult)
}

public run(params: RunParams): RunResult {
this.setParams<RunParams>(params)
this.wasm._Run()
return this.getResult<RunResult>(dummyRunResult)
}

private files = new Map<string, Set<string>>()
private documents = new Map<string, Document>()

Expand Down
5 changes: 4 additions & 1 deletion packages/runner-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"version": "1.0.0",
"author": "seven332",
"license": "MIT",
"main": "src/index.ts"
"main": "src/index.ts",
"dependencies": {
"@workspace/float": "workspace:*"
}
}
1 change: 1 addition & 0 deletions packages/runner-data/src/url.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const kQueryUrl = 'sksl/query'
export const kRunUrl = 'sksl/run'
31 changes: 31 additions & 0 deletions packages/runner-data/src/wasm.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Float } from '@workspace/float'

export interface QueryParams {
source: string
}
Expand All @@ -23,3 +25,32 @@ export const dummyQueryResult: QueryResult = {
kind: '',
uniforms: [dummySkSLUniform],
}

export interface RunParams {
source: string
values: string[]
}

export interface SkSLColor {
r: Float
g: Float
b: Float
a: Float
}

export const dummySkSLColor: SkSLColor = {
r: Float.kZero,
g: Float.kZero,
b: Float.kZero,
a: Float.kZero,
}

export interface RunResult {
succeed: boolean
color: SkSLColor
}

export const dummyRunResult: RunResult = {
succeed: false,
color: dummySkSLColor,
}
5 changes: 4 additions & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"version": "1.0.0",
"author": "seven332",
"license": "MIT",
"main": "src/index.ts"
"main": "src/index.ts",
"dependencies": {
"@workspace/float": "workspace:*"
}
}
2 changes: 1 addition & 1 deletion packages/util/src/simple-codec.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Float } from './float'
import { Float } from '@workspace/float'
import { encode, decode } from './simple-codec'

interface TestObject {
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/simple-codec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DynamicBuffer } from './dynamic-buffer'
import { Float } from './float'
import { Float } from '@workspace/float'

export function encode(value: unknown): Uint8Array {
const buffer = new DynamicBuffer()
Expand Down
3 changes: 3 additions & 0 deletions packages/wasm/c++/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_library(sksl-wasm-lib STATIC
action/definition.cpp
action/completion.cpp
runner/query.cpp
runner/run.cpp
)
target_include_directories(sksl-wasm-lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(sksl-wasm-lib PUBLIC skia_sksl)
Expand All @@ -36,6 +37,7 @@ set(EXPORTED_FUNCTIONS
_Definition
_Completion
_Query
_Run
)
list(JOIN EXPORTED_FUNCTIONS "," EXPORTED_FUNCTIONS_JOINED)

Expand All @@ -57,6 +59,7 @@ add_executable(sksl-wasm-lib-test
formatter_test.cpp
simple_codec_test.cpp
utf_offset_test.cpp
runner/run_test.cpp
)
target_link_libraries(sksl-wasm-lib-test PRIVATE sksl-wasm-lib gmock_main)
enable_testing()
Expand Down
14 changes: 14 additions & 0 deletions packages/wasm/c++/src/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,17 @@ struct SkSLUniform {
Write(bytes, value.name);
}
};

struct SkSLColor {
float r;
float g;
float b;
float a;

friend void Write(std::vector<std::byte>* bytes, const SkSLColor& value) {
Write(bytes, value.r);
Write(bytes, value.g);
Write(bytes, value.b);
Write(bytes, value.a);
}
};
2 changes: 2 additions & 0 deletions packages/wasm/c++/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "action/update.h"
#include "module.h"
#include "runner/query.h"
#include "runner/run.h"
#include "utf_offset.h"

static Modules modules;
Expand Down Expand Up @@ -85,6 +86,7 @@ ACTION(Completion, "completion")
}

RUNNER(Query, "query")
RUNNER(Run, "run")

#undef RUNNER
}
84 changes: 84 additions & 0 deletions packages/wasm/c++/src/runner/run.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "runner/run.h"

#include <include/core/SkStream.h>
#include <src/core/SkRasterPipeline.h>
#include <src/sksl/SkSLCompiler.h>
#include <src/sksl/SkSLParser.h>
#include <src/sksl/SkSLProgramSettings.h>
#include <src/sksl/SkSLUtil.h>
#include <src/sksl/codegen/SkSLRasterPipelineBuilder.h>
#include <src/sksl/codegen/SkSLRasterPipelineCodeGenerator.h>
#include <src/sksl/ir/SkSLFunctionDeclaration.h>
#include <src/sksl/ir/SkSLProgram.h>
#include <src/sksl/tracing/SkSLDebugTracePlayer.h>
#include <src/sksl/tracing/SkSLDebugTracePriv.h>

#include <string>

#include "data.h"
#include "kind.h"

static std::unique_ptr<SkSL::Program> CompileProgram(std::string source) {
auto kind_result = GetKind(source);
if (!kind_result) {
return nullptr;
}

auto kind = ToSkSLProgramKind(kind_result->str);
if (!kind) {
return nullptr;
}

SkSL::Compiler compiler(SkSL::ShaderCapsFactory::Standalone());
SkSL::ProgramSettings settings;
settings.fUseMemoryPool = false;
settings.fOptimize = false;
settings.fForceNoInline = true;
if (SkSL::ProgramConfig::IsRuntimeEffect(*kind)) {
settings.fAllowNarrowingConversions = true;
}
SkSL::Parser parser(&compiler, settings, *kind, std::move(source));
return parser.program();
}

RunResult Run(RunParams params) {
RunResult result;

auto program = CompileProgram(std::move(params.source));
if (!program) {
return result;
}

const auto* function = program->getFunction("main");
if (!function) {
return result;
}

auto trace = sk_make_sp<SkSL::DebugTracePriv>();
std::unique_ptr<SkSL::RP::Program> raster_program =
SkSL::MakeRasterPipelineProgram(*program, *function->definition(), trace.get(), true);
if (!raster_program) {
return result;
}

static constexpr auto kFirstHeapAllocation = 4096;
SkArenaAlloc alloc(kFirstHeapAllocation);
SkRasterPipeline pipeline(&alloc);
// TODO: values
raster_program->appendStages(&pipeline, &alloc, nullptr, {});

SkRGBA4f<SkAlphaType::kPremul_SkAlphaType> out {};
SkRasterPipeline_MemoryCtx out_ctx = {&out, 0};
pipeline.append(SkRasterPipelineOp::store_f32, &out_ctx);
pipeline.run(0, 0, 1, 1);

result.succeed = true;
auto unpremul = out.unpremul();
result.color = SkSLColor {
.r = unpremul.fR,
.g = unpremul.fG,
.b = unpremul.fB,
.a = unpremul.fA,
};
return result;
}
31 changes: 31 additions & 0 deletions packages/wasm/c++/src/runner/run.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <string>
#include <vector>

#include "data.h"
#include "simple_codec.h"

struct RunParams {
std::string source;
std::vector<std::string> values;

friend std::size_t Read(std::span<const std::byte> bytes, std::size_t offset, RunParams* value) {
std::size_t read = 0;
read += Read(bytes, offset + read, &value->source);
read += Read(bytes, offset + read, &value->values);
return read;
}
};

struct RunResult {
bool succeed = false;
SkSLColor color {};

friend void Write(std::vector<std::byte>* bytes, const RunResult& value) {
Write(bytes, value.succeed);
Write(bytes, value.color);
}
};

RunResult Run(RunParams params);
Loading

0 comments on commit 587887f

Please sign in to comment.