First implementation of GrDawnUniformHandler.

Change-Id: I76824eca6e72505004b4dfb03fc35c2849176ecb
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/230459
Commit-Queue: Stephen White <senorblanco@chromium.org>
Reviewed-by: Greg Daniel <egdaniel@google.com>
This commit is contained in:
Stephen White 2019-07-30 10:37:47 -04:00 committed by Skia Commit-Bot
parent 1f94d3900b
commit 4da34bf747
3 changed files with 349 additions and 0 deletions

View File

@ -717,6 +717,8 @@ skia_dawn_sources = [
"$_src/gpu/dawn/GrDawnGpuCommandBuffer.h", "$_src/gpu/dawn/GrDawnGpuCommandBuffer.h",
"$_src/gpu/dawn/GrDawnRenderTarget.cpp", "$_src/gpu/dawn/GrDawnRenderTarget.cpp",
"$_src/gpu/dawn/GrDawnRenderTarget.h", "$_src/gpu/dawn/GrDawnRenderTarget.h",
"$_src/gpu/dawn/GrDawnUniformHandler.cpp",
"$_src/gpu/dawn/GrDawnUniformHandler.h",
"$_src/gpu/dawn/GrDawnUtil.cpp", "$_src/gpu/dawn/GrDawnUtil.cpp",
"$_src/gpu/dawn/GrDawnUtil.h", "$_src/gpu/dawn/GrDawnUtil.h",
] ]

View File

@ -0,0 +1,284 @@
/*
* Copyright 2019 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "src/gpu/dawn/GrDawnUniformHandler.h"
#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
GrDawnUniformHandler::GrDawnUniformHandler(GrGLSLProgramBuilder* program)
: INHERITED(program)
, fUniforms(kUniformsPerBlock)
{
}
const GrShaderVar& GrDawnUniformHandler::getUniformVariable(UniformHandle u) const {
return fUniforms[u.toIndex()].fVar;
}
const char* GrDawnUniformHandler::getUniformCStr(UniformHandle u) const {
return fUniforms[u.toIndex()].fVar.getName().c_str();
}
// FIXME: this code was ripped from GrVkUniformHandler; should be refactored.
namespace {
uint32_t grsltype_to_alignment_mask(GrSLType type) {
switch(type) {
case kByte_GrSLType: // fall through
case kUByte_GrSLType:
return 0x0;
case kByte2_GrSLType: // fall through
case kUByte2_GrSLType:
return 0x1;
case kByte3_GrSLType: // fall through
case kByte4_GrSLType:
case kUByte3_GrSLType:
case kUByte4_GrSLType:
return 0x3;
case kShort_GrSLType: // fall through
case kUShort_GrSLType:
return 0x1;
case kShort2_GrSLType: // fall through
case kUShort2_GrSLType:
return 0x3;
case kShort3_GrSLType: // fall through
case kShort4_GrSLType:
case kUShort3_GrSLType:
case kUShort4_GrSLType:
return 0x7;
case kInt_GrSLType:
case kUint_GrSLType:
return 0x3;
case kHalf_GrSLType: // fall through
case kFloat_GrSLType:
return 0x3;
case kHalf2_GrSLType: // fall through
case kFloat2_GrSLType:
return 0x7;
case kHalf3_GrSLType: // fall through
case kFloat3_GrSLType:
return 0xF;
case kHalf4_GrSLType: // fall through
case kFloat4_GrSLType:
return 0xF;
case kUint2_GrSLType:
return 0x7;
case kInt2_GrSLType:
return 0x7;
case kInt3_GrSLType:
return 0xF;
case kInt4_GrSLType:
return 0xF;
case kHalf2x2_GrSLType: // fall through
case kFloat2x2_GrSLType:
return 0x7;
case kHalf3x3_GrSLType: // fall through
case kFloat3x3_GrSLType:
return 0xF;
case kHalf4x4_GrSLType: // fall through
case kFloat4x4_GrSLType:
return 0xF;
// This query is only valid for certain types.
case kVoid_GrSLType:
case kBool_GrSLType:
case kTexture2DSampler_GrSLType:
case kTextureExternalSampler_GrSLType:
case kTexture2DRectSampler_GrSLType:
case kTexture2D_GrSLType:
case kSampler_GrSLType:
break;
}
SK_ABORT("Unexpected type");
return 0;
}
static inline uint32_t grsltype_to_size(GrSLType type) {
switch(type) {
case kByte_GrSLType:
case kUByte_GrSLType:
return 1;
case kByte2_GrSLType:
case kUByte2_GrSLType:
return 2;
case kByte3_GrSLType:
case kUByte3_GrSLType:
return 3;
case kByte4_GrSLType:
case kUByte4_GrSLType:
return 4;
case kShort_GrSLType:
return sizeof(int16_t);
case kShort2_GrSLType:
return 2 * sizeof(int16_t);
case kShort3_GrSLType:
return 3 * sizeof(int16_t);
case kShort4_GrSLType:
return 4 * sizeof(int16_t);
case kUShort_GrSLType:
return sizeof(uint16_t);
case kUShort2_GrSLType:
return 2 * sizeof(uint16_t);
case kUShort3_GrSLType:
return 3 * sizeof(uint16_t);
case kUShort4_GrSLType:
return 4 * sizeof(uint16_t);
case kInt_GrSLType:
return sizeof(int32_t);
case kUint_GrSLType:
return sizeof(int32_t);
case kHalf_GrSLType: // fall through
case kFloat_GrSLType:
return sizeof(float);
case kHalf2_GrSLType: // fall through
case kFloat2_GrSLType:
return 2 * sizeof(float);
case kHalf3_GrSLType: // fall through
case kFloat3_GrSLType:
return 3 * sizeof(float);
case kHalf4_GrSLType: // fall through
case kFloat4_GrSLType:
return 4 * sizeof(float);
case kUint2_GrSLType:
return 2 * sizeof(uint32_t);
case kInt2_GrSLType:
return 2 * sizeof(int32_t);
case kInt3_GrSLType:
return 3 * sizeof(int32_t);
case kInt4_GrSLType:
return 4 * sizeof(int32_t);
case kHalf2x2_GrSLType: // fall through
case kFloat2x2_GrSLType:
//TODO: this will be 4 * szof(float) on std430.
return 8 * sizeof(float);
case kHalf3x3_GrSLType: // fall through
case kFloat3x3_GrSLType:
return 12 * sizeof(float);
case kHalf4x4_GrSLType: // fall through
case kFloat4x4_GrSLType:
return 16 * sizeof(float);
// This query is only valid for certain types.
case kVoid_GrSLType:
case kBool_GrSLType:
case kTexture2DSampler_GrSLType:
case kTextureExternalSampler_GrSLType:
case kTexture2DRectSampler_GrSLType:
case kTexture2D_GrSLType:
case kSampler_GrSLType:
break;
}
SK_ABORT("Unexpected type");
return 0;
}
uint32_t get_ubo_offset(uint32_t* currentOffset,
GrSLType type,
int arrayCount) {
uint32_t alignmentMask = grsltype_to_alignment_mask(type);
// We want to use the std140 layout here, so we must make arrays align to 16 bytes.
if (arrayCount || type == kFloat2x2_GrSLType) {
alignmentMask = 0xF;
}
uint32_t offsetDiff = *currentOffset & alignmentMask;
if (offsetDiff != 0) {
offsetDiff = alignmentMask - offsetDiff + 1;
}
uint32_t uniformOffset = *currentOffset + offsetDiff;
SkASSERT(sizeof(float) == 4);
if (arrayCount) {
uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_size(type));
SkASSERT(0 == (elementSize & 0xF));
*currentOffset = uniformOffset + elementSize * arrayCount;
} else {
*currentOffset = uniformOffset + grsltype_to_size(type);
}
return uniformOffset;
}
}
GrGLSLUniformHandler::UniformHandle GrDawnUniformHandler::internalAddUniformArray(
uint32_t visibility,
GrSLType type,
const char* name,
bool mangleName,
int arrayCount,
const char** outName) {
UniformInfo& info = fUniforms.push_back();
info.fVisibility = visibility;
if (visibility == kFragment_GrShaderFlag) {
info.fUBOOffset = get_ubo_offset(&fCurrentFragmentUBOOffset, type, arrayCount);
} else {
info.fUBOOffset = get_ubo_offset(&fCurrentGeometryUBOOffset, type, arrayCount);
}
GrShaderVar& var = info.fVar;
char prefix = 'u';
if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
prefix = '\0';
}
fProgramBuilder->nameVariable(var.accessName(), prefix, name, mangleName);
var.setType(type);
var.setTypeModifier(GrShaderVar::kNone_TypeModifier);
var.setArrayCount(arrayCount);
SkString layoutQualifier;
layoutQualifier.appendf("offset = %d", info.fUBOOffset);
var.addLayoutQualifier(layoutQualifier.c_str());
if (outName) {
*outName = var.c_str();
}
return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
}
GrGLSLUniformHandler::SamplerHandle GrDawnUniformHandler::addSampler(const GrTexture* texture,
const GrSamplerState&,
const GrSwizzle& swizzle,
const char* name,
const GrShaderCaps* caps) {
SkASSERT(!"unimplemented");
SkASSERT(name && strlen(name));
return GrGLSLUniformHandler::SamplerHandle(0);
}
const char* GrDawnUniformHandler::samplerVariable(
GrGLSLUniformHandler::SamplerHandle handle) const {
SkASSERT(!"unimplemented");
return "";
}
GrSwizzle GrDawnUniformHandler::samplerSwizzle(GrGLSLUniformHandler::SamplerHandle handle) const {
SkASSERT(!"unimplemented");
return GrSwizzle();
}
void GrDawnUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
SkString uniformsString;
for (int i = 0; i < fUniforms.count(); ++i) {
if (fUniforms[i].fVisibility & visibility) {
fUniforms[i].fVar.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
uniformsString.append(";\n");
}
}
if (!uniformsString.isEmpty()) {
uint32_t uniformBinding;
const char* stage;
if (kVertex_GrShaderFlag == visibility) {
uniformBinding = kGeometryBinding;
stage = "vertex";
} else if (kGeometry_GrShaderFlag == visibility) {
uniformBinding = kGeometryBinding;
stage = "geometry";
} else {
SkASSERT(kFragment_GrShaderFlag == visibility);
uniformBinding = kFragBinding;
stage = "fragment";
}
out->appendf("layout (set = %d, binding = %d) uniform %sUniformBuffer\n{\n",
kUniformBufferBindGroup, uniformBinding, stage);
out->appendf("%s\n};\n", uniformsString.c_str());
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 2019 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrDawnUniformHandler_DEFINED
#define GrDawnUniformHandler_DEFINED
#include "src/gpu/GrAllocator.h"
#include "src/gpu/glsl/GrGLSLUniformHandler.h"
class GrDawnGpu;
class GrDawnUniformHandler : public GrGLSLUniformHandler {
public:
static const int kUniformsPerBlock = 8;
const GrShaderVar& getUniformVariable(UniformHandle u) const override;
const char* getUniformCStr(UniformHandle u) const override;
enum {
kUniformBufferBindGroup = 0,
kSamplerBindGroup = 1,
kTexelBufferBindGroup = 2,
};
struct UniformInfo {
GrShaderVar fVar;
int fUBOOffset;
int fVisibility;
};
typedef GrTAllocator<UniformInfo> UniformInfoArray;
enum {
kGeometryBinding = 0,
kFragBinding = 1,
};
private:
explicit GrDawnUniformHandler(GrGLSLProgramBuilder* program);
SamplerHandle addSampler(const GrTexture*, const GrSamplerState&, const GrSwizzle&,
const char* name, const GrShaderCaps*) override;
const char* samplerVariable(SamplerHandle handle) const override;
GrSwizzle samplerSwizzle(SamplerHandle handle) const override;
void appendUniformDecls(GrShaderFlags visibility, SkString*) const override;
UniformHandle internalAddUniformArray(uint32_t visibility,
GrSLType type,
const char* name,
bool mangleName,
int arrayCount,
const char** outName) override;
GrShaderVar fDummyVar;
UniformInfoArray fUniforms;
uint32_t fCurrentGeometryUBOOffset = 0;
uint32_t fCurrentFragmentUBOOffset = 0;
friend class GrDawnProgramBuilder;
typedef GrGLSLUniformHandler INHERITED;
};
#endif