Wire up texture sampling in unit-tests.

This commit is contained in:
Chinmay Garde
2021-07-06 17:05:06 -07:00
committed by Dan Field
parent 9792f9203d
commit 0f6b0b86d8
3 changed files with 34 additions and 16 deletions

View File

@@ -23,9 +23,7 @@ class Texture {
void SetLabel(const std::string_view& label);
[[nodiscard]] bool SetContents(const uint8_t* contents,
size_t length,
size_t mip_level = 0u);
[[nodiscard]] bool SetContents(const uint8_t* contents, size_t length);
bool IsValid() const;

View File

@@ -12,28 +12,40 @@ Texture::Texture(TextureDescriptor desc, id<MTLTexture> texture)
return;
}
if (desc_.size != GetSize()) {
FML_DLOG(ERROR)
<< "The texture and its descriptor disagree about its size.";
return;
}
is_valid_ = true;
}
Texture::~Texture() = default;
bool Texture::SetContents(const uint8_t* contents,
size_t length,
size_t mip_level) {
bool Texture::SetContents(const uint8_t* contents, size_t length) {
if (!IsValid() || !contents) {
return false;
}
FML_CHECK(false);
return false;
// Out of bounds access.
if (length != desc_.GetSizeOfBaseMipLevel()) {
return false;
}
// MTLRegionMake2D(NSUInteger x, NSUInteger y, NSUInteger width,
// NSUInteger height)
// TODO(csg): Perhaps the storage mode should be added to the texture
// descriptor so that invalid region replacements on potentially non-host
// visible textures are disallowed. The annoying bit about the API below is
// that there seems to be no error handling guidance.
const auto region =
MTLRegionMake2D(0u, 0u, desc_.size.width, desc_.size.height);
[texture_ replaceRegion:region //
mipmapLevel:0u //
withBytes:contents //
bytesPerRow:desc_.GetBytesPerRow() //
];
// [texture_ replaceRegion:(MTLRegion)
// mipmapLevel:(NSUInteger)withBytes:(nonnull const
// void*)bytesPerRow
// :(NSUInteger)];
return true;
}
ISize Texture::GetSize() const {

View File

@@ -58,7 +58,8 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) {
auto uploaded = texture->SetContents(result.GetAllocation()->GetMapping(),
result.GetAllocation()->GetSize());
ASSERT_TRUE(uploaded);
auto sampler = context->GetSamplerLibrary()->GetSampler({});
ASSERT_TRUE(sampler);
Renderer::RenderCallback callback = [&](const Surface& surface,
RenderPass& pass) {
BoxVertexShader::UniformBuffer uniforms;
@@ -67,11 +68,18 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) {
Command cmd;
cmd.label = "Box";
cmd.pipeline = box_pipeline;
cmd.vertex_bindings.buffers[VertexDescriptor::kReservedVertexBufferIndex] =
vertex_buffer.vertex_buffer;
cmd.vertex_bindings
.buffers[BoxVertexShader::kUniformUniformBuffer.binding] =
pass.GetTransientsBuffer().EmplaceUniform(uniforms);
cmd.fragment_bindings
.textures[BoxFragmentShader::kInputContentsTexture.location] = texture;
cmd.fragment_bindings
.samplers[BoxFragmentShader::kInputContentsTexture.location] = sampler;
cmd.index_buffer = vertex_buffer.index_buffer;
cmd.index_count = vertex_buffer.index_count;
cmd.primitive_type = PrimitiveType::kTriange;
@@ -80,7 +88,7 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) {
}
return true;
};
// OpenPlaygroundHere(callback);
OpenPlaygroundHere(callback);
}
} // namespace testing