[Mesa-dev] [PATCH 1/2] util: import cache.c/h from glsl
Marek Olšák
maraeo at gmail.com
Tue Nov 15 19:07:00 UTC 2016
From: Marek Olšák <marek.olsak at amd.com>
It's not dependent on GLSL and it can be useful for shader caches that don't
deal with GLSL.
v2: address review comments
---
configure.ac | 4 ----
src/compiler/Makefile.glsl.am | 4 ----
src/compiler/Makefile.sources | 4 ----
src/compiler/glsl/tests/cache_test.c | 22 +---------------------
src/util/Makefile.sources | 2 ++
src/{compiler/glsl/cache.c => util/disk_cache.c} | 16 +++++++++-------
src/{compiler/glsl/cache.h => util/disk_cache.h} | 11 +++++------
7 files changed, 17 insertions(+), 46 deletions(-)
rename src/{compiler/glsl/cache.c => util/disk_cache.c} (98%)
rename src/{compiler/glsl/cache.h => util/disk_cache.h} (99%)
diff --git a/configure.ac b/configure.ac
index 9d62b90..90845e6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1354,24 +1354,20 @@ AC_ARG_ENABLE([shader-cache],
[if test "x$with_sha1" != "x"; then
enable_shader_cache=yes
else
enable_shader_cache=no
fi])
if test "x$with_sha1" = "x"; then
if test "x$enable_shader_cache" = "xyes"; then
AC_MSG_ERROR([Cannot enable shader cache (no SHA-1 implementation found)])
fi
fi
-AM_CONDITIONAL([ENABLE_SHADER_CACHE], [test x$enable_shader_cache = xyes])
-if test "x$enable_shader_cache" = "xyes"; then
- AC_DEFINE([ENABLE_SHADER_CACHE], [1], [Enable shader cache])
-fi
if test "x$enable_dri" = xyes; then
if test "$enable_static" = yes; then
AC_MSG_ERROR([Cannot use static libraries for DRI drivers])
fi
# not a hard requirement as swrast does not depend on it
if test "x$have_libdrm" = xyes; then
DRI_PC_REQ_PRIV="libdrm >= $LIBDRM_REQUIRED"
fi
diff --git a/src/compiler/Makefile.glsl.am b/src/compiler/Makefile.glsl.am
index 3eac677..5b1d74e 100644
--- a/src/compiler/Makefile.glsl.am
+++ b/src/compiler/Makefile.glsl.am
@@ -124,24 +124,20 @@ glsl_glcpp_glcpp_LDADD = \
-lm
glsl_libglsl_la_LIBADD = \
nir/libnir.la \
glsl/libglcpp.la
glsl_libglsl_la_SOURCES = \
$(LIBGLSL_GENERATED_FILES) \
$(LIBGLSL_FILES)
-if ENABLE_SHADER_CACHE
-glsl_libglsl_la_SOURCES += $(LIBGLSL_SHADER_CACHE_FILES)
-endif
-
glsl_libstandalone_la_SOURCES = \
$(GLSL_COMPILER_CXX_FILES)
glsl_libstandalone_la_LIBADD = \
glsl/libglsl.la \
$(top_builddir)/src/libglsl_util.la \
$(top_builddir)/src/util/libmesautil.la \
$(PTHREAD_LIBS)
glsl_compiler_SOURCES = \
diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
index 08d93e0..d05bcac 100644
--- a/src/compiler/Makefile.sources
+++ b/src/compiler/Makefile.sources
@@ -129,24 +129,20 @@ LIBGLSL_FILES = \
glsl/opt_redundant_jumps.cpp \
glsl/opt_structure_splitting.cpp \
glsl/opt_swizzle_swizzle.cpp \
glsl/opt_tree_grafting.cpp \
glsl/opt_vectorize.cpp \
glsl/program.h \
glsl/propagate_invariance.cpp \
glsl/s_expression.cpp \
glsl/s_expression.h
-LIBGLSL_SHADER_CACHE_FILES = \
- glsl/cache.c \
- glsl/cache.h
-
# glsl_compiler
GLSL_COMPILER_CXX_FILES = \
glsl/ir_builder_print_visitor.cpp \
glsl/ir_builder_print_visitor.h \
glsl/opt_add_neg_to_sub.h \
glsl/standalone_scaffolding.cpp \
glsl/standalone_scaffolding.h \
glsl/standalone.cpp \
glsl/standalone.h
diff --git a/src/compiler/glsl/tests/cache_test.c b/src/compiler/glsl/tests/cache_test.c
index 724dfcd..94a3c1d 100644
--- a/src/compiler/glsl/tests/cache_test.c
+++ b/src/compiler/glsl/tests/cache_test.c
@@ -25,45 +25,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ftw.h>
#include <errno.h>
#include <stdarg.h>
#include "util/mesa-sha1.h"
-#include "cache.h"
+#include "util/disk_cache.h"
bool error = false;
#ifdef ENABLE_SHADER_CACHE
-void
-_mesa_warning(void *ctx, const char *fmt, ...);
-
-void
-_mesa_warning(void *ctx, const char *fmt, ...)
-{
- va_list vargs;
- (void) ctx;
-
- va_start(vargs, fmt);
-
- /* This output is not thread-safe, but that's good enough for the
- * standalone compiler.
- */
- fprintf(stderr, "Mesa warning: ");
- vfprintf(stderr, fmt, vargs);
- fprintf(stderr, "\n");
-
- va_end(vargs);
-}
static void
expect_equal(uint64_t actual, uint64_t expected, const char *test)
{
if (actual != expected) {
fprintf(stderr, "Error: Test '%s' failed: Expected=%ld, Actual=%ld\n",
test, expected, actual);
error = true;
}
}
diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources
index c5531c8..b7ca347 100644
--- a/src/util/Makefile.sources
+++ b/src/util/Makefile.sources
@@ -1,16 +1,18 @@
MESA_UTIL_FILES := \
bitscan.c \
bitscan.h \
bitset.h \
debug.c \
debug.h \
+ disk_cache.c \
+ disk_cache.h \
format_r11g11b10f.h \
format_rgb9e5.h \
format_srgb.h \
half_float.c \
half_float.h \
hash_table.c \
hash_table.h \
list.h \
macros.h \
mesa-sha1.c \
diff --git a/src/compiler/glsl/cache.c b/src/util/disk_cache.c
similarity index 98%
rename from src/compiler/glsl/cache.c
rename to src/util/disk_cache.c
index e74c27d..79242aa 100644
--- a/src/compiler/glsl/cache.c
+++ b/src/util/disk_cache.c
@@ -14,40 +14,42 @@
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
+#ifdef ENABLE_SHADER_CACHE
+
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#include <errno.h>
#include <dirent.h>
#include "util/u_atomic.h"
#include "util/mesa-sha1.h"
#include "util/ralloc.h"
#include "main/errors.h"
-#include "cache.h"
+#include "disk_cache.h"
/* Number of bits to mask off from a cache key to get an index. */
#define CACHE_INDEX_KEY_BITS 16
/* Mask for computing an index from a key. */
#define CACHE_INDEX_KEY_MASK ((1 << CACHE_INDEX_KEY_BITS) - 1)
/* The number of keys that can be stored in the index. */
#define CACHE_INDEX_MAX_KEYS (1 << CACHE_INDEX_KEY_BITS)
@@ -79,34 +81,32 @@ mkdir_if_needed(char *path)
{
struct stat sb;
/* If the path exists already, then our work is done if it's a
* directory, but it's an error if it is not.
*/
if (stat(path, &sb) == 0) {
if (S_ISDIR(sb.st_mode)) {
return 0;
} else {
- _mesa_warning(NULL,
- "Cannot use %s for shader cache (not a directory)"
- "---disabling.\n", path);
+ fprintf(stderr, "Cannot use %s for shader cache (not a directory)"
+ "---disabling.\n", path);
return -1;
}
}
int ret = mkdir(path, 0755);
if (ret == 0 || (ret == -1 && errno == EEXIST))
return 0;
- _mesa_warning(NULL,
- "Failed to create %s for shader cache (%s)---disabling.\n",
- path, strerror(errno));
+ fprintf(stderr, "Failed to create %s for shader cache (%s)---disabling.\n",
+ path, strerror(errno));
return -1;
}
/* Concatenate an existing path and a new name to form a new path. If the new
* path does not exist as a directory, create it then return the resulting
* name of the new path (ralloc'ed off of 'ctx').
*
* Returns NULL on any error, such as:
*
@@ -701,10 +701,12 @@ bool
cache_has_key(struct program_cache *cache, cache_key key)
{
uint32_t *key_chunk = (uint32_t *) key;
int i = *key_chunk & CACHE_INDEX_KEY_MASK;
unsigned char *entry;
entry = &cache->stored_keys[i + CACHE_KEY_SIZE];
return memcmp(entry, key, CACHE_KEY_SIZE) == 0;
}
+
+#endif
diff --git a/src/compiler/glsl/cache.h b/src/util/disk_cache.h
similarity index 99%
rename from src/compiler/glsl/cache.h
rename to src/util/disk_cache.h
index d804169..d4d9398 100644
--- a/src/compiler/glsl/cache.h
+++ b/src/util/disk_cache.h
@@ -14,31 +14,30 @@
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
-#pragma once
-#ifndef CACHE_H
-#define CACHE_H
+#ifndef DISK_CACHE_H
+#define DISK_CACHE_H
+
+#include <stdint.h>
+#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
-#include <stdint.h>
-#include <stdbool.h>
-
/* Size of cache keys in bytes. */
#define CACHE_KEY_SIZE 20
typedef uint8_t cache_key[CACHE_KEY_SIZE];
/* Provide inlined stub functions if the shader cache is disabled. */
#ifdef ENABLE_SHADER_CACHE
/**
--
2.7.4
More information about the mesa-dev
mailing list