Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPIRV-Cross workarounds for early_fragment_tests #1171

Merged
merged 2 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gapis/api/gles/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,9 @@ func compat(ctx context.Context, device *device.Instance) (transform.Transformer
log.W(ctx, "%v compat: %v", cmd, err)
}
opts := shadertools.Options{
ShaderType: st,
Relaxed: true, // find_issues will still report bad GLSL.
ShaderType: st,
Relaxed: true, // find_issues will still report bad GLSL.
StripOptimizations: true,
}

// Trim any prefix whitespace / newlines.
Expand Down
1 change: 1 addition & 0 deletions gapis/shadertools/CMakeFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

set(files
shadertools.go
shadertools_test.go
)
set(dirs
cc
Expand Down
2 changes: 1 addition & 1 deletion gapis/shadertools/cc/libmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ code_with_debug_info_t* convertGlsl(const char* input, size_t length, const opti
strcpy(result->disassembly_string, tmp.c_str());
}

std::string source = spirv2glsl(std::move(spirv_new));
std::string source = spirv2glsl(std::move(spirv_new), options->strip_optimizations);

result->source_code = new char[source.length() + 1];
strcpy(result->source_code, source.c_str());
Expand Down
1 change: 1 addition & 0 deletions gapis/shadertools/cc/libmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ typedef struct options_t {
bool check_after_changes;
bool disassemble;
bool relaxed;
bool strip_optimizations;
} options_t;

typedef struct spirv_binary_t {
Expand Down
5 changes: 4 additions & 1 deletion gapis/shadertools/cc/spirv2glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
// so it is important we never include both at the same time.
#include "third_party/SPIRV-Cross/spirv_glsl.hpp"

std::string spirv2glsl(std::vector<uint32_t> spirv) {
std::string spirv2glsl(std::vector<uint32_t> spirv, bool strip_optimizations) {
spirv_cross::CompilerGLSL glsl(std::move(spirv));
spirv_cross::CompilerGLSL::Options cross_options;
cross_options.version = 330;
cross_options.es = false;
cross_options.force_temporary = false;
cross_options.vertex.fixup_clipspace = false;
glsl.set_options(cross_options);
if (strip_optimizations) {
glsl.unset_execution_mode(spv::ExecutionModeEarlyFragmentTests);
}
return glsl.compile();
}
2 changes: 1 addition & 1 deletion gapis/shadertools/cc/spirv2glsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
#include <string>
#include <vector>

std::string spirv2glsl(std::vector<uint32_t> spirv);
std::string spirv2glsl(std::vector<uint32_t> spirv, bool strip_optimizations);
31 changes: 19 additions & 12 deletions gapis/shadertools/shadertools.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var mutex sync.Mutex

// Instruction represents a SPIR-V instruction.
type Instruction struct {
Id uint32 // Result id.
ID uint32 // Result identifer.
Opcode uint32 // Opcode.
Words []uint32 // Operands.
Name string // Optional symbol name.
Expand All @@ -50,12 +50,13 @@ type CodeWithDebugInfo struct {
Info []Instruction // A set of SPIR-V debug instructions.
}

// FormatDebugInfo returns the instructions as a string.
func FormatDebugInfo(insts []Instruction, linePrefix string) string {
var buffer bytes.Buffer
for _, inst := range insts {
buffer.WriteString(linePrefix)
if inst.Id != 0 {
buffer.WriteString(fmt.Sprintf("%%%-5v = ", inst.Id))
if inst.ID != 0 {
buffer.WriteString(fmt.Sprintf("%%%-5v = ", inst.ID))
} else {
buffer.WriteString(fmt.Sprintf(" = "))
}
Expand Down Expand Up @@ -125,6 +126,11 @@ type Options struct {
Disassemble bool
// If true, let some minor invalid statements compile.
Relaxed bool
// If true, optimizations that require high-end GL versions, or extensions
// will be stripped. These optimizations should have no impact on the end
// result of the shader, but may impact performance.
// Example: Early Fragment Test.
StripOptimizations bool
}

// ConvertGlsl modifies the given GLSL according to the options specified via
Expand Down Expand Up @@ -159,6 +165,7 @@ func ConvertGlsl(source string, o *Options) (CodeWithDebugInfo, error) {
check_after_changes: C.bool(o.CheckAfterChanges),
disassemble: C.bool(o.Disassemble),
relaxed: C.bool(o.Relaxed),
strip_optimizations: C.bool(o.StripOptimizations),
}
result := C.convertGlsl(cstr(source), C.size_t(len(source)), &opts)
defer C.deleteGlslCodeWithDebug(result)
Expand All @@ -169,18 +176,18 @@ func ConvertGlsl(source string, o *Options) (CodeWithDebugInfo, error) {
}

if result.info != nil {
c_insts := (*[1 << 30]C.struct_instruction_t)(unsafe.Pointer(result.info.insts))
cInsts := (*[1 << 30]C.struct_instruction_t)(unsafe.Pointer(result.info.insts))
for i := 0; i < int(result.info.insts_num); i++ {
c_inst := c_insts[i]
cInst := cInsts[i]
inst := Instruction{
Id: uint32(c_inst.id),
Opcode: uint32(c_inst.opcode),
Words: make([]uint32, 0, c_inst.words_num),
Name: C.GoString(c_inst.name),
ID: uint32(cInst.id),
Opcode: uint32(cInst.opcode),
Words: make([]uint32, 0, cInst.words_num),
Name: C.GoString(cInst.name),
}
c_words := (*[1 << 30]C.uint32_t)(unsafe.Pointer(c_inst.words))
for j := 0; j < int(c_inst.words_num); j++ {
inst.Words = append(inst.Words, uint32(c_words[j]))
cWords := (*[1 << 30]C.uint32_t)(unsafe.Pointer(cInst.words))
for j := 0; j < int(cInst.words_num); j++ {
inst.Words = append(inst.Words, uint32(cWords[j]))
}
ret.Info = append(ret.Info, inst)
}
Expand Down
89 changes: 89 additions & 0 deletions gapis/shadertools/shadertools_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (C) 2017 Google Inc.
//
// 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.

package shadertools_test

import (
"testing"

"github.com/google/gapid/core/assert"
"github.com/google/gapid/core/log"
"github.com/google/gapid/gapis/shadertools"
)

func TestConvertGlsl(t *testing.T) {
for _, test := range []struct {
desc string
opts shadertools.Options
src string
expected string
}{
{
"Test shadertools propagates early_fragment_tests",
shadertools.Options{
ShaderType: shadertools.TypeFragment,
CheckAfterChanges: true,
},
`#version 310 es
layout(early_fragment_tests) in;
out highp vec4 color;
void main() { color = vec4(1., 1., 1., 1.); }`,
`#version 330
#ifdef GL_ARB_shading_language_420pack
#extension GL_ARB_shading_language_420pack : require
#endif
#extension GL_ARB_shader_image_load_store : require
layout(early_fragment_tests) in;

out vec4 color;

void main()
{
color = vec4(1.0);
}

`}, {
"Test shadertools strips early_fragment_tests",
shadertools.Options{
ShaderType: shadertools.TypeFragment,
CheckAfterChanges: true,
StripOptimizations: true,
},
`#version 310 es
layout(early_fragment_tests) in;
out highp vec4 color;
void main() { color = vec4(1., 1., 1., 1.); }`,
`#version 330
#ifdef GL_ARB_shading_language_420pack
#extension GL_ARB_shading_language_420pack : require
#endif

out vec4 color;

void main()
{
color = vec4(1.0);
}

`},
} {
t.Run(test.desc, func(t *testing.T) {
ctx := log.Testing(t)
out, err := shadertools.ConvertGlsl(test.src, &test.opts)
if assert.For(ctx, "err").ThatError(err).Succeeded() {
assert.For(ctx, "src").ThatString(out.SourceCode).Equals(test.expected)
}
})
}
}
2 changes: 1 addition & 1 deletion third_party/SPIRV-Cross
Submodule SPIRV-Cross updated 54 files
+3 −0 CMakeLists.txt
+11 −0 main.cpp
+16 −0 reference/shaders-hlsl/asm/comp/specialization-constant-workgroup.asm.comp
+26 −0 reference/shaders-hlsl/asm/comp/storage-buffer-basic.asm.comp
+8 −0 reference/shaders-hlsl/asm/vert/empty-struct-composite.asm.vert
+7 −1 reference/shaders-hlsl/comp/builtins.comp
+145 −0 reference/shaders-hlsl/frag/image-query-selective.frag
+131 −0 reference/shaders-hlsl/frag/image-query.frag
+26 −0 reference/shaders-hlsl/frag/matrix-input.frag
+71 −0 reference/shaders-hlsl/frag/mod.frag
+23 −0 reference/shaders-hlsl/vert/matrix-output.vert
+21 −0 reference/shaders-msl/asm/comp/specialization-constant-workgroup.asm.comp
+22 −0 reference/shaders-msl/asm/comp/storage-buffer-basic.asm.comp
+9 −0 reference/shaders-msl/asm/vert/empty-struct-composite.asm.vert
+17 −0 reference/shaders-msl/comp/builtins.comp
+2 −2 reference/shaders/asm/comp/bitcast_iadd.asm.comp
+2 −2 reference/shaders/asm/comp/bitcast_iequal.asm.comp
+2 −2 reference/shaders/asm/comp/bitcast_sar.asm.comp
+2 −2 reference/shaders/asm/comp/bitcast_sdiv.asm.comp
+2 −2 reference/shaders/asm/comp/bitcast_slr.asm.comp
+2 −2 reference/shaders/asm/comp/multiple-entry.asm.comp
+2 −2 reference/shaders/asm/comp/name-alias.asm.invalid.comp
+13 −0 reference/shaders/asm/comp/specialization-constant-workgroup.asm.comp
+20 −0 reference/shaders/asm/comp/storage-buffer-basic.asm.comp
+9 −0 reference/shaders/asm/frag/phi-loop-variable.asm.frag
+6 −0 reference/shaders/asm/vert/empty-struct-composite.asm.vert
+53 −0 reference/shaders/desktop-only/frag/image-query.desktop.frag
+47 −0 shaders-hlsl/asm/comp/specialization-constant-workgroup.asm.comp
+57 −0 shaders-hlsl/asm/comp/storage-buffer-basic.asm.comp
+37 −0 shaders-hlsl/asm/vert/empty-struct-composite.asm.vert
+1 −0 shaders-hlsl/comp/builtins.comp
+35 −0 shaders-hlsl/frag/image-query-selective.frag
+33 −0 shaders-hlsl/frag/image-query.frag
+9 −0 shaders-hlsl/frag/matrix-input.frag
+22 −0 shaders-hlsl/frag/mod.frag
+9 −0 shaders-hlsl/vert/matrix-output.vert
+47 −0 shaders-msl/asm/comp/specialization-constant-workgroup.asm.comp
+58 −0 shaders-msl/asm/comp/storage-buffer-basic.asm.comp
+37 −0 shaders-msl/asm/vert/empty-struct-composite.asm.vert
+12 −0 shaders-msl/comp/builtins.comp
+47 −0 shaders/asm/comp/specialization-constant-workgroup.asm.comp
+57 −0 shaders/asm/comp/storage-buffer-basic.asm.comp
+71 −0 shaders/asm/frag/phi-loop-variable.asm.frag
+37 −0 shaders/asm/vert/empty-struct-composite.asm.vert
+56 −0 shaders/desktop-only/frag/image-query.desktop.frag
+58 −98 spirv_common.hpp
+103 −105 spirv_cross.cpp
+25 −5 spirv_cross.hpp
+288 −66 spirv_glsl.cpp
+4 −0 spirv_glsl.hpp
+246 −27 spirv_hlsl.cpp
+25 −0 spirv_hlsl.hpp
+63 −19 spirv_msl.cpp
+18 −6 test_shaders.py