blob: bbc99d4b19dbf5759af5d599804ead2178cfb1b7 [file] [log] [blame]
{{/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/}}
{{/* ---- Includes ---- */}}
{{Include "cpp_common.tmpl" }}
{{$ | Macro "gfx_api.cpp" | Format (Global "clang-format") | Write "gfx_api.cpp"}}
{{/*
-------------------------------------------------------------------------------
Entry point.
-------------------------------------------------------------------------------
*/}}
{{define "gfx_api.cpp"}}
{{AssertType $ "API"}}
{{template "C++.GeneratedHeader"}}
#include "gfx_api.h"
#include "interpreter.h"
#include "stack.h"
#include <gapic/get_gfx_proc_address.h>
#include <gapic/log.h>
namespace gapir {
namespace gfxapi {
namespace {
{{range $c := AllCommands $}}
{{if not (GetAnnotation $c "synthetic")}}
{{template "CommandHandler" $c}}
{{end}}
{{end}}
} // end of anonymous namespace
{{range $c := AllCommands $}}
{{if not (GetAnnotation $c "synthetic")}}
{{template "C++.FunctionPtrDecl" $c}} = nullptr;
{{end}}
{{end}}
{{template "Register" $}}
{{template "Initialize" $}}
} // namespace gfxapi
} // namespace gapir
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the Register function that registers all the functions with the
interpreter.
-------------------------------------------------------------------------------
*/}}
{{define "Register"}}
{{AssertType $ "API"}}
void Register(Interpreter* interpreter) {
{{range $c := AllCommands $}}
{{if not (GetAnnotation $c "synthetic")}}
{{$name := Macro "C++.Public" (Macro "CmdName" $c)}}
interpreter->registerFunction(Ids::{{$name}}, call{{$name}});
{{end}}
{{end}}
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the Initialize function that looks-up the API function addresses for
the currently bound context.
-------------------------------------------------------------------------------
*/}}
{{define "Initialize"}}
{{AssertType $ "API"}}
void Initialize() {
{{range $c := AllCommands $}}
{{if not (GetAnnotation $c "synthetic")}}
{{$name := Macro "CmdName" $c}}
{{$name}} = reinterpret_cast<{{Macro "C++.FunctionPtrType" $c}}>(gapic::GetGfxProcAddress("{{$name}}", false));
{{end}}
{{end}}
}
{{end}}
{{define "CommandHandler"}}
{{AssertType $ "Function"}}
{{$name := Macro "CmdName" $}}
bool call{{Macro "C++.Public" $name}}(Stack* stack, bool pushReturn) {
{{range $p := (Reverse $.CallParameters)}}
{{Macro "C++.ParameterType" $p}} {{$p.Name}} = stack->pop<{{Macro "C++.ParameterType" $p}}>();
{{end}}
if (stack->isValid()) {
GAPID_INFO("{{$name}}(§
{{range $i, $p := $.CallParameters}}
{{if $i}}, {{end}}§
{{if $p.Output}}%p§
{{else }}{{Macro "C++.PrintfFormatCode" (TypeOf $p)}}§
{{end}}
{{end}}
)\n"§
{{range $p := $.CallParameters}}
, {{$p.Name}}§
{{end}}
);
if ({{$name}} != nullptr) {
{{if not (IsVoid $.Return.Type)}}
{{Macro "C++.ReturnType" $}} return_value = {{Macro "Call" $}};
GAPID_INFO("Returned: {{Macro "C++.PrintfFormatCode" $.Return.Type}}\n", return_value);
if (pushReturn) {
stack->push<{{Macro "C++.ReturnType" $}}>(return_value);
}
{{else}}
{{Macro "Call" $}};
{{end}}
} else {
GAPID_WARNING("Attempted to call unsupported function {{$name}}\n");
}
{{if 0}} {{/* Enable for debugging */}}
const Error err = glGetError();
if (err != Error::GL_NO_ERROR) {
GAPID_WARNING("{{$name}} returned error: 0x%x\n", err);
}
{{end}}
return true;
} else {
GAPID_WARNING("Error during calling function {{$name}}\n");
return false;
}
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the logic to call the specified command
-------------------------------------------------------------------------------
*/}}
{{define "Call"}}
{{AssertType $ "Function"}}
{{Macro "CmdName" $}}(§
{{range $i, $p := $.CallParameters}}
{{if $i}}, {{end}}{{$p.Name}}§
{{end}})
{{end}}