Skip to content

Commit

Permalink
feat: query sksl uniforms
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed Dec 15, 2023
1 parent b982dc3 commit bb86588
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 6 deletions.
9 changes: 8 additions & 1 deletion packages/extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as path from 'path'
import * as vscode from 'vscode'
import { LanguageClient, TransportKind } from 'vscode-languageclient/node'
import * as fs from 'fs'
import { kQueryUrl } from './runner-data'
import { QueryResult } from '@workspace/lsp'

let client: LanguageClient | undefined

Expand Down Expand Up @@ -67,6 +69,11 @@ function showRunner(context: vscode.ExtensionContext, uri: vscode.Uri | undefine
)
}

function selectSkSL(panel: vscode.WebviewPanel, uri: vscode.Uri) {
async function selectSkSL(panel: vscode.WebviewPanel, uri: vscode.Uri) {
panel.webview.postMessage(uri.toString())
const buffer = fs.readFileSync(uri.fsPath)
const result: QueryResult | undefined = await client?.sendRequest(kQueryUrl, {
source: buffer.toString(),
})
console.log(result)
}
1 change: 1 addition & 0 deletions packages/extension/src/runner-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const kQueryUrl = 'sksl/query'
9 changes: 8 additions & 1 deletion packages/extension/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ls from 'vscode-languageserver/node'
import * as lstd from 'vscode-languageserver-textdocument'
import { SkSLServer } from '@workspace/lsp'
import { QueryParams, SkSLServer } from '@workspace/lsp'
import { kQueryUrl } from './runner-data'

const connection = ls.createConnection(ls.ProposedFeatures.all)
const documents = new ls.TextDocuments(lstd.TextDocument)
Expand Down Expand Up @@ -88,5 +89,11 @@ connection.onCompletion((params) => {
return server?.completion(params.textDocument.uri, params.position)
})

// Runner

connection.onRequest(kQueryUrl, (params: QueryParams) => {
return server?.query(params)
})

documents.listen(connection)
connection.listen()
2 changes: 1 addition & 1 deletion packages/lsp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"author": "seven332",
"license": "MIT",
"main": "src/sksl-server.ts",
"main": "src/index.ts",
"dependencies": {
"@workspace/util": "workspace:*",
"@workspace/wasm": "workspace:*",
Expand Down
2 changes: 2 additions & 0 deletions packages/lsp/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './runner-data'
export * from './sksl-server'
25 changes: 25 additions & 0 deletions packages/lsp/src/runner-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export interface QueryParams {
source: string
}

export interface SkSLUniform {
type: string
name: string
}

export const dummySkSLUniform: SkSLUniform = {
type: '',
name: '',
}

export interface QueryResult {
succeed: boolean
kind: string
uniforms: SkSLUniform[]
}

export const dummyQueryResult: QueryResult = {
succeed: false,
kind: '',
uniforms: [dummySkSLUniform],
}
9 changes: 9 additions & 0 deletions packages/lsp/src/sksl-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 './runner-data'

export const kTokenTypes = [
'class',
Expand Down Expand Up @@ -257,6 +258,14 @@ export class SkSLServer {
return result.items
}

// Runner

public query(params: QueryParams): QueryResult {
this.setParams<QueryParams>(params)
this.wasm._Query()
return this.getResult<QueryResult>(dummyQueryResult)
}

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

Expand Down
22 changes: 21 additions & 1 deletion packages/wasm/c++/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,38 @@ add_library(sksl-wasm-lib STATIC
action/hover.cpp
action/definition.cpp
action/completion.cpp
runner/query.cpp
)
target_include_directories(sksl-wasm-lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(sksl-wasm-lib PUBLIC skia_sksl)

set(EXPORTED_FUNCTIONS
_ToUTFOffsets
_SetParamsSize
_GetParamsPtr
_GetResultPtr
_GetResultSize
_Update
_Close
_GetSymbol
_Format
_GetToken
_GetTokenRange
_Hover
_Definition
_Completion
_Query
)
list(JOIN EXPORTED_FUNCTIONS "," EXPORTED_FUNCTIONS_JOINED)

add_executable(sksl-wasm main.cpp)
target_link_libraries(sksl-wasm PRIVATE sksl-wasm-lib)
target_link_options(sksl-wasm PRIVATE
--no-entry
-sENVIRONMENT=node
-sMODULARIZE
-sEXPORTED_RUNTIME_METHODS=ccall
-sEXPORTED_FUNCTIONS=_ToUTFOffsets,_SetParamsSize,_GetParamsPtr,_GetResultPtr,_GetResultSize,_Update,_Close,_GetSymbol,_Format,_GetToken,_GetTokenRange,_Hover,_Definition,_Completion
-sEXPORTED_FUNCTIONS=${EXPORTED_FUNCTIONS_JOINED}
-sALLOW_MEMORY_GROWTH=1
-sINITIAL_MEMORY=32MB
-sDISABLE_EXCEPTION_CATCHING=1
Expand Down
10 changes: 10 additions & 0 deletions packages/wasm/c++/src/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,13 @@ struct SkSLCompletion {
Write(bytes, static_cast<int>(value.kind));
}
};

struct SkSLUniform {
std::string type;
std::string name;

friend void Write(std::vector<std::byte>* bytes, const SkSLUniform& value) {
Write(bytes, value.type);
Write(bytes, value.name);
}
};
16 changes: 16 additions & 0 deletions packages/wasm/c++/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "action/hover.h"
#include "action/update.h"
#include "module.h"
#include "runner/query.h"
#include "utf_offset.h"

static Modules modules;
Expand Down Expand Up @@ -71,4 +72,19 @@ ACTION(Definition, "definition")
ACTION(Completion, "completion")

#undef ACTION

#define RUNNER(Type, Name) \
void Type() { \
Type##Params params; \
Read(params_bytes, 0, &params); \
std::cout << Name " start" << '\n'; \
auto result = Type(std::move(params)); \
std::cout << Name " end" << '\n'; \
result_bytes.clear(); \
Write(&result_bytes, result); \
}

RUNNER(Query, "query")

#undef RUNNER
}
61 changes: 61 additions & 0 deletions packages/wasm/c++/src/runner/query.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "runner/query.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/SkSLIRNode.h>
#include <src/sksl/ir/SkSLProgram.h>
#include <src/sksl/ir/SkSLVarDeclarations.h>

#include <optional>

#include "kind.h"

static std::unique_ptr<SkSL::Program> CompileProgram(std::string source, SkSL::ProgramKind kind) {
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();
}

QueryResult Query(QueryParams params) {
QueryResult result;

auto kind_result = GetKind(params.source);
if (!kind_result) {
return result;
}

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

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

result.succeed = true;
result.kind = kind_result->str;
for (const auto& element : program->fOwnedElements) {
if (element->kind() == SkSL::ProgramElementKind::kGlobalVar) {
auto* var = element->as<SkSL::GlobalVarDeclaration>().varDeclaration().var();
if (var->modifierFlags().isUniform()) {
result.uniforms.push_back(SkSLUniform {var->type().abbreviatedName(), std::string(var->name())});
}
}
}
return result;
}
31 changes: 31 additions & 0 deletions packages/wasm/c++/src/runner/query.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 QueryParams {
std::string source;

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

struct QueryResult {
bool succeed = false;
std::string kind;
std::vector<SkSLUniform> uniforms;

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

QueryResult Query(QueryParams params);
6 changes: 4 additions & 2 deletions packages/wasm/c++/third_party/skia/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ cmake_path(SET SKIA_SOURCE_DIR NORMALIZE ${CMAKE_CURRENT_SOURCE_DIR}/../external
cmake_path(SET SKIA_CMAKE_DIR NORMALIZE ${CMAKE_CURRENT_BINARY_DIR}/cmake)

add_library(skia_sksl STATIC
${SKIA_SOURCE_DIR}/src/core/SkRasterPipeline.cpp

${SKIA_SOURCE_DIR}/src/sksl/SkSLAnalysis.cpp
${SKIA_SOURCE_DIR}/src/sksl/SkSLBuiltinTypes.cpp
${SKIA_SOURCE_DIR}/src/sksl/SkSLCompiler.cpp
Expand Down Expand Up @@ -40,8 +42,8 @@ add_library(skia_sksl STATIC
# ${SKIA_SOURCE_DIR}/src/sksl/analysis/SkSLReturnsInputAlpha.cpp
${SKIA_SOURCE_DIR}/src/sksl/analysis/SkSLSwitchCaseContainsExit.cpp
${SKIA_SOURCE_DIR}/src/sksl/analysis/SkSLSymbolTableStackBuilder.cpp
# ${SKIA_SOURCE_DIR}/src/sksl/codegen/SkSLRasterPipelineBuilder.cpp
# ${SKIA_SOURCE_DIR}/src/sksl/codegen/SkSLRasterPipelineCodeGenerator.cpp
${SKIA_SOURCE_DIR}/src/sksl/codegen/SkSLRasterPipelineBuilder.cpp
${SKIA_SOURCE_DIR}/src/sksl/codegen/SkSLRasterPipelineCodeGenerator.cpp
${SKIA_SOURCE_DIR}/src/sksl/ir/SkSLBinaryExpression.cpp
${SKIA_SOURCE_DIR}/src/sksl/ir/SkSLBlock.cpp
${SKIA_SOURCE_DIR}/src/sksl/ir/SkSLChildCall.cpp
Expand Down
1 change: 1 addition & 0 deletions packages/wasm/src/sksl-wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface SkSLWasm extends EmscriptenModule {
_Hover(): void
_Definition(): void
_Completion(): void
_Query(): void
}

export default function createSkSLWasm(moduleOverrides?: Partial<EmscriptenModule>): Promise<SkSLWasm>

0 comments on commit bb86588

Please sign in to comment.