2017-05-26 21:17:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "tests/Test.h"
|
2017-05-26 21:17:19 +00:00
|
|
|
|
2018-05-11 14:14:21 +00:00
|
|
|
#include <array>
|
|
|
|
#include <vector>
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/gpu/GrContext.h"
|
|
|
|
#include "include/private/GrResourceKey.h"
|
|
|
|
#include "src/core/SkMakeUnique.h"
|
|
|
|
#include "src/gpu/GrCaps.h"
|
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#include "src/gpu/GrGeometryProcessor.h"
|
|
|
|
#include "src/gpu/GrMemoryPool.h"
|
|
|
|
#include "src/gpu/GrOpFlushState.h"
|
2019-08-26 15:08:51 +00:00
|
|
|
#include "src/gpu/GrOpsRenderPass.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrRenderTargetContext.h"
|
|
|
|
#include "src/gpu/GrRenderTargetContextPriv.h"
|
|
|
|
#include "src/gpu/GrResourceProvider.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLVarying.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
|
2017-05-26 21:17:19 +00:00
|
|
|
|
|
|
|
GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
|
|
|
|
|
|
|
|
static constexpr int kBoxSize = 2;
|
|
|
|
static constexpr int kBoxCountY = 8;
|
|
|
|
static constexpr int kBoxCountX = 8;
|
|
|
|
static constexpr int kBoxCount = kBoxCountY * kBoxCountX;
|
|
|
|
|
|
|
|
static constexpr int kImageWidth = kBoxCountY * kBoxSize;
|
|
|
|
static constexpr int kImageHeight = kBoxCountX * kBoxSize;
|
|
|
|
|
|
|
|
static constexpr int kIndexPatternRepeatCount = 3;
|
|
|
|
constexpr uint16_t kIndexPattern[6] = {0, 1, 2, 1, 2, 3};
|
|
|
|
|
|
|
|
|
|
|
|
class DrawMeshHelper {
|
|
|
|
public:
|
|
|
|
DrawMeshHelper(GrOpFlushState* state) : fState(state) {}
|
|
|
|
|
|
|
|
sk_sp<const GrBuffer> getIndexBuffer();
|
|
|
|
|
|
|
|
template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const SkTArray<T>& data) {
|
|
|
|
return this->makeVertexBuffer(data.begin(), data.count());
|
|
|
|
}
|
2017-05-31 18:51:23 +00:00
|
|
|
template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const std::vector<T>& data) {
|
|
|
|
return this->makeVertexBuffer(data.data(), data.size());
|
|
|
|
}
|
2017-05-26 21:17:19 +00:00
|
|
|
template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const T* data, int count);
|
|
|
|
|
|
|
|
void drawMesh(const GrMesh& mesh);
|
|
|
|
|
|
|
|
private:
|
|
|
|
GrOpFlushState* fState;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Box {
|
|
|
|
float fX, fY;
|
|
|
|
GrColor fColor;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a GPU-backend specific test. It tries to test all possible usecases of GrMesh. The test
|
|
|
|
* works by drawing checkerboards of colored boxes, reading back the pixels, and comparing with
|
|
|
|
* expected results. The boxes are drawn on integer boundaries and the (opaque) colors are chosen
|
|
|
|
* from the set (r,g,b) = (0,255)^3, so the GPU renderings ought to produce exact matches.
|
|
|
|
*/
|
|
|
|
|
2018-06-07 15:05:56 +00:00
|
|
|
static void run_test(GrContext* context, const char* testName, skiatest::Reporter*,
|
2019-08-21 13:38:10 +00:00
|
|
|
const std::unique_ptr<GrRenderTargetContext>&, const SkBitmap& gold,
|
2018-06-07 15:05:56 +00:00
|
|
|
std::function<void(DrawMeshHelper*)> testFn);
|
2017-05-26 21:17:19 +00:00
|
|
|
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrMeshTest, reporter, ctxInfo) {
|
2018-06-07 15:05:56 +00:00
|
|
|
GrContext* context = ctxInfo.grContext();
|
2017-05-26 21:17:19 +00:00
|
|
|
|
2019-08-21 13:38:10 +00:00
|
|
|
auto rtc = context->priv().makeDeferredRenderTargetContext(
|
|
|
|
SkBackingFit::kExact, kImageWidth, kImageHeight, GrColorType::kRGBA_8888, nullptr);
|
2017-05-26 21:17:19 +00:00
|
|
|
if (!rtc) {
|
|
|
|
ERRORF(reporter, "could not create render target context.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTArray<Box> boxes;
|
|
|
|
SkTArray<std::array<Box, 4>> vertexData;
|
|
|
|
SkBitmap gold;
|
|
|
|
|
|
|
|
// ---- setup ----------
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
|
|
|
gold.allocN32Pixels(kImageWidth, kImageHeight);
|
|
|
|
|
|
|
|
SkCanvas goldCanvas(gold);
|
|
|
|
|
|
|
|
for (int y = 0; y < kBoxCountY; ++y) {
|
|
|
|
for (int x = 0; x < kBoxCountX; ++x) {
|
|
|
|
int c = y + x;
|
|
|
|
int rgb[3] = {-(c & 1) & 0xff, -((c >> 1) & 1) & 0xff, -((c >> 2) & 1) & 0xff};
|
|
|
|
|
|
|
|
const Box box = boxes.push_back() = {
|
2018-11-30 20:33:19 +00:00
|
|
|
float(x * kBoxSize),
|
|
|
|
float(y * kBoxSize),
|
|
|
|
GrColorPackRGBA(rgb[0], rgb[1], rgb[2], 255)
|
2017-05-26 21:17:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::array<Box, 4>& boxVertices = vertexData.push_back();
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
boxVertices[i] = {
|
2018-11-30 20:33:19 +00:00
|
|
|
box.fX + (i / 2) * kBoxSize,
|
|
|
|
box.fY + (i % 2) * kBoxSize,
|
|
|
|
box.fColor
|
2017-05-26 21:17:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
paint.setARGB(255, rgb[0], rgb[1], rgb[2]);
|
|
|
|
goldCanvas.drawRect(SkRect::MakeXYWH(box.fX, box.fY, kBoxSize, kBoxSize), paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---- tests ----------
|
|
|
|
|
2018-11-30 20:33:19 +00:00
|
|
|
#define VALIDATE(buff) \
|
|
|
|
do { \
|
|
|
|
if (!buff) { \
|
|
|
|
ERRORF(reporter, #buff " is null."); \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2017-05-26 21:17:19 +00:00
|
|
|
|
2018-06-07 15:05:56 +00:00
|
|
|
run_test(context, "setNonIndexedNonInstanced", reporter, rtc, gold,
|
|
|
|
[&](DrawMeshHelper* helper) {
|
|
|
|
SkTArray<Box> expandedVertexData;
|
|
|
|
for (int i = 0; i < kBoxCount; ++i) {
|
|
|
|
for (int j = 0; j < 6; ++j) {
|
|
|
|
expandedVertexData.push_back(vertexData[i][kIndexPattern[j]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw boxes one line at a time to exercise base vertex.
|
|
|
|
auto vbuff = helper->makeVertexBuffer(expandedVertexData);
|
|
|
|
VALIDATE(vbuff);
|
|
|
|
for (int y = 0; y < kBoxCountY; ++y) {
|
|
|
|
GrMesh mesh(GrPrimitiveType::kTriangles);
|
|
|
|
mesh.setNonIndexedNonInstanced(kBoxCountX * 6);
|
2019-01-29 19:38:50 +00:00
|
|
|
mesh.setVertexData(vbuff, y * kBoxCountX * 6);
|
2018-06-07 15:05:56 +00:00
|
|
|
helper->drawMesh(mesh);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test(context, "setIndexed", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
|
2017-05-26 21:17:19 +00:00
|
|
|
auto ibuff = helper->getIndexBuffer();
|
|
|
|
VALIDATE(ibuff);
|
|
|
|
auto vbuff = helper->makeVertexBuffer(vertexData);
|
|
|
|
VALIDATE(vbuff);
|
|
|
|
int baseRepetition = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
// Start at various repetitions within the patterned index buffer to exercise base index.
|
|
|
|
while (i < kBoxCount) {
|
|
|
|
GR_STATIC_ASSERT(kIndexPatternRepeatCount >= 3);
|
|
|
|
int repetitionCount = SkTMin(3 - baseRepetition, kBoxCount - i);
|
|
|
|
|
2017-06-13 16:55:06 +00:00
|
|
|
GrMesh mesh(GrPrimitiveType::kTriangles);
|
2019-01-29 19:38:50 +00:00
|
|
|
mesh.setIndexed(ibuff, repetitionCount * 6, baseRepetition * 6, baseRepetition * 4,
|
|
|
|
(baseRepetition + repetitionCount) * 4 - 1, GrPrimitiveRestart::kNo);
|
|
|
|
mesh.setVertexData(vbuff, (i - baseRepetition) * 4);
|
2017-05-26 21:17:19 +00:00
|
|
|
helper->drawMesh(mesh);
|
|
|
|
|
|
|
|
baseRepetition = (baseRepetition + 1) % 3;
|
|
|
|
i += repetitionCount;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-07 15:05:56 +00:00
|
|
|
run_test(context, "setIndexedPatterned", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
|
2017-05-26 21:17:19 +00:00
|
|
|
auto ibuff = helper->getIndexBuffer();
|
|
|
|
VALIDATE(ibuff);
|
|
|
|
auto vbuff = helper->makeVertexBuffer(vertexData);
|
|
|
|
VALIDATE(vbuff);
|
|
|
|
|
|
|
|
// Draw boxes one line at a time to exercise base vertex. setIndexedPatterned does not
|
|
|
|
// support a base index.
|
|
|
|
for (int y = 0; y < kBoxCountY; ++y) {
|
2017-06-13 16:55:06 +00:00
|
|
|
GrMesh mesh(GrPrimitiveType::kTriangles);
|
2019-01-29 19:38:50 +00:00
|
|
|
mesh.setIndexedPatterned(ibuff, 6, 4, kBoxCountX, kIndexPatternRepeatCount);
|
|
|
|
mesh.setVertexData(vbuff, y * kBoxCountX * 4);
|
2017-05-26 21:17:19 +00:00
|
|
|
helper->drawMesh(mesh);
|
|
|
|
}
|
|
|
|
});
|
2017-05-31 18:51:23 +00:00
|
|
|
|
|
|
|
for (bool indexed : {false, true}) {
|
2019-02-04 18:26:26 +00:00
|
|
|
if (!context->priv().caps()->instanceAttribSupport()) {
|
2017-05-31 18:51:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-06-07 15:05:56 +00:00
|
|
|
run_test(context, indexed ? "setIndexedInstanced" : "setInstanced",
|
2017-05-31 18:51:23 +00:00
|
|
|
reporter, rtc, gold, [&](DrawMeshHelper* helper) {
|
|
|
|
auto idxbuff = indexed ? helper->getIndexBuffer() : nullptr;
|
|
|
|
auto instbuff = helper->makeVertexBuffer(boxes);
|
|
|
|
VALIDATE(instbuff);
|
|
|
|
auto vbuff = helper->makeVertexBuffer(std::vector<float>{0,0, 0,1, 1,0, 1,1});
|
|
|
|
VALIDATE(vbuff);
|
|
|
|
auto vbuff2 = helper->makeVertexBuffer( // for testing base vertex.
|
|
|
|
std::vector<float>{-1,-1, -1,-1, 0,0, 0,1, 1,0, 1,1});
|
|
|
|
VALIDATE(vbuff2);
|
|
|
|
|
|
|
|
// Draw boxes one line at a time to exercise base instance, base vertex, and null vertex
|
|
|
|
// buffer. setIndexedInstanced intentionally does not support a base index.
|
|
|
|
for (int y = 0; y < kBoxCountY; ++y) {
|
2017-06-13 16:55:06 +00:00
|
|
|
GrMesh mesh(indexed ? GrPrimitiveType::kTriangles
|
|
|
|
: GrPrimitiveType::kTriangleStrip);
|
2017-05-31 18:51:23 +00:00
|
|
|
if (indexed) {
|
|
|
|
VALIDATE(idxbuff);
|
2019-01-29 19:38:50 +00:00
|
|
|
mesh.setIndexedInstanced(idxbuff, 6, instbuff, kBoxCountX, y * kBoxCountX,
|
|
|
|
GrPrimitiveRestart::kNo);
|
2017-05-31 18:51:23 +00:00
|
|
|
} else {
|
2019-01-29 19:38:50 +00:00
|
|
|
mesh.setInstanced(instbuff, kBoxCountX, y * kBoxCountX, 4);
|
2017-05-31 18:51:23 +00:00
|
|
|
}
|
|
|
|
switch (y % 3) {
|
|
|
|
case 0:
|
2019-02-04 18:26:26 +00:00
|
|
|
if (context->priv().caps()->shaderCaps()->vertexIDSupport()) {
|
2017-05-31 18:51:23 +00:00
|
|
|
if (y % 2) {
|
|
|
|
// We don't need this call because it's the initial state of GrMesh.
|
|
|
|
mesh.setVertexData(nullptr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Fallthru.
|
|
|
|
case 1:
|
2019-01-29 19:38:50 +00:00
|
|
|
mesh.setVertexData(vbuff);
|
2017-05-31 18:51:23 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2019-01-29 19:38:50 +00:00
|
|
|
mesh.setVertexData(vbuff2, 2);
|
2017-05-31 18:51:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
helper->drawMesh(mesh);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-05-26 21:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class GrMeshTestOp : public GrDrawOp {
|
|
|
|
public:
|
|
|
|
DEFINE_OP_CLASS_ID
|
|
|
|
|
2018-06-07 15:05:56 +00:00
|
|
|
static std::unique_ptr<GrDrawOp> Make(GrContext* context,
|
|
|
|
std::function<void(DrawMeshHelper*)> testFn) {
|
2019-02-04 18:26:26 +00:00
|
|
|
GrOpMemoryPool* pool = context->priv().opMemoryPool();
|
2018-06-19 17:09:54 +00:00
|
|
|
|
|
|
|
return pool->allocate<GrMeshTestOp>(testFn);
|
2018-06-07 15:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-06-12 14:11:12 +00:00
|
|
|
friend class GrOpMemoryPool; // for ctor
|
|
|
|
|
2017-05-26 21:17:19 +00:00
|
|
|
GrMeshTestOp(std::function<void(DrawMeshHelper*)> testFn)
|
|
|
|
: INHERITED(ClassID())
|
|
|
|
, fTestFn(testFn) {
|
|
|
|
this->setBounds(SkRect::MakeIWH(kImageWidth, kImageHeight),
|
|
|
|
HasAABloat::kNo, IsZeroArea::kNo);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* name() const override { return "GrMeshTestOp"; }
|
|
|
|
FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
|
2019-06-24 00:07:38 +00:00
|
|
|
GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
|
|
|
|
bool hasMixedSampledCoverage, GrClampType) override {
|
2019-01-15 18:53:00 +00:00
|
|
|
return GrProcessorSet::EmptySetAnalysis();
|
2017-06-16 14:04:34 +00:00
|
|
|
}
|
2017-05-26 21:17:19 +00:00
|
|
|
void onPrepare(GrOpFlushState*) override {}
|
2018-11-14 18:56:37 +00:00
|
|
|
void onExecute(GrOpFlushState* state, const SkRect& chainBounds) override {
|
2017-05-26 21:17:19 +00:00
|
|
|
DrawMeshHelper helper(state);
|
|
|
|
fTestFn(&helper);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void(DrawMeshHelper*)> fTestFn;
|
|
|
|
|
|
|
|
typedef GrDrawOp INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GrMeshTestProcessor : public GrGeometryProcessor {
|
|
|
|
public:
|
2017-05-31 18:51:23 +00:00
|
|
|
GrMeshTestProcessor(bool instanced, bool hasVertexBuffer)
|
2018-06-19 18:33:47 +00:00
|
|
|
: INHERITED(kGrMeshTestProcessor_ClassID) {
|
2017-05-31 18:51:23 +00:00
|
|
|
if (instanced) {
|
2018-09-14 20:16:55 +00:00
|
|
|
fInstanceLocation = {"location", kFloat2_GrVertexAttribType, kHalf2_GrSLType};
|
Change how GPs configure attributes
Adds setVertexAttributes and setInstanceAttributes. These take a pointer
to the first attribute, and a count. The count is the total number of
possible attributes, though some may not be initialized. The base class
computes the number of initialized attributes, pre-computes the strides,
and only allows subsequent access to the initialized attributes.
The attributes need to be allocated contiguously. Some GPs place them in
an array, though most just place them as consecutive members, and pass
a pointer to the first one.
Indexed access would be possible, but now it makes more sense to iterate
over all attributes, so enable that, and use range-based for everywhere.
Completely remove the per-attribute offset helper (again - possible, but
not real helpful), and make the stride always available. In many ops,
just use the GP's computed stride, rather than re-computing it.
Bug: skia:
Change-Id: Ie4cccb7969a98ee5a10b373e714fbd702e875b3e
Reviewed-on: https://skia-review.googlesource.com/c/169241
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2018-11-12 20:34:00 +00:00
|
|
|
fInstanceColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
|
|
|
|
this->setInstanceAttributes(&fInstanceLocation, 2);
|
2017-05-31 18:51:23 +00:00
|
|
|
if (hasVertexBuffer) {
|
Change how GPs configure attributes
Adds setVertexAttributes and setInstanceAttributes. These take a pointer
to the first attribute, and a count. The count is the total number of
possible attributes, though some may not be initialized. The base class
computes the number of initialized attributes, pre-computes the strides,
and only allows subsequent access to the initialized attributes.
The attributes need to be allocated contiguously. Some GPs place them in
an array, though most just place them as consecutive members, and pass
a pointer to the first one.
Indexed access would be possible, but now it makes more sense to iterate
over all attributes, so enable that, and use range-based for everywhere.
Completely remove the per-attribute offset helper (again - possible, but
not real helpful), and make the stride always available. In many ops,
just use the GP's computed stride, rather than re-computing it.
Bug: skia:
Change-Id: Ie4cccb7969a98ee5a10b373e714fbd702e875b3e
Reviewed-on: https://skia-review.googlesource.com/c/169241
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2018-11-12 20:34:00 +00:00
|
|
|
fVertexPosition = {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType};
|
|
|
|
this->setVertexAttributes(&fVertexPosition, 1);
|
2017-05-31 18:51:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
Change how GPs configure attributes
Adds setVertexAttributes and setInstanceAttributes. These take a pointer
to the first attribute, and a count. The count is the total number of
possible attributes, though some may not be initialized. The base class
computes the number of initialized attributes, pre-computes the strides,
and only allows subsequent access to the initialized attributes.
The attributes need to be allocated contiguously. Some GPs place them in
an array, though most just place them as consecutive members, and pass
a pointer to the first one.
Indexed access would be possible, but now it makes more sense to iterate
over all attributes, so enable that, and use range-based for everywhere.
Completely remove the per-attribute offset helper (again - possible, but
not real helpful), and make the stride always available. In many ops,
just use the GP's computed stride, rather than re-computing it.
Bug: skia:
Change-Id: Ie4cccb7969a98ee5a10b373e714fbd702e875b3e
Reviewed-on: https://skia-review.googlesource.com/c/169241
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2018-11-12 20:34:00 +00:00
|
|
|
fVertexPosition = {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType};
|
|
|
|
fVertexColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
|
|
|
|
this->setVertexAttributes(&fVertexPosition, 2);
|
2017-05-31 18:51:23 +00:00
|
|
|
}
|
2017-05-26 21:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* name() const override { return "GrMeshTest Processor"; }
|
|
|
|
|
Change how GPs configure attributes
Adds setVertexAttributes and setInstanceAttributes. These take a pointer
to the first attribute, and a count. The count is the total number of
possible attributes, though some may not be initialized. The base class
computes the number of initialized attributes, pre-computes the strides,
and only allows subsequent access to the initialized attributes.
The attributes need to be allocated contiguously. Some GPs place them in
an array, though most just place them as consecutive members, and pass
a pointer to the first one.
Indexed access would be possible, but now it makes more sense to iterate
over all attributes, so enable that, and use range-based for everywhere.
Completely remove the per-attribute offset helper (again - possible, but
not real helpful), and make the stride always available. In many ops,
just use the GP's computed stride, rather than re-computing it.
Bug: skia:
Change-Id: Ie4cccb7969a98ee5a10b373e714fbd702e875b3e
Reviewed-on: https://skia-review.googlesource.com/c/169241
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2018-11-12 20:34:00 +00:00
|
|
|
const Attribute& inColor() const {
|
|
|
|
return fVertexColor.isInitialized() ? fVertexColor : fInstanceColor;
|
|
|
|
}
|
|
|
|
|
2017-05-31 18:51:23 +00:00
|
|
|
void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
|
2018-06-19 18:33:47 +00:00
|
|
|
b->add32(fInstanceLocation.isInitialized());
|
Change how GPs configure attributes
Adds setVertexAttributes and setInstanceAttributes. These take a pointer
to the first attribute, and a count. The count is the total number of
possible attributes, though some may not be initialized. The base class
computes the number of initialized attributes, pre-computes the strides,
and only allows subsequent access to the initialized attributes.
The attributes need to be allocated contiguously. Some GPs place them in
an array, though most just place them as consecutive members, and pass
a pointer to the first one.
Indexed access would be possible, but now it makes more sense to iterate
over all attributes, so enable that, and use range-based for everywhere.
Completely remove the per-attribute offset helper (again - possible, but
not real helpful), and make the stride always available. In many ops,
just use the GP's computed stride, rather than re-computing it.
Bug: skia:
Change-Id: Ie4cccb7969a98ee5a10b373e714fbd702e875b3e
Reviewed-on: https://skia-review.googlesource.com/c/169241
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2018-11-12 20:34:00 +00:00
|
|
|
b->add32(fVertexPosition.isInitialized());
|
2017-05-31 18:51:23 +00:00
|
|
|
}
|
2017-05-26 21:17:19 +00:00
|
|
|
|
|
|
|
GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
|
|
|
|
|
2018-06-19 18:33:47 +00:00
|
|
|
private:
|
Change how GPs configure attributes
Adds setVertexAttributes and setInstanceAttributes. These take a pointer
to the first attribute, and a count. The count is the total number of
possible attributes, though some may not be initialized. The base class
computes the number of initialized attributes, pre-computes the strides,
and only allows subsequent access to the initialized attributes.
The attributes need to be allocated contiguously. Some GPs place them in
an array, though most just place them as consecutive members, and pass
a pointer to the first one.
Indexed access would be possible, but now it makes more sense to iterate
over all attributes, so enable that, and use range-based for everywhere.
Completely remove the per-attribute offset helper (again - possible, but
not real helpful), and make the stride always available. In many ops,
just use the GP's computed stride, rather than re-computing it.
Bug: skia:
Change-Id: Ie4cccb7969a98ee5a10b373e714fbd702e875b3e
Reviewed-on: https://skia-review.googlesource.com/c/169241
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2018-11-12 20:34:00 +00:00
|
|
|
Attribute fVertexPosition;
|
|
|
|
Attribute fVertexColor;
|
2018-06-19 18:33:47 +00:00
|
|
|
|
|
|
|
Attribute fInstanceLocation;
|
Change how GPs configure attributes
Adds setVertexAttributes and setInstanceAttributes. These take a pointer
to the first attribute, and a count. The count is the total number of
possible attributes, though some may not be initialized. The base class
computes the number of initialized attributes, pre-computes the strides,
and only allows subsequent access to the initialized attributes.
The attributes need to be allocated contiguously. Some GPs place them in
an array, though most just place them as consecutive members, and pass
a pointer to the first one.
Indexed access would be possible, but now it makes more sense to iterate
over all attributes, so enable that, and use range-based for everywhere.
Completely remove the per-attribute offset helper (again - possible, but
not real helpful), and make the stride always available. In many ops,
just use the GP's computed stride, rather than re-computing it.
Bug: skia:
Change-Id: Ie4cccb7969a98ee5a10b373e714fbd702e875b3e
Reviewed-on: https://skia-review.googlesource.com/c/169241
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2018-11-12 20:34:00 +00:00
|
|
|
Attribute fInstanceColor;
|
2017-05-26 21:17:19 +00:00
|
|
|
|
|
|
|
friend class GLSLMeshTestProcessor;
|
|
|
|
typedef GrGeometryProcessor INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GLSLMeshTestProcessor : public GrGLSLGeometryProcessor {
|
|
|
|
void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
|
|
|
|
FPCoordTransformIter&& transformIter) final {}
|
|
|
|
|
|
|
|
void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
|
|
|
|
const GrMeshTestProcessor& mp = args.fGP.cast<GrMeshTestProcessor>();
|
|
|
|
|
|
|
|
GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
|
|
|
|
varyingHandler->emitAttributes(mp);
|
Change how GPs configure attributes
Adds setVertexAttributes and setInstanceAttributes. These take a pointer
to the first attribute, and a count. The count is the total number of
possible attributes, though some may not be initialized. The base class
computes the number of initialized attributes, pre-computes the strides,
and only allows subsequent access to the initialized attributes.
The attributes need to be allocated contiguously. Some GPs place them in
an array, though most just place them as consecutive members, and pass
a pointer to the first one.
Indexed access would be possible, but now it makes more sense to iterate
over all attributes, so enable that, and use range-based for everywhere.
Completely remove the per-attribute offset helper (again - possible, but
not real helpful), and make the stride always available. In many ops,
just use the GP's computed stride, rather than re-computing it.
Bug: skia:
Change-Id: Ie4cccb7969a98ee5a10b373e714fbd702e875b3e
Reviewed-on: https://skia-review.googlesource.com/c/169241
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2018-11-12 20:34:00 +00:00
|
|
|
varyingHandler->addPassThroughAttribute(mp.inColor(), args.fOutputColor);
|
2017-05-26 21:17:19 +00:00
|
|
|
|
|
|
|
GrGLSLVertexBuilder* v = args.fVertBuilder;
|
2018-06-19 18:33:47 +00:00
|
|
|
if (!mp.fInstanceLocation.isInitialized()) {
|
Change how GPs configure attributes
Adds setVertexAttributes and setInstanceAttributes. These take a pointer
to the first attribute, and a count. The count is the total number of
possible attributes, though some may not be initialized. The base class
computes the number of initialized attributes, pre-computes the strides,
and only allows subsequent access to the initialized attributes.
The attributes need to be allocated contiguously. Some GPs place them in
an array, though most just place them as consecutive members, and pass
a pointer to the first one.
Indexed access would be possible, but now it makes more sense to iterate
over all attributes, so enable that, and use range-based for everywhere.
Completely remove the per-attribute offset helper (again - possible, but
not real helpful), and make the stride always available. In many ops,
just use the GP's computed stride, rather than re-computing it.
Bug: skia:
Change-Id: Ie4cccb7969a98ee5a10b373e714fbd702e875b3e
Reviewed-on: https://skia-review.googlesource.com/c/169241
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2018-11-12 20:34:00 +00:00
|
|
|
v->codeAppendf("float2 vertex = %s;", mp.fVertexPosition.name());
|
2017-05-31 18:51:23 +00:00
|
|
|
} else {
|
Change how GPs configure attributes
Adds setVertexAttributes and setInstanceAttributes. These take a pointer
to the first attribute, and a count. The count is the total number of
possible attributes, though some may not be initialized. The base class
computes the number of initialized attributes, pre-computes the strides,
and only allows subsequent access to the initialized attributes.
The attributes need to be allocated contiguously. Some GPs place them in
an array, though most just place them as consecutive members, and pass
a pointer to the first one.
Indexed access would be possible, but now it makes more sense to iterate
over all attributes, so enable that, and use range-based for everywhere.
Completely remove the per-attribute offset helper (again - possible, but
not real helpful), and make the stride always available. In many ops,
just use the GP's computed stride, rather than re-computing it.
Bug: skia:
Change-Id: Ie4cccb7969a98ee5a10b373e714fbd702e875b3e
Reviewed-on: https://skia-review.googlesource.com/c/169241
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2018-11-12 20:34:00 +00:00
|
|
|
if (mp.fVertexPosition.isInitialized()) {
|
|
|
|
v->codeAppendf("float2 offset = %s;", mp.fVertexPosition.name());
|
2017-05-31 18:51:23 +00:00
|
|
|
} else {
|
2017-09-20 15:24:15 +00:00
|
|
|
v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);");
|
2017-05-31 18:51:23 +00:00
|
|
|
}
|
2018-06-19 18:33:47 +00:00
|
|
|
v->codeAppendf("float2 vertex = %s + offset * %i;", mp.fInstanceLocation.name(),
|
2018-05-29 19:33:06 +00:00
|
|
|
kBoxSize);
|
2017-05-31 18:51:23 +00:00
|
|
|
}
|
2017-09-20 15:24:15 +00:00
|
|
|
gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
|
2017-05-26 21:17:19 +00:00
|
|
|
|
2018-02-14 20:38:14 +00:00
|
|
|
GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
|
2017-09-18 18:10:39 +00:00
|
|
|
f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
|
2017-05-26 21:17:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
|
|
|
|
return new GLSLMeshTestProcessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count) {
|
2019-02-05 14:41:37 +00:00
|
|
|
return sk_sp<const GrBuffer>(fState->resourceProvider()->createBuffer(
|
2019-02-07 16:31:24 +00:00
|
|
|
count * sizeof(T), GrGpuBufferType::kVertex, kDynamic_GrAccessPattern, data));
|
2017-05-26 21:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() {
|
|
|
|
GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
|
2017-10-16 17:01:07 +00:00
|
|
|
return fState->resourceProvider()->findOrCreatePatternedIndexBuffer(
|
|
|
|
kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey);
|
2017-05-26 21:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawMeshHelper::drawMesh(const GrMesh& mesh) {
|
2019-06-19 15:58:01 +00:00
|
|
|
GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrc, GrSwizzle::RGBA());
|
2017-05-31 18:51:23 +00:00
|
|
|
GrMeshTestProcessor mtp(mesh.isInstanced(), mesh.hasVertexData());
|
2019-08-26 15:08:51 +00:00
|
|
|
fState->opsRenderPass()->draw(mtp, pipeline, nullptr, nullptr, &mesh, 1,
|
|
|
|
SkRect::MakeIWH(kImageWidth, kImageHeight));
|
2017-05-26 21:17:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 15:05:56 +00:00
|
|
|
static void run_test(GrContext* context, const char* testName, skiatest::Reporter* reporter,
|
2019-08-21 13:38:10 +00:00
|
|
|
const std::unique_ptr<GrRenderTargetContext>& rtc, const SkBitmap& gold,
|
2017-05-26 21:17:19 +00:00
|
|
|
std::function<void(DrawMeshHelper*)> testFn) {
|
|
|
|
const int w = gold.width(), h = gold.height(), rowBytes = gold.rowBytes();
|
|
|
|
const uint32_t* goldPx = reinterpret_cast<const uint32_t*>(gold.getPixels());
|
|
|
|
if (h != rtc->height() || w != rtc->width()) {
|
|
|
|
ERRORF(reporter, "[%s] expectation and rtc not compatible (?).", testName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sizeof(uint32_t) * kImageWidth != gold.rowBytes()) {
|
|
|
|
ERRORF(reporter, "unexpected row bytes in gold image.", testName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkAutoSTMalloc<kImageHeight * kImageWidth, uint32_t> resultPx(h * rowBytes);
|
2018-11-05 20:06:26 +00:00
|
|
|
rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xbaaaaaad),
|
|
|
|
GrRenderTargetContext::CanClearFullscreen::kYes);
|
2018-06-07 15:05:56 +00:00
|
|
|
rtc->priv().testingOnly_addDrawOp(GrMeshTestOp::Make(context, testFn));
|
2019-07-01 17:05:28 +00:00
|
|
|
rtc->readPixels(gold.info(), resultPx, rowBytes, {0, 0});
|
2017-05-26 21:17:19 +00:00
|
|
|
for (int y = 0; y < h; ++y) {
|
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
uint32_t expected = goldPx[y * kImageWidth + x];
|
|
|
|
uint32_t actual = resultPx[y * kImageWidth + x];
|
|
|
|
if (expected != actual) {
|
|
|
|
ERRORF(reporter, "[%s] pixel (%i,%i): got 0x%x expected 0x%x",
|
|
|
|
testName, x, y, actual, expected);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|