2016-11-29 16:59:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
2017-03-09 14:03:58 +00:00
|
|
|
#include "GrClip.h"
|
2016-11-29 16:59:17 +00:00
|
|
|
#include "GrFragmentProcessor.h"
|
|
|
|
#include "GrRenderTargetContext.h"
|
|
|
|
#include "GrTexture.h"
|
|
|
|
#include "glsl/GrGLSLFragmentProcessor.h"
|
|
|
|
#include "glsl/GrGLSLFragmentShaderBuilder.h"
|
|
|
|
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageStorageLoad, reporter, ctxInfo) {
|
|
|
|
class TestFP : public GrFragmentProcessor {
|
|
|
|
public:
|
2017-08-11 13:40:37 +00:00
|
|
|
static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
|
|
|
|
GrSLMemoryModel mm,
|
|
|
|
GrSLRestrict restrict) {
|
|
|
|
return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(proxy), mm, restrict));
|
2016-11-29 16:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* name() const override { return "Image Load Test FP"; }
|
|
|
|
|
2017-08-11 13:40:37 +00:00
|
|
|
std::unique_ptr<GrFragmentProcessor> clone() const override {
|
|
|
|
return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
|
2017-07-28 17:41:51 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 16:59:17 +00:00
|
|
|
private:
|
2017-06-15 16:07:18 +00:00
|
|
|
TestFP(sk_sp<GrTextureProxy> proxy, GrSLMemoryModel mm, GrSLRestrict restrict)
|
2017-01-27 15:59:27 +00:00
|
|
|
: INHERITED(kNone_OptimizationFlags)
|
2017-05-12 18:49:16 +00:00
|
|
|
, fImageStorageAccess(std::move(proxy), kRead_GrIOType, mm, restrict) {
|
2016-11-29 16:59:17 +00:00
|
|
|
this->initClassID<TestFP>();
|
2017-06-15 16:07:18 +00:00
|
|
|
this->addImageStorageAccess(&fImageStorageAccess);
|
2016-11-29 16:59:17 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 17:41:51 +00:00
|
|
|
explicit TestFP(const TestFP& that)
|
|
|
|
: INHERITED(that.optimizationFlags())
|
|
|
|
, fImageStorageAccess(that.fImageStorageAccess) {
|
|
|
|
this->initClassID<TestFP>();
|
|
|
|
this->addImageStorageAccess(&fImageStorageAccess);
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:43:05 +00:00
|
|
|
void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
|
2016-11-29 16:59:17 +00:00
|
|
|
|
|
|
|
bool onIsEqual(const GrFragmentProcessor& that) const override { return true; }
|
|
|
|
|
|
|
|
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
|
|
|
|
class GLSLProcessor : public GrGLSLFragmentProcessor {
|
|
|
|
public:
|
|
|
|
GLSLProcessor() = default;
|
|
|
|
void emitCode(EmitArgs& args) override {
|
|
|
|
const TestFP& tfp = args.fFp.cast<TestFP>();
|
|
|
|
GrGLSLFPFragmentBuilder* fb = args.fFragBuilder;
|
|
|
|
SkString imageLoadStr;
|
2017-08-17 15:07:59 +00:00
|
|
|
fb->codeAppend("highp float2 coord = sk_FragCoord.xy;");
|
2016-11-29 16:59:17 +00:00
|
|
|
fb->appendImageStorageLoad(&imageLoadStr, args.fImageStorages[0],
|
2017-07-28 19:19:46 +00:00
|
|
|
"int2(coord)");
|
2017-05-29 16:37:20 +00:00
|
|
|
if (GrPixelConfigIsSint(tfp.fImageStorageAccess.peekTexture()->config())) {
|
2016-11-29 16:59:17 +00:00
|
|
|
// Map the signed bytes so that when then get read back as unorm values they
|
|
|
|
// will have their original bit pattern.
|
2017-08-17 15:07:59 +00:00
|
|
|
fb->codeAppendf("highp int4 ivals = %s;", imageLoadStr.c_str());
|
2016-11-29 16:59:17 +00:00
|
|
|
// NV gives a linker error for this:
|
|
|
|
// fb->codeAppend("ivals +=
|
2017-07-28 19:19:46 +00:00
|
|
|
// "mix(int4(0), int4(256), lessThan(ivals, int4(0)));");
|
2016-11-29 16:59:17 +00:00
|
|
|
fb->codeAppend("if (ivals.r < 0) { ivals.r += 256; }");
|
|
|
|
fb->codeAppend("if (ivals.g < 0) { ivals.g += 256; }");
|
|
|
|
fb->codeAppend("if (ivals.b < 0) { ivals.b += 256; }");
|
|
|
|
fb->codeAppend("if (ivals.a < 0) { ivals.a += 256; }");
|
2017-08-17 15:07:59 +00:00
|
|
|
fb->codeAppendf("%s = float4(ivals)/255;", args.fOutputColor);
|
2016-11-29 16:59:17 +00:00
|
|
|
} else {
|
|
|
|
fb->codeAppendf("%s = %s;", args.fOutputColor, imageLoadStr.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return new GLSLProcessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageStorageAccess fImageStorageAccess;
|
2017-01-27 15:59:27 +00:00
|
|
|
typedef GrFragmentProcessor INHERITED;
|
2016-11-29 16:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr int kS = 256;
|
|
|
|
GrContext* context = ctxInfo.grContext();
|
2016-11-29 20:29:41 +00:00
|
|
|
if (context->caps()->shaderCaps()->maxFragmentImageStorages() < 1) {
|
2016-11-29 16:59:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<uint32_t[]> data(new uint32_t[kS * kS]);
|
|
|
|
for (int j = 0; j < kS; ++j) {
|
|
|
|
for (int i = 0; i < kS; ++i) {
|
|
|
|
data[i + kS * j] = GrColorPackRGBA(i, j, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<uint32_t[]> idata(new uint32_t[kS * kS]);
|
|
|
|
for (int j = 0; j < kS; ++j) {
|
|
|
|
for (int i = 0; i < kS; ++i) {
|
|
|
|
int8_t r = i - 128;
|
|
|
|
int8_t g = j - 128;
|
|
|
|
int8_t b = -128;
|
|
|
|
int8_t a = -128;
|
|
|
|
idata[i + kS * j] = ((uint8_t)a << 24) | ((uint8_t)b << 16) |
|
|
|
|
((uint8_t)g << 8) | (uint8_t)r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently image accesses always have "top left" semantics.
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fOrigin = kTopLeft_GrSurfaceOrigin;
|
|
|
|
desc.fWidth = kS;
|
|
|
|
desc.fHeight = kS;
|
|
|
|
struct {
|
|
|
|
GrPixelConfig fConfig;
|
|
|
|
std::unique_ptr<uint32_t[]> fData;
|
|
|
|
} tests[] = {
|
|
|
|
{
|
|
|
|
kRGBA_8888_GrPixelConfig,
|
|
|
|
std::move(data)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
kRGBA_8888_sint_GrPixelConfig,
|
|
|
|
std::move(idata)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
for (const auto& test : tests) {
|
|
|
|
// This test should work with any memory model and with or without restrict
|
|
|
|
for (auto mm : {GrSLMemoryModel::kNone,
|
|
|
|
GrSLMemoryModel::kCoherent,
|
|
|
|
GrSLMemoryModel::kVolatile}) {
|
|
|
|
for (auto restrict : {GrSLRestrict::kNo, GrSLRestrict::kYes}) {
|
|
|
|
if (!context->caps()->canConfigBeImageStorage(test.fConfig)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
desc.fConfig = test.fConfig;
|
2017-04-06 11:59:41 +00:00
|
|
|
sk_sp<GrTextureProxy> imageStorageTexture =
|
|
|
|
GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc,
|
|
|
|
SkBudgeted::kYes, test.fData.get(), 0);
|
2016-11-29 16:59:17 +00:00
|
|
|
|
|
|
|
sk_sp<GrRenderTargetContext> rtContext =
|
2017-04-24 14:57:28 +00:00
|
|
|
context->makeDeferredRenderTargetContext(SkBackingFit::kExact, kS, kS,
|
|
|
|
kRGBA_8888_GrPixelConfig, nullptr);
|
2017-07-28 17:41:51 +00:00
|
|
|
// We make a clone to test that copying GrFragmentProcessor::ImageStorageAccess
|
2017-08-11 13:40:37 +00:00
|
|
|
// works.
|
|
|
|
std::unique_ptr<GrFragmentProcessor> fps[2];
|
|
|
|
fps[0] = TestFP::Make(imageStorageTexture, mm, restrict);
|
|
|
|
fps[1] = fps[0]->clone();
|
|
|
|
for (auto& fp : fps) {
|
2017-07-28 17:41:51 +00:00
|
|
|
GrPaint paint;
|
|
|
|
paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
|
2017-08-11 13:40:37 +00:00
|
|
|
paint.addColorFragmentProcessor(std::move(fp));
|
2017-07-28 17:41:51 +00:00
|
|
|
rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
|
|
|
|
std::unique_ptr<uint32_t[]> readData(new uint32_t[kS * kS]);
|
|
|
|
SkImageInfo info = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
|
|
|
rtContext->readPixels(info, readData.get(), 0, 0, 0);
|
|
|
|
int failed = false;
|
|
|
|
for (int j = 0; j < kS && !failed; ++j) {
|
|
|
|
for (int i = 0; i < kS && !failed; ++i) {
|
|
|
|
uint32_t d = test.fData[j * kS + i];
|
|
|
|
uint32_t rd = readData[j * kS + i];
|
|
|
|
if (d != rd) {
|
|
|
|
failed = true;
|
|
|
|
ERRORF(reporter, "Expected 0x%08x, got 0x%08x at %d, %d.",
|
|
|
|
d, rd, i, j);
|
|
|
|
}
|
2016-11-29 16:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|